Nat
Nat

Reputation: 13

Deleting a row using entity framework

I'm trying to delete a row from a table in my database but I keep getting a null reference exception.

I know that the code I wrote isn't grabbing the ID number from the ComboBox but I don't know how to fix it.

Here's the code I have:

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    try
    {
        //select row to delete
        Doctor del = ((Doctor)cbDocIdd.SelectedItem);
        Doctor deleted = (from d in MainWindow.nlh.Doctors
                          where d.DoctorID == del.DoctorID
                          select d).First();
        //delete row from db
        MainWindow.nlh.Doctors.DeleteObject(deleted);
        //Save to database
        MainWindow.nlh.SaveChanges();
        MessageBox.Show("Doctor deleted");
        this.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Any help would be greatly appreciated.

This is the code used to fill the combobox:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        cbDocIdd.DataContext = MainWindow.nlh.Doctors;
    }

Upvotes: 1

Views: 350

Answers (2)

marc_s
marc_s

Reputation: 755207

You need to use .FirstOrDefault and check whether a valid entity has been found:

//select row to delete
Doctor del = ((Doctor)cbDocIdd.SelectedItem);
Doctor deleted = (from d in MainWindow.nlh.Doctors
                  where d.DoctorID == del.DoctorID
                  select d).FirstOrDefault();

// check if it even exists!!
if(deleted != null) 
{ 
    //delete row from db
    MainWindow.nlh.Doctors.DeleteObject(deleted);
    //Save to database
    MainWindow.nlh.SaveChanges();
    MessageBox.Show("Doctor deleted");
    this.Close();
}

If you use .First() and the doctor with that given ID doesn't exist, you'll get an exception

Also: make sure the del value is OK and not null before using it in the following statement. Same goes for MainWindow - are you sure this is not null ??

Update: can you try these two lines and tell me what the result is??

//select row to delete
object selectedObj = cbDocIdd.SelectedItem;

if(selectedObj != null)
{
    string typeOfSelectedObj = selectedObj.GetType().Name;
}

Doctor del = ((Doctor)cbDocIdd.SelectedItem);

Is selectedObj something other than null?? And if so: what type is it??

Update #2: Ok - so the doctor does exist and is being returned OK - can you try this for me??

Replace this line:

Doctor del = ((Doctor)cbDocIdd.SelectedItem);

with this instead:

Doctor del = cbDocIdd.SelectedItem as Doctor;

When you run this - is del now something other than null ?

Solution:

In the end, the real reason why this code broke lies in the fact that the call to MainWindow.nlh.Doctors.DeleteObject(deleted); caused an event handler to fire, which included this code:

private void cbDocIdd_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // populate with DoctorID from db
    Doctor deletedDoc = ((Doctor)cbDocIdd.SelectedItem);
    tbLastName.Text = deletedDoc.LastName.ToString();
    tbFirstName.Text = deletedDoc.FirstName.ToString();
    tbLicenseNumber.Text = deletedDoc.LicenseNumber.ToString();
}

but in this situation, when a doctor is being deleted, the deletedDoc was returned as NULL, but no check is in place to ensure to access only a non-NULL object ..... therefore, an infamous "null-reference exception" was thrown when trying to access the deletedDoc.LastName property...

Upvotes: 2

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

"null reference exception" shows when you try to use a method that is containing null value.

Such as if product have null value or assigned to null like product=null then you cannot use method as product.count() or something because null.count() is not supported.

    Doctor deleted = (from d in MainWindow.nlh.Doctors
                      where d.DoctorID == del.DoctorID
                      select d).FirstOrDefault(); 

use this and check null.

Upvotes: 0

Related Questions