I am receiving the error "Input string was not in a correct format" on the line:
int index = Convert.ToInt32(e.CommandArgument);
My goal is to get the ID (both letters and numbers) from my Gridview (Cell 1 of the selected row), for processing the records in the DB associated with that ID.
Most of the pages that I found said to use the above code to find the row number for gridview.
My aspx page:
<asp:gridview ID="gridview1" runat="server" DataKeyNames="ID" AutoGenerateColumns="false" OnRowCommand="gridview1_RowCommand">
<columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn_select" runat="server" Text="Select" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Record_ID" DataField="ID" />
<asp:BoundField HeaderText="Record_Date" DataField="Date" />
</Columns>
</asp:gridview>
My Code Behind:
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
//code stuff
final();
}
protected void final()
{
//code stuff for populating the gridview. Gridview populates as expected.
}
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);//where I receive the error.
string id = gridview1.Rows[index].Cells[1].Text;
using (SqlConnection connection string)
{
try
{
cmd = new SqlCommand("DBSP", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Char).Value = id;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
final();
}
catch (Exception ex)
{
//code stuff
}
}
}
}
A couple of other things I have tried are from:
Accessing GridView Cells Value
How to get cell value in GridView (WITHOUT using cell index)
Get the cell value of a GridView row
Getting value from a Gridview cell
as well as others.