locked
Error while deleting record from gridview RRS feed

  • Question

  • I have gridview structure as follows.

    The code written on delete 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;
                }
            }

    But when i press delete button I get the error as follows.

    Error : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

    What is the problem? I can't understand. Plz help on this topic.


    nravhad

    • Moved by Max Meng Tuesday, April 16, 2013 1:48 AM
    Sunday, April 14, 2013 5:04 AM

Answers