Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.NET
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
  <asp:BoundField DataField="emp_fname" HeaderText="emp_fname"
      SortExpression="emp_fname" />
  <asp:BoundField DataField="emp_lname" HeaderText="emp_lname"
      SortExpression="emp_lname" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:dbConnectionString1 %>"
    SelectCommand="SELECT [emp_fname], [emp_lname] FROM [employee] WHERE ([com_id] = @com_id)">
<SelectParameters>
    <asp:SessionParameter Name="com_id" SessionField="comid" Type="Int32" />
</SelectParameters>

</asp:SqlDataSource>

C#
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);
    con.Close();
}
Posted
Updated 28-Jun-20 4:41am
v2
Comments
Sergey Alexandrovich Kryukov 15-Jun-12 13:31pm    
In what line?
--SA
Leena00 15-Jun-12 13:52pm    
In this line

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

1 solution

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.

Edit:
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:
C#
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. 
    GridView.DataKeys[0].Value.ToString()
    // Might still throw an exception, since you're not sure if count = 2.
    GridView.DataKeys[1].Value.ToString()
}
Hope that helps :)
 
Share this answer
 
v2
Comments
Leena00 15-Jun-12 14:46pm    
That's it... Thanks a lot
Sander Rossel 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.
Sander Rossel 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900