Index was out of range. Must be non-negative...

  • Thread starter Thread starter William Ryan eMVP
  • Start date Start date
W

William Ryan eMVP

Gerard:

There could be a lot of things. I'm guessing that somewhere like the
table's collectino for instance, you are referencing something that doesn't
exist. Why not try/catch the Fill block and trap OleDbException. That
should give you a little more insight. If you want, send me the code at
bill At Devbuzz DotCom and I"ll be glad to take a look at it (or better post
the actual code if it's not too large).

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 
Hi,

A call of the Fill method of a OleDbAdaptor results in an exception: "Index
was out of range. Must be non-negative and less... Parameter name: index"
The error occurs sometimes after a fill after a 2nd update on a DataSet (ds
bound to a DataGrid and allocated once - the DataSet is not recreated after
an Update):

The DataSetFill looks like:
cmd = new OleDbCommand(query, Connection)
da = new OleDbAdaptor
da.SelectCommand = cmd
da.Fill(ds, tableName)
da.Dispose


The UpdateOfData looks like:
da = new OleDbAdaptor(query, Connection)
cmdBld = new OleDbCommandBuilder(da)
da.Update(ds, tableName)

So the order is:
ds = New DataSet

DataSetFill
dataGrid.datasource = ds
'' some changes ''
UpdateOfData

DataSetFill
dataGrid.datasource = ds
'' some changes ''
UpdateOfData

DataSetFill
EXCEPTION (da.Fill fails...)

Anybody an idea what's wrong and how to work around?
Thanks in advance,
Gerard
 
Hello Ryan,



Thanks for your help.

The situation is even more "bizarre": I don't even need to make an update, a
refill of the data will cause the problem after some tries (< 10). The stack
trace shows:



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.DataTable.LoadDataRow(Object[] values, Boolean
fAcceptChanges)

at System.Data.Common.SchemaMapping.LoadDataRow(Boolean clearDataValues,
Boolean acceptChanges)

at System.Data.Common.DbDataAdapter.FillLoadDataRow(SchemaMapping
mapping)

at System.Data.Common.DbDataAdapter.FillFromReader(Object data, String
srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords,
DataColumn parentChapterColumn, Object parentChapterValue)

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String
srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)

at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior)

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior)

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String
srcTable)

at Appl.DataRetrieval.ExecQuery(String query, DataSet ds, String tblName)





The ExecQuery fills the DataSet ds for two related tables: Customers and
PriceArgeements (in that order).

The DataSet ds is bound to a DataGrid and is allocated once. The ExecQuery
looks like:



Public Sub ExecQuery(ByVal query As String, ByVal ds As DataSet, ByVal
tblName As String)

Dim cmd As OleDbCommand



Try

cmd = New OleDb.OleDbCommand(query, Connection)

da = New OleDbDataAdapter



If cmd.Connection.State = ConnectionState.Open Then

cmd.Connection.Close()

End If



cmd.Connection.Open()



da.SelectCommand = cmd

da.Fill(ds, tblName)

da.Dispose()

Catch ex As Exception

Throw New Exception("ExecQuery failed (>>" & query & "<<)", ex)

Finally

cmd.Connection.Close()

End Try

End Sub



Try a some refills (of the DataGrid) and the error will occur.

Is there a way out?



Thank for any help.

Gerard
 
Back
Top