简体   繁体   中英

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?

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM