I have gridview like as follows in asp.net 2010.
When I press delete button of gridview I am getting a error like this
Error : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The code written on that button as follows.
protected void gvBrdDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
int BoardID = Convert.ToInt32(gvBrdDetail.DataKeys[e.RowIndex].Values["ID"].ToString());
// string NationalityName=gvNatDetail.DataKeys[e.RowIndex].Values["NatNam"].ToString();
SqlCommand cmd = new SqlCommand("delete from TbBrdMst where ID=" + BoardID, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
lblSuccess.Text = "Message : Record deleted successfully with Board ID= " + BoardID;
}
catch (Exception ex)
{
lblError.Text = "Error : " + ex.Message;
con.Close();
return;
}
}