c# - Deleting a row using entity framework -


i'm trying delete row table in database keep getting null reference exception.

i know code wrote isn't grabbing id number combobox don't know how fix it.

here's code have:

private void btndelete_click(object sender, routedeventargs e) {     try     {         //select row delete         doctor del = ((doctor)cbdocidd.selecteditem);         doctor deleted = (from d in mainwindow.nlh.doctors                           d.doctorid == del.doctorid                           select d).first();         //delete row db         mainwindow.nlh.doctors.deleteobject(deleted);         //save database         mainwindow.nlh.savechanges();         messagebox.show("doctor deleted");         this.close();     }     catch (exception ex)     {         messagebox.show(ex.message);     } } 

any appreciated.

this code used fill combobox:

private void window_loaded(object sender, routedeventargs e)     {         cbdocidd.datacontext = mainwindow.nlh.doctors;     } 

you need use .firstordefault , check whether valid entity has been found:

//select row delete doctor del = ((doctor)cbdocidd.selecteditem); doctor deleted = (from d in mainwindow.nlh.doctors                   d.doctorid == del.doctorid                   select d).firstordefault();  // check if exists!! if(deleted != null)  {      //delete row db     mainwindow.nlh.doctors.deleteobject(deleted);     //save database     mainwindow.nlh.savechanges();     messagebox.show("doctor deleted");     this.close(); } 

if use .first() , doctor given id doesn't exist, you'll exception

also: make sure del value ok , not null before using in following statement. same goes mainwindow - sure not null ??

update: can try these 2 lines , tell me result is??

//select row delete object selectedobj = cbdocidd.selecteditem;  if(selectedobj != null) {     string typeofselectedobj = selectedobj.gettype().name; }  doctor del = ((doctor)cbdocidd.selecteditem); 

is selectedobj other null?? , if so: type it??

update #2: ok - doctor exist , being returned ok - can try me??

replace line:

doctor del = ((doctor)cbdocidd.selecteditem); 

with instead:

doctor del = cbdocidd.selecteditem doctor; 

when run - del other null ?

solution:

in end, real reason why code broke lies in fact call mainwindow.nlh.doctors.deleteobject(deleted); caused event handler fire, included code:

private void cbdocidd_selectionchanged(object sender, selectionchangedeventargs e) {     // populate doctorid db     doctor deleteddoc = ((doctor)cbdocidd.selecteditem);     tblastname.text = deleteddoc.lastname.tostring();     tbfirstname.text = deleteddoc.firstname.tostring();     tblicensenumber.text = deleteddoc.licensenumber.tostring(); } 

but in situation, when doctor being deleted, deleteddoc returned null, no check in place ensure access non-null object ..... therefore, infamous "null-reference exception" thrown when trying access deleteddoc.lastname property...


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -