how to delete the records in gridview

Posted by Kishore22 under ASP.NET on 9/20/2013 | Points: 10 | Views : 821 | Status : [Member] | Replies : 4
protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());


SqlCommand cmd2 = new SqlCommand("delete from tab where id=" +ID, con);
cmd2.ExecuteNonQuery();
connect();
}



it is not workin



protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());


SqlCommand cmd2 = new SqlCommand("delete from tab where id=" +ID, con);
cmd2.ExecuteNonQuery();
connect();
}




Responses

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

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

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Why do you have connect(); after delete operation?
I think Something you are missing control over the code....
Better to put break point at GridView1_RowDeleting1() event and then press F5
then press F10 for debugging line by line...
Check whether control goes to the above function or not?
And also check the value of ID after "int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString()); " statement execution

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://forums.asp.net/t/1448793.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Workaround:
1) Another work we can do is changing CommandName property of "Delete" button to anything other than "Delete" and handle it in RowCommand event, "Delete" command is default CommandName for firing RowDeleting event of GridView control.

Refer this link once
http://stackoverflow.com/questions/13225986/asp-net-error-in-deleting-record-from-gridview

2) Do you have DataKey name in the GridView Declaration?
You haven't defined any datakeynames for the GridView "GridView1" and your trying to get it.
Since GridView1.DataKeys collection is empty and you are trying to get a value from it therefore you are getting this error
http://stackoverflow.com/questions/2306742/index-was-out-of-range-must-be-non-negative-and-less-than-the-size-of-the-colle

3) Do you have PostbackUrl for Edit/Delete linkbutton?
http://forums.asp.net/t/1626128.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response