Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       {
           int id = Convert.ToInt16(GridView1.DataKeys[e.NewEditIndex].Values[0].ToString());

           cnn.Open();
           SqlCommand cmd = new SqlCommand("delete from call_details where id=" + id, cnn);
           int result = cmd.ExecuteNonQuery();
           cnn.Close();

           GridView1.DataBind();


this code will give me error like:Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

i have not use template field.then what is solution.
Posted
Updated 31-Dec-12 0:02am
v2
Comments
Zafar Sultan 31-Dec-12 6:21am    
You are deleting a record inside RowEditing event!

//Please Try this code for your help....

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

GridView1.EditIndex = e.NewEditIndex;

//yourControlName it'sInstance =
//(yourControlName)//(GridView1.Rows[GridView1.EditIndex].FindControl("yourContr//olId"));

//For Example

Label lbl = (Label)(GridView1.Rows[GridView1.EditIndex].FindControl("lblId"));

int id = convert.ToInt16(lbl.Text);

cnn.Open();
SqlCommand cmd = new SqlCommand("delete from call_details where id=" + id, cnn);
int result = cmd.ExecuteNonQuery();
cnn.Close();

GridView1.DataBind();
C#

 
Share this answer
 
1. Add a button inside itemtemplate.
2. Use CommandArgument to pass the id of record to be deleted.
3. Add RowCommand event for the gridview.
4. In RowCommand event delete the record.
5. Re-Bind the gridview.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900