Exception: DataTable internal index is corrupted: '5'. on ...
-
Thursday, June 22, 2006 11:09 AMI got this exception
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
.
.
.
on a Windows Form that contains two DataGridView controls that are both data-bound. It's a simple proof-of-concept application, with the data binding completely IDE-generated. I wrote no custom code for that part of the application.
In my case, the problem occurs after I delete the last row of one table from the DataGridView via a context menu selection and then add a new row to the table immediately thereafter. The delete code is shown below:
// Figure out who's doing the delete
ContextMenuStrip myMenu = (ContextMenuStrip)((ToolStripMenuItem)(sender)).GetCurrentParent();
DataGridView dgv = (DataGridView)myMenu.SourceControl;
// If we have something to delete
if (dgv.CurrentRow != null)
{
// Get its index
int ix = dgv.CurrentRow.Index;
BindingSource bs = (BindingSource)dgv.DataSource;
DataSet ds = (DataSet)bs.DataSource;
DataTable dt = ds.Tables[bs.DataMember];
dt.Rows[ix].Delete();
ds.AcceptChanges();
}
Note that if there's a better way to do the delete, I'd like to know about it. I've seen many different techniques, and I'm not it a position to judge which makes the most sense.
The row validation event handler that follows contains the following:
messagesBindingSource.EndEdit();
messagesTableAdapter.Update(xLogDataSet.Messages);
I have code in the row enter event handler that executes next to get a value from a column in the just-entered row and fill the other DataGridView based on that value.
The exception does not occur if I delete the next-to-last row this way and then add a new row at the end, nor does it occur if I use the Delete key (triggering the DataGridView's delete processsing) to delete the last row and then add a new row. There is no custom code for the add -- I simply go to the new row in the DataGridView, enter values, and tab out of the row. The same row validation code seen above executes on the add.- Moved by Val MazurModerator Tuesday, September 04, 2012 7:42 PM (From:ADO.NET Managed Providers)
All Replies
-
Tuesday, September 04, 2012 11:39 AM
Go through this :
http://stackoverflow.com/questions/450675/datatable-internal-index-is-corrupted
thanks
-
Wednesday, September 05, 2012 3:56 PMDon't do the ds.AcceptChanges() ... this is a common mistake that lots of people make. What the AcceptChanges() does is mark all DataRows as Unchanged AND it totally removes the Deleted DataRows. The Adapter.Update() looks for DataRows that are marked as Modified, Inserted or Deleted in order to update the database.
~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com

