Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
 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 31-Dec-12 1:00am
Edited 31-Dec-12 1:02am
v2
Comments
Zafar Sultan at 31-Dec-12 6:21am
   
You are deleting a record inside RowEditing event!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

//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();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,455
1 OriginalGriff 8,295
2 Peter Leow 4,839
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100