Asp.net webforms with Entity Framework 6 Update page - c#

I'm working with asp.net webforms and I'm a beginner with Entity Framework. I need to know how to make an update page and send data from page to a method (binding) without having to bind field by field and save changes.
Suppose I have this class :
public class Fiche
{
[Key]
public int Id { get; set; }
public string mrmme { get; set; }
public string nometprenom { get; set; }
public string adresse { get; set; }
public string ville { get; set; }
public string cp { get; set; }
public string tele { get; set; }
public string portable { get; set; }
public string datenaissance { get; set; }
public string email { get; set; }
public string situation { get; set; }
public string profession { get; set; }
#region // Santé & Prévoyance
public string typesp { get; set; }
public string nombresassures { get; set; }
public string regime { get; set; }
public string nbrenfants { get; set; }
public string mutuelleactuelle { get; set; }
public string tarifcontrat { get; set; }
public string niveaugaranties { get; set; }
public string mutuelle { get; set; }
#endregion
public string Createdbynom {
get {
return Data.Db.Users.Find(Createdby).nom;
}
}
public string Affectedtonom
{
get
{
return Data.Db.Users.Find(Affectedto).nom;
}
}
public ICollection<Contrat> Contrats { get; set; }
public ICollection<Commentaire> Commentaires { get; set; }
public string Createdby { get; set; }
[ForeignKey("Createdby")]
public ApplicationUser createuser { get; set; }
public string Affectedto { get; set; }
[ForeignKey("Affectedto")]
public ApplicationUser affetcteduser { get; set; }
}
I have lot of fields if I want to create a form to update this model. I need to create textbox for every field and when I click "submit" button, I need to get every value from every textbox and bind to model and call SaveChnages?
Is this the only way in webforms to update data a database?
Is there any way to binding data from my form to my update method?

Related

I am getting error while converting json to object

i am trying to convert JSON to object following is my json
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":"payment.captured","contains":["payment"],"payload":{"payment":{"entity":{"id":"pay_AKR45WLH0g1ANu","entity":"payment","amount":100,"currency":"INR","status":"captured","order_id":"order_AKR41LsWIgOAB1","invoice_id":null,"international":false,"method":"netbanking","amount_refunded":0,"refund_status":null,"captured":true,"description":"Admission Fees","card_id":null,"bank":"SBIN","wallet":null,"vpa":null,"email":"xxxxx.xxxx#xxx.xxx","contact":"xxxxx","notes":{"address":"NA","merchant_order_id":"2516"},"fee":2,"tax":0,"error_code":null,"error_description":null,"created_at":1528367383}}},"created_at":1528367384}
and the code I am trying to convert to object is
jsonString = JsonConvert.SerializeObject(documentContents);
RazorPayPayload desJsonString = JsonConvert.DeserializeObject<RazorPayPayload>(jsonString);
and the classes where I want to deserialized
public class RazorPayPayload
{
public string entity { get; set; }
public string account_id { get; set; }
public string events { get; set; }
public List<string> contains { get; set; }
public payments payload { get; set; }
public string created_at { get; set; }
}
public class payments
{
public Entities payment { get; set; }
}
public class notes
{
public string address { get; set; }
public string merchant_order_id { get; set; }
public string source { get; set; }
}
public class Entities
{
public Entity entity { get; set; }
}
public class Entity
{
public string id { get; set; }
public string entity { get; set; }
public string amount { get; set; }
public string currency { get; set; }
public string order_id { get; set; }
public string invoice_id { get; set; }
public string international { get; set; }
public string method { get; set; }
public string amount_refunded { get; set; }
public string refund_status { get; set; }
public string captured { get; set; }
public string description { get; set; }
public string card_id { get; set; }
public string bank { get; set; }
public string wallet { get; set; }
public string vpa { get; set; }
public string email { get; set; }
public string contact { get; set; }
public notes notes { get; set; }
public string fee { get; set; }
public string tax { get; set; }
public string error_code { get; set; }
public string error_description { get; set; }
}
I am getting the error "Error converting value to type 'FeePayr_Razor_Webhook.RazorPayPayload'"
Have you tried changing:
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":
to
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","events":
That would better match your class definition. Or rename your class field to event.
There is a useful visual studio function for converting a json object to c# classes.
Copy the JSON into the clipboard. (helps if the JSON object properties holds data, to determine the data type)
Go to visual studio and place cursor where you'd like to paste in the c# classes.
click edit => paste special => paste json as classes

asp.net EF code first model for PBX managment

