Click here to Skip to main content
12,695,365 members (26,808 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
Index was out of range. Must be non-negative and less than the size of the collection.
I'm trying to get the id of gridwiew row and use the link button to print the row info in another page and it keeps give me this error ,
any help please

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
  <asp:BoundField DataField="emp_fname" HeaderText="emp_fname"
      SortExpression="emp_fname" />
  <asp:BoundField DataField="emp_lname" HeaderText="emp_lname"
      SortExpression="emp_lname" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:dbConnectionString1 %>"
    SelectCommand="SELECT [emp_fname], [emp_lname] FROM [employee] WHERE ([com_id] = @com_id)">
    <asp:SessionParameter Name="com_id" SessionField="comid" Type="Int32" />


protected void LinkButton1_Click(object sender, EventArgs e)

    LinkButton lnkbtn = sender as LinkButton;
    //getting particular row linkbutton
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
    //getting email_id of particular row
    int email_id = Convert.ToInt32(GridView1.DataKeys[0].Value.ToString());
    string email_from = gvrow.Cells[0].Text;
    Response.Redirect("~/readmsg.aspx?email_id=" + email_id);
Posted 15-Jun-12 7:58am
Updated 15-Jun-12 8:20am
Tim Corey115.4K
SAKryukov 15-Jun-12 13:31pm
In what line?
Leena00 15-Jun-12 13:52pm
In this line

int email_id = Convert.ToInt32(GridView1.DataKeys[0].Value.ToString());

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The exception that is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array.[^]
It seems your grid has no DataKeys[^]. Are you sure your GridView[^] is filled with records? Because that doesn't seem to be the case.
Anyway, simply debugging and stepping through the code should reveal if GridView.DataKeys[0] does actually exist.

You should always use the Count or Length (extension) method or Property of collections to make sure there are enough objects in the collections before trying to access one.
Consider the following:
if (GridView.DataKeys.Count > 0)
    // Your code accessing the 1st element in the collection goes here.
    // Remember that DataKeys[0] returns the first element (count >= 1).

    // This is safe, since Count is bigger than 0, so there is at least one item. 
    // Might still throw an exception, since you're not sure if count = 2.
Hope that helps :)
Leena00 15-Jun-12 14:46pm
That's it... Thanks a lot
Naerling 15-Jun-12 15:49pm
Glad it helped. I have edited my solution for a little more detail and how to work around this problem :)
Tim Corey 15-Jun-12 15:02pm
It is also good to note that you should always check for a the existance of a row before trying to access it. Assuming you have data in a GridView or other data container is not a good idea. Trust but verify.
Naerling 15-Jun-12 15:44pm
Indeed, use the Count or Length (extension) method or Property of collections to make sure there are enough objects in the collections before trying to access one.
I will update my answer with this :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 16 Aug 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100