0

I'm trying to delete a row using the delete function provided trouhg to gridview wizard.

But when i click on delete I get following error:

An exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.dll but was not handled in user code

This is my code:

 public void deleteProject(DAL.Project p)
    {
        DAL.Project result = (from project in dc.Projects
                              where project.pk_project_id == p.pk_project_id
                              select project).Single();
        dc.Projects.DeleteOnSubmit(result);
        dc.SubmitChanges();
    }

when I debug it the value "p" has no properties.

The strange thing is that when I want to update my rows I use somewhat the same method:

 DAL.Project result = (from project in dc.Projects
                              where project.pk_project_id == p.pk_project_id
                              select project).Single();
        result.titel = p.titel;
        result.beschrijving = p.beschrijving;

        dc.SubmitChanges();

This goes well. So why doesn't it work with deleting rows?

4
  • it looks like DAL.Project is not being assigned before calling deleteProject, what is different about the update method? Commented Aug 27, 2014 at 13:04
  • nvm I thought you meant p was null Commented Aug 27, 2014 at 13:06
  • Indeed when I call the delete method "p" = null, but when calling update it is not null. The strange this is i call them both in the same way so their shouldn't be a problem. Commented Aug 27, 2014 at 13:28
  • You will need to figure out what is different between the two methods and the code that executes these methods. Commented Aug 27, 2014 at 13:53

1 Answer 1

1

Enumerable.Single throws an InvalidOperationException if the sequnce contains more than one item or theinput sequence is empty. You can use First or FirstOrDefault:

DAL.Project result = (from project in dc.Projects
                      where project.pk_project_id == p.pk_project_id
                      select project).FirstOrDefault();
if(result != null)
{
     // ...
}

(However, i don't know why the update work )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.