I am getting Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
randomly emailed through to me on my website. I cannot reproduce this error either by force or general testing and it is somewhat confusing.
From what I can see in the stack trace, it happens randomly when opening a dataset and trying to get a value. The stack trace showed I got the error when calling this function:
function TDB.FGetLastInsertID: Integer;
const
selSQL = 'select scope_identity() as LastID';
var
selCursor: TDataSet;
begin
selCursor := Cursor(selSQL); //Returns a DataSet from a TADOQuery
try
Result := selCursor.FieldByName('LASTID').AsInteger;
finally
selCursor.Close;
selCursor.Free;
end;
end;
As I said, I cannot get this to reproduce the error and it seems to happen randomly on any query I run. I have tried closing the connection, setting it to inactive ect to reproduce the error but cannot.
Has anyone got any ideas?
EDIT: It seems its the close causing issues after examining the stack trace more closely:
Stack Trace: at
System.Collections.ArrayList.get_Item(Int32 index) at
Borland.Vcl.TList.Delete(Int32 Index) at Borland.Vcl.TList.Remove(Object Item) at
Borland.Vcl.TDBBufferList.FreeHGlobal(IntPtr Ptr) at
Borland.Vcl.TCustomADODataSet.FreeRecordBuffer(IntPtr& Buffer) at
Borland.Vcl.TDataSet.SetBufListSize(Int32 Value) at
Borland.Vcl.TDataSet.CloseCursor() at
Borland.Vcl.TDataSet.SetActive(Boolean Value) at Borland.Vcl.TDataSet.Close()
EDIT2: I have put a check in the code to make sure the selCursor.Active
before selCursor.Close. The stack trace suggests there is nothing to close.
Very odd and I still don't know why but it seems that randomly the Cursor is already closed. It seems that adding:
if (selCursor.Active) then
selCursor.Close;
has sorted the issue ...
Thanks for the answers and time spent trying to help.