System.ArgumentOutOfRangeException: Index was out of range

H

hari

Anyone have a link to solve this one?
I am using the SqlData class in an asp.net page. The class fills a data grid which contains a column to render more data to a string builder class. The index of the data grid is derived by:
int nPatcom = (int) grid.DataKeys[e.Item.ItemIndex];

Even tried this to no avail:

int nID = Convert.ToInt32(grid.DataKeys[e.Item.ItemIndex]);

I've tried everything and spend days on this because it was working a month or so ago and I assumed that it was my error.

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) at ADO.SrchContacts.HandleCommands(Object sender, DataGridCommandEventArgs e) in o:\reports\pages\srchcontacts.aspx.cs:line 70

When I Google this I am told not to turn off connection pooling, which will solve the problem, and that framework 1.1 will fix this. Not so. The site runs on W2003 server.



Any help would be appreciated.

Harry
 
S

Scott Mitchell [MVP]

Hari, the problem is likely because somehow the DataKeyField of the
DataGrid got reset back to a blank value. Make sure that your
DataGrid's DataKeyField property is set, and is assigned to the property
field name being bound to the grid.

hth
Anyone have a link to solve this one?
I am using the SqlData class in an asp.net page. The class fills a data
grid which contains a column to render more data to a string builder
class. The index of the data grid is derived by:

int nPatcom = (int) grid.DataKeys[e.Item.ItemIndex];

Even tried this to no avail:

int nID = Convert.ToInt32(grid.DataKeys[e.Item.ItemIndex]);

I've tried everything and spend days on this because it was working a
month or so ago and I assumed that it was my error.

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index at System.Collections.ArrayList.get_Item(Int32 index) at
System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) at
ADO.SrchContacts.HandleCommands(Object sender, DataGridCommandEventArgs
e) in o:\reports\pages\srchcontacts.aspx.cs:line 70

When I Google this I am told not to turn off connection pooling, which
will solve the problem, and that framework 1.1 will fix this. Not so.
The site runs on W2003 server.



--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
G

Guest

I had a similar problem. You might want to try this -- the sample code on
MSDN leaves out a key bit: You have to explicitly assign a key field prior
to binding, even though the dataset may appear to have already assigned it
for you.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SQLDataAdapter1.Fill(DataSet1)
If Not IsPostBack Then
DataGrid1.DataKeyField() = "MyPrimaryKeyFieldName"
DataGrid1.DataBind()
End If
End Sub


Scott Mitchell said:
Hari, the problem is likely because somehow the DataKeyField of the
DataGrid got reset back to a blank value. Make sure that your
DataGrid's DataKeyField property is set, and is assigned to the property
field name being bound to the grid.

hth
Anyone have a link to solve this one?
I am using the SqlData class in an asp.net page. The class fills a data
grid which contains a column to render more data to a string builder
class. The index of the data grid is derived by:

int nPatcom = (int) grid.DataKeys[e.Item.ItemIndex];

Even tried this to no avail:

int nID = Convert.ToInt32(grid.DataKeys[e.Item.ItemIndex]);

I've tried everything and spend days on this because it was working a
month or so ago and I assumed that it was my error.

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index at System.Collections.ArrayList.get_Item(Int32 index) at
System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) at
ADO.SrchContacts.HandleCommands(Object sender, DataGridCommandEventArgs
e) in o:\reports\pages\srchcontacts.aspx.cs:line 70

When I Google this I am told not to turn off connection pooling, which
will solve the problem, and that framework 1.1 will fix this. Not so.
The site runs on W2003 server.



--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
Joined
Nov 15, 2006
Messages
1
Reaction score
0
Same error for ArrayList while binding.

Hi Scott,
I too getting the same error while binding the data to the Reapeter control, I have inline coding done where i have data in ArrayList.

_dataRepeater.DataBind(); - throws error here.
error msg: "Index was out of range. Must be non-negative and less than the size of the collection."

In ascx page I have:
<%# ((Phone)DataBinder.Eval(Container.DataItem, "PhoneList[0]")).AreaCode %>

can anybody have idea, how can i resolve this? All suggestions will be valuable. Thanks.


-----------------------------------------------------
Scott Mitchell [MVP] said:
Hari, the problem is likely because somehow the DataKeyField of the
DataGrid got reset back to a blank value. Make sure that your
DataGrid's DataKeyField property is set, and is assigned to the property
field name being bound to the grid.

hth

hari wrote:
> Anyone have a link to solve this one?
> I am using the SqlData class in an asp.net page. The class fills a data
> grid which contains a column to render more data to a string builder
> class. The index of the data grid is derived by:
>
> int nPatcom = (int) grid.DataKeys[e.Item.ItemIndex];
>
> Even tried this to no avail:
>
> int nID = Convert.ToInt32(grid.DataKeys[e.Item.ItemIndex]);
>
> I've tried everything and spend days on this because it was working a
> month or so ago and I assumed that it was my error.
>
> System.ArgumentOutOfRangeException: Index was out of range. Must be
> non-negative and less than the size of the collection. Parameter name:
> index at System.Collections.ArrayList.get_Item(Int32 index) at
> System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) at
> ADO.SrchContacts.HandleCommands(Object sender, DataGridCommandEventArgs
> e) in o:\reports\pages\srchcontacts.aspx.cs:line 70
>
> When I Google this I am told not to turn off connection pooling, which
> will solve the problem, and that framework 1.1 will fix this. Not so.
> The site runs on W2003 server.




--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top