0

I have a gridview that pulls data from local SQL server. I chose 3 columns to be displayed on the gridview. I added a fourth column (select command). I would like to get the data from the first column which is an id when I click the select command but i always get an error "An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code Additional information: Index was out of range. Must be non-negative and less than the size of the collection."

Basically I would like to get the id from the first column then assign it to a session variable then redirect to a second page and then use the content of that session variable to populate another textbox.

    protected void grdClients_RowCommand(object sender, GridViewCommandEventArgs e)
{
      string id = grdClients.Rows[grdClients.SelectedIndex].Cells[0].Text.ToString();
      Session["ID"] = id;
      Response.Redirect("secondPage.aspx");
}

Any suggestions?

Thanks

Ali Aziz
  • 1
  • 1
  • Does `grdClients.Rows` return any records? How about `grdClients.Rows[grdClients.SelectedIndex].Cells`? You need to identify at which point it's failing before you can fully trouble shoot it. Though if I had to take a guess I wonder if you're binding the datasource properly. – Marisa Mar 02 '16 at 21:58
  • Yes it does. I will try this – Ali Aziz Mar 03 '16 at 16:15

3 Answers3

0

1- add attribute to the GridView

DataKeyNames="aboutusID"

2- add a templatefield to your columns

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="SelectSession" Text="EDIT"  CommandArgument='<%# DataBinder.Eval(Container,"RowIndex")%>' ></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

3- in your code behind

protected void GridViewAbout_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // this to skip on paging or sorting command
    if (e.CommandName != "Sort" & e.CommandName != "Page")
    {
        // Get the command argument where the index of the clicked button is stored
        int index = Convert.ToInt32(e.CommandArgument);
        // Get the data key of the index where the id is stored
        int id = Convert.ToInt32(GridViewAbout.DataKeys[index].Value);
        if (e.CommandName == "SelectSession")
        {
            // Your Code
        }
    }
}
Mariam
  • 533
  • 2
  • 12
  • 22
0

i think you should use the SelectedIndexChanged event for that.
Then you assign the GridviewRow property to the selected row "GridViewRow row = grdClients.SelectedRow;"

After that you can use row to get the value of the first cell and assign it to your session or where ever you want. "row.Cells[0].Text;"

    protected void grdClients_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grdClients.SelectedRow;
        string id = row.Cells[0].Text;
        Session["ID"] = id;
        Response.Redirect("secondPage.aspx");
    }
Creator
  • 1,502
  • 4
  • 17
  • 30
0

Thank you everyone, I was able to come up with a solution by taking pieces from every solution you provided. Thanks very much.

Below is my solution:

 protected void grdClients_RowCommand(object sender, GridViewCommandEventArgs e)
 {

            if (e.CommandName == "Select")
            {

                int index = Convert.ToInt32(e.CommandArgument);
                string id = grdClients.Rows[index].Cells[0].Text.ToString();
                Session["ID"] = id;
                Response.Redirect("secondForm.aspx");
            }
}
Ali Aziz
  • 1
  • 1