not able to delete multiple records in gridview

Posted by Priti2010 under ASP.NET on 7/23/2012 | Points: 10 | Views : 1023 | Status : [Member] | Replies : 2
protected void btnDel_Click(object sender, EventArgs e)
{

try
{
con.ConnectionString = ConString;
con.Open();
foreach (GridViewRow row in grdvw.Rows)
{
//CheckBox cb = (CheckBox)row.FindControl("chkDel");
CheckBox cb = (CheckBox)grdvw.Rows[row.RowIndex].FindControl("chkDel");
if (cb.Checked)
{
//int id =Convert.ToInt32( grdvw.DataKeys[row.RowIndex]["Emp_no"].ToString);
int Emp_no = Convert.ToInt32(grdvw.DataKeys[row.RowIndex].Values[0].ToString());

cmd = new SqlCommand("Delete from employee where Emp_no=" + Emp_no, con);
cmd.ExecuteNonQuery();
con.Close();
BindData();
lblMsg.Text = "Selected Records Deleted";

}
}

}

catch (Exception ex)
{
Response.Write(ex.Message);
}

}
I am getting an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "
and only single record is deleted

Thanks & Regards
Priti



Responses

Posted by: Ranjeet_8 on: 7/23/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Refer this url
http://www.codeproject.com/Tips/405576/Deleting-Multiple-Rows-in-GridView-in-ASP-NET

Or
Share ur ASPX Page

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

Posted by: Vforvijay on: 7/23/2012 [Member] Starter | Points: 25

Up
0
Down
hi..
Refer the below link to delete the multiple records on gridview..

http://www.dotnetcode.in/2011/06/delete-multiple-recordsrows-in-grid.html

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

Login to post response