InsertOnSubmit method throws NullReferenceException ... Linq to sql C# entity/DataContext class -
here's datacontext
class:
public class dealer : datacontext { public table<vehicle> vehicles; public table<customer> customers => gettable<customer>(); public table<account> accounts; public table<transaction> transactions; public dealer(string connection) : base(connection) { } }
here's customer
class:
[table(name="customers")] public class customer { [column(isprimarykey = true, dbtype = "int not null identity", isdbgenerated = true, canbenull = false)] public int customerid { get; set; } [column(canbenull = false)] public string firstname { get; set; } [column(canbenull = false)] public string lastname { get; set; } [column(canbenull = false)] public string ssn { get; set; } public override string tostring() { return string.concat(this.firstname, " ", this.lastname, " ", this.ssn); } private entityset<vehicle> vehicles = null; [association(storage = "vehicles", otherkey = "customerid", thiskey = "customerid")] public entityset<vehicle> vehicles { get; set; } //implement inotifypropertychanged public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string propertyname) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
here's code throws nullreferenceexception
:
private void button_click(object sender, routedeventargs e) { customer c = new customer() { firstname="john", lastname="smith", ssn = "123456" }; dealer d = new dealer(app.connectionstring); d.customers.insertonsubmit(c); //d.customers null , throws null reference exception.!!! try { d.submitchanges(); } catch (exception x) { messagebox.show(x.message); }
i have googled many hours , can't figure out why it's throwing nullreferenceexception
... (found lot of other posts non of solutions seem work me )
please ...
thanks in advance.
i had same problem yesterday. removing getters , setters datacontext class helped. way i'd change customerid column property addingautosync=autosync.oninsert
Comments
Post a Comment