0

I have a parent gridview and a detail gridview, I can insert/update/delete the parent gridview. I can insert new records on the detail gridviews But I cannot Update/delete any of the records in any of the detail gridviews I get the following exception.

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

I know this is not possible because I am the only one editing this record.

How can I fix this?

keyoke
  • 1,209
  • 12
  • 26

2 Answers2

0

First of all if you are doing something wrong with bind then follow this:
How to implement common scenarios when using the ASPxGridView component / MVC GridView Extension bound with EntityDataSource / Entity Framework

If everything correct then it is entity framework issue in storing the data, it is passed wrong at the time of delete or update operation. As i explored about it is that is cause by the entity's ID (key) field not being set. Thus when the context go to save the data, it could not find an primary key field = 0. Be sure to place a break point in your update statement and verify that the entity's ID has been set.

As i think try to set the KeyFieldName="KeyFieldName" of the detail grid. it should solve your problem.

Follow these links for reference:
Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

http://smisro.blogspot.in/2010/04/store-update-insert-or-delete-statement.html
Kevin McNeish solution on this

Ref: DevExpress Master Detail Example

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • this is very close to what is happening, I am not displaying one of the foreignkey id's as this is the master detail relationship id. I have included this as a hidden column in the gridview which fixes that but now the Version column is null when updating which I assume means I should include this as well but am not sure what the column type should be. – keyoke Apr 09 '12 at 10:54
0

Ok I found the answer, didn't need the additional column for version but mixed two support issues I found on the devexpress site and came up with the following.

    protected void gvPackages_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        e.OldValues["Version"] = GetVersionField(sender as ASPxGridView);
    }

    Byte[] GetVersionField(ASPxGridView grid)
    {
        return grid.GetRowValues(grid.EditingRowVisibleIndex, "Version") as Byte[];
    }

that works great, this together with the foreignkey id means that it now works as expected

keyoke
  • 1,209
  • 12
  • 26