user683326
user683326

Reputation: 39

ASP.net delete row using gridview

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?

Upvotes: 0

Views: 434

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460118

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 )

Upvotes: 1

Related Questions