Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

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

    Hi,

    I get this error :

    Code:
    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
    Error is on this line:

    Code:
    string rowID;
    
    rowID = DataGridSpirits.DataKeys[e.Item.ItemIndex].ToString();
    I have a datagrid, i have a delete and update buttons. This error occurs on both of these events. SOmething to do with returning the row number. It worked earlier, i didn't change anything???

    Any help greatly appreciated.

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

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

    Sounds silly but have you set a DataKeyField for the DataGrid?

    If you have could you post the full method that handles the update and also the DataGrid declaration.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

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

    Additionally, make sure your code is only executing on a Row or AlternatingRow and not on the headers or footers.

    Test the value of e.Item.ItemIndex before you attempt to use it
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

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

    Thanks to both of you!

    For some bizarre reason the DataKeyField was deleted from my webForm, probably due to my clumsyness. Anyway all working now!

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width