Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im working on a content management system with visual studio 2012 and entity framework.
as i run this part:
C#
protected void grdResults_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    int w = int.Parse(grdResults.Rows[e.RowIndex].Cells[0].Text);
    NewsDbModel.NewsDbEntities db = new NewsDbModel.NewsDbEntities();
   
    var x = (from NewsTable in db.Tables where NewsTable.Id == w select NewsTable);
    db.DeleteObject(x); 
      
    db.SaveChanges();

}

i face this error:
"The object cannot be deleted because it was not found in the ObjectStateManager."

pls help me in solving this problem.
thanks alot
Posted
Updated 9-Sep-14 9:46am
v3
Comments
f.bateni 7-Sep-14 10:34am    
thanks dear George Jonsson
i did what u said. x hasnt null value so it inter the if but again faced the same error:"The object cannot be deleted because it was not found in the ObjectStateManager.
how can i make ObjectStateManager finds var x?
thanks again
George Jonsson 9-Sep-14 21:03pm    
I didn't see your comment coz you put it in the wrong place.
See below.

1 solution

Well, you try to delete the same object twice. That should be an issue.
Also add a check for your x variable so it actually contains a value.

C#
protected void grdResults_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    int w = int.Parse(grdResults.Rows[e.RowIndex].Cells[0].Text);
    NewsDbModel.NewsDbEntities db = new NewsDbModel.NewsDbEntities();

    var x = (from NewsTable in db.Tables where NewsTable.Id == w select NewsTable);
    if (x != null)    // Set a break point here and debug.
        db.DeleteObject(x);
    db.DeleteObject(x);

    db.SaveChanges();

}
 
Share this answer
 
Comments
Nelek 9-Sep-14 18:38pm    
The OP got the wrong "have a question or comment" widget and reply you in the question-comments. Have a look.
George Jonsson 9-Sep-14 21:00pm    
Thanks for the notice.
Nelek 10-Sep-14 4:07am    
You are welcome :)
George Jonsson 9-Sep-14 21:32pm    
Have you checked the value of 'w'?
Or first, check the value of grdResults.Rows[e.RowIndex].Cells[0].Text
f.bateni 10-Sep-14 11:31am    
yeah i checked it. it get correct id of my table
also value of grdResults.Rows[e.RowIndex].Cells[0].Text is true & x get value of the correct row of table.
i also sent it to another gridViwe to check it. but as it faced to db.DeletObject(x) it stopped and has run time error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900