J
Jon Fairchild
"Index was out of range" error when editing DataRow
Our application makes thousands of edits to DataSets without problem,
but occasionally this error appears: "Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index"
I have found one place in the code where I can reproduce the error,
though still intermittently. In this case I am changing the value of
field in a DataRow. This field happens to be a foreign key to a parent
table. The parent table contains "Incidents" and the child table
contains "Devices", and they are related through an "Incident ID".
Here is the code that fails occasionally:
deviceRow["INCIDENT_ID"] = newIncidentID;
My testing indicates that the column name and value are valid. As the
stack trace of the error shows, the problem instead has to do with
changing the row state during the edit:
Exception: System.ArgumentOutOfRangeException
Message: Index was out of range. Must be non-negative and less than
the size of the collection.
Parameter name: index
Source: mscorlib
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Data.DataTable.RecordStateChanged(Int32 record1,
DataViewRowState oldState1, DataViewRowState newState1, Int32 record2,
DataViewRowState oldState2, DataViewRowState newState2)
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord, DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)
Any ideas on what could be causing this?
Thanks,
Jon Fairchild
Our application makes thousands of edits to DataSets without problem,
but occasionally this error appears: "Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index"
I have found one place in the code where I can reproduce the error,
though still intermittently. In this case I am changing the value of
field in a DataRow. This field happens to be a foreign key to a parent
table. The parent table contains "Incidents" and the child table
contains "Devices", and they are related through an "Incident ID".
Here is the code that fails occasionally:
deviceRow["INCIDENT_ID"] = newIncidentID;
My testing indicates that the column name and value are valid. As the
stack trace of the error shows, the problem instead has to do with
changing the row state during the edit:
Exception: System.ArgumentOutOfRangeException
Message: Index was out of range. Must be non-negative and less than
the size of the collection.
Parameter name: index
Source: mscorlib
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Data.DataTable.RecordStateChanged(Int32 record1,
DataViewRowState oldState1, DataViewRowState newState1, Int32 record2,
DataViewRowState oldState2, DataViewRowState newState2)
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord, DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)
Any ideas on what could be causing this?
Thanks,
Jon Fairchild