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 0:00am
Edited 31-Dec-12 0: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 OriginalGriff 500
1 Prakriti Goyal 292
2 Yogesh Kumar Tyagi 224
3 Maciej Los 195
4 CPallini 170
0 OriginalGriff 6,652
1 Sergey Alexandrovich Kryukov 5,449
2 Maciej Los 3,474
3 Peter Leow 3,303
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
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