Nilesh Avhad

Nilesh Avhad

  • NA
  • 600
  • 306.3k

Error while deleting record from gridview

Apr 14 2013 12:58 AM
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;
            }
        }

Answers (2)