0

I have this code for delete row in database by using LINQ to Entity in VB.net and when run i just receive a error message like that:

this object cannot be delete because it was not found in the ObjectStateManager

For Each row As DataGridViewRow In dataList
    Dim staffCd As String = row.Cells(Col_fCode).Value.ToString
    Dim deleteRow = From p In REMSDemoDB.W_frmMST020_A0_0 _
                              Where p.staffCD.Contains(staffCd) _
                              Select p
    REMSDemoDB.DeleteObject(deleteRow)
Next

Any help would be much appreciated

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
imDever
  • 1
  • 1
  • you might want to refer to this post: http://stackoverflow.com/questions/7791149/the-object-cannot-be-deleted-because-it-was-not-found-in-the-objectstatemanager – Hatjhie Oct 10 '14 at 03:21

1 Answers1

0

The result of

From p In REMSDemoDB.W_frmMST020_A0_0 _
          Where p.staffCD.Contains(staffCd) _
          Select p

is an IQueryable(Of T), not one instance.

You should get the instance to delete, e.g. by

REMSDemoDB.DeleteObject(deleteRow.FirstOrdefault())

(and check if it's not null).

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291