Click here to Skip to main content
12,815,616 members (41,279 online)
Rate this:
 
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
Updated 31-Dec-12 1:02am
v2
Comments
Zafar Sultan 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2017
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