System.ArgumentOutOfRangeException: Index was out of range

Discussion in 'ASP .Net' started by hari, Oct 26, 2004.

  1. hari

    hari Guest

    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
    hari, Oct 26, 2004
    #1
    1. Advertising

  2. 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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Oct 26, 2004
    #2
    1. Advertising

  3. 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 [MVP]" wrote:

    > 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
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
    >
    =?Utf-8?B?VGFsZXNwaW5uZXI=?=, Nov 19, 2004
    #3
  4. hari

    Vimal

    Joined:
    Nov 15, 2006
    Messages:
    1
    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.


    -----------------------------------------------------
    Vimal, Nov 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,954
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  2. Replies:
    1
    Views:
    433
    Joshua Flanagan
    Dec 10, 2005
  3. loga123
    Replies:
    2
    Views:
    5,689
    loga123
    Jun 28, 2006
  4. J055
    Replies:
    4
    Views:
    7,899
    Steven Cheng[MSFT]
    Nov 14, 2007
  5. leezard

    System.ArgumentOutOfRangeException

    leezard, Aug 20, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    132
    leezard
    Aug 20, 2003
Loading...

Share This Page