I want to make the simple application to use with my PBX. I want to manage the providers and Lines tat connected to my PBX. The main objects are Provider and Line. Every provider has some lines connected to it. Lines can be added or deleted enabled or disabled and reserved. I want to keep the status of lines and history of actions in one table. Does this right solution or I need to create another table for history?
public class Line
{
public int LineId { get; set; }
public Provider Provider { get;set;}
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
}
What is the best solution for the code first model creating in the logic like that? Maybe I need to add some additional fields?
You need another table for history. Something like:
public class Line
{
public int LineId { get; set; }
public virtual Provider Provider { get; set; }
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
public virtual ICollection<LineStatusHistory> ChangeHistory { get; } = new HashSet<LineStatusHistory>();
}
public class LineStatusHistory
{
[Key( )]
public int LineId { get; set; }
[Key()]
public int LineStatusHistoryId { get; set; }
public virtual Line Line { get; set; }
public DateTime ChangeDate { get; set; }
public string Change { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
public virtual ICollection<Line> Lines { get; } = new HashSet<Line>();
}

PHP error with multiple levels of key-value JSON pairs

I am using multiple levels of JSON data coming into php from a C# application, as in:
public class User_Group
{
public int ID_UserGroup { get; set; }
public string Name_UserGroup { get; set; }
public int UserID { get; set; }
}
public class User_Role
{
public int ID_User { get; set; }
public string Role_User { get; set; }
public string User_Role_Description { get; set; }
public List<User_Group> UserGroup { get; set; }
}
public class Stand_Orte
{
public int ID { get; set; }
public string Bezeichnung { get; set; }
public List<Modul> modul { get; set; }
}
public class Modul
{
public string ID { get; set; }
public string Seriennummer { get; set; }
public string Bezeichnung { get; set; }
public string StandortID { get; set; }
public List<Mess_Kanal> MessKanal { get; set; }
}
public class Mess_Kanal
{
public string ID { get; set; }
public string ModulID { get; set; }
public List<LogMess_Daten> LogMessDaten { get; set; }
}
public class LogMess_Daten
{
public string KanalID { get; set; }
public string Zeitstempel { get; set; }
}
public class RootObject
{
public int ID_Project { get; set; }
public string Name_Project { get; set; }
public int Receiver_ID { get; set; }
public string Receiver_Name { get; set; }
public int UserID { get; set; }
public User_Role UserRole { get; set; }
public Stand_Orte Standorte { get; set; }
}
I have an issue with accessing the 3rd level data elements.
For Eg., I am able to get the values uptil Project-> Stand_Orte-> Modul. but after that Project-> Stand_Orte-> Modul-> MessKanal Throws an error in PHP as " Trying to get property of non-object ".
I tried the following:
$phpArray['project']->Standorte[0]->modul[0]->MessKanal[0]->ID
$messkanals=$phpArray->Standorte->modul->MessKanal;
And then used a "foreach ($messkanals as $messkanal)" to insert MessKanal data into MYSQL.
it gives me the following errors respectively.
Cannot use object of type stdClass as array
Trying to get property of non-object
does anyone have any idea?
Thanks,
Revathy

Automatic Creating of Views with dropdown on 1:n relations

I followed a Tutorial a few weeks ago which shows how to create a simple ASP.NET MVC 3 App (http://www.asp.net/mvc/tutorials/mvc-music-store-part-1).
Now I created another app that is actually doing something very simple, but I cant make Visual Studio 2010 automatically creating a View that shows a selection of the 1:n connection.
The program is a simple News system with a relation NewsEntry.NewsCategory to NewsCategory.ID.
NewsEntry.cs
public class NewsEntry
{
public int ID { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public NewsCategory NewsCategory { get; set; }
}
NewsDB.cs
public class NewsCategory
{
public int ID { get; set; }
public string Name { get; set; }
public List<NewsEntry> News { get; set; }
}
NewsDB.cs
public class NewsDB : DbContext
{
public DbSet<NewsEntry> NewsEntry { get; set; }
public DbSet<NewsCategory> NewsCategory { get; set; }
}
So my question is what is missing that VS is not creating a view with category in a drop down list?
It obviously helped to add a NewsCategoryId field:
public class NewsEntry
{
public int NewsEntryId { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public int NewsCategoryId { get; set; }
public virtual NewsCategory NewsCategory { get; set; }
}

Creating a class field of type IList<>?

I'm trying to create a POCO object called Friend.cs. I can't seem to create inline properties for the IList.
public class User
{
public string ID { get; set; }
public string Name { get; set; }
public string Rating { get; set; }
public string Photo { get; set; }
public string Reputation { get; set; }
public string Group { get; set; }
public string GroupColor { get; set; }
public string PostCount { get; set; }
public string PostPerDay { get; set; }
public string JoinDate { get; set; }
public string Views { get; set; }
public string LastActive { get; set; }
public string Title { get; set; }
public string Age { get; set; }
public string Birthday { get; set; }
public string Sex { get; set; }
public string LinkedIn { get; set; }
public string Facebook { get; set; }
public string Twitter { get; set; }
public IList<Friend> {get???
}
Thanks for the help!
You forget the name of the property:
public IList<Friend> Friends {get; set;}
should work.
Your missing the property name
ie
Public IList<Friend> Friends { get; set; }
public class User
{
public IList<Friend> Friends
{
get { return _friends; }
set { _friends = new List<Friend>(value); }
}
private List<Friend> _friends;
}

Categories

Resources