how to delete a row in grid view by setting auto genarate delete button is true

May 26 2010 11:07 AM
hi

   i am trying to delete a  row in grid view by using c# language.
but it is displaying an error like
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

can
any one please suggest me where i did mistake.
i am sending the total code

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;user id=sa;password=sri;database=nalluri");
    protected void Page_Load(object sender, EventArgs e)
    {
     
        SqlDataAdapter da = new SqlDataAdapter("select* from emp", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "empdetails");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        SqlCommand cmd = new SqlCommand("delete emp where eno='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'",con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
       
    }
}

Answers (2)