Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
585
ObjectDataSource - MissingRecordException
posted

I have read numerous forum posts regarding MissingRecordException, but none of them could clarify my issue yet. Unfortunately, some of the provided links (to forum posts as well as to knowledge base articles) are broken and can not be accessed any more.

Scenario:
I have got a WebDataGrid, which is bound to an ObjectDataSource. The ODS again works with a custom ViewModel (return type = List<ViewModelItem>). The grid allows for adding new entries, copying existing entries, editing entries (in-place edit) and deleting entries.
The grid is configured for batch updating, sorting and filtering is done inside the view model. All CRUD operations are handled manually.

Issue:
Everything works as intended, except copying an existing entry. Usually, the user copies a few entries and updates their values, then copies a few more and updates those and so on. This works most of the time.
Sometimes tho, it seems that the grid is getting confused. When the user clicks to copy another entry, he will receive a MissingRecordException and loose all changes made so far.
Looking at the grid in the debugger reveals the following: Normally it would contain entries with - for example - unique position numbers 1 to 14, but right before the exception is thrown the inner list would show double position numbers while others are missing (like: 2, 2, 4, 4, 7, 7, 8, 8, 10, 10, 12, 12, 13, 13 => still 14 in total, but messed up).

Interestingly, the following code prevents the grid from throwing the exception and everything works fine:

protected void SampleItemDataGrid_DataBound(object sender, EventArgs e)
{
    List<string> ckList = new List<string>();
    foreach (GridRecord rec in SampleItemDataGrid.Rows)
    {
        ckList.Add(((SampleItem)rec.DataItem).CompositeKey);
    }
}

I am pretty sure that I missed out on something, but I could not figure it out from all the posts and articles I have read. I would be happy to get pointed in the right direction here or to be provided with an extensive sample on WebDataGrid and ObjectDataSource.

I have attached more code (aspx and aspx.cs) at the following links:
http://pastebin.com/cikmXZUE
http://pastebin.com/kbDZSKAf
It has been simplified and shortened, since the original .aspx file is more than 3k lines long.