Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnDel_Click(object sender, EventArgs e)
   {

       try
       {
           con.ConnectionString = ConString;
           con.Open();
           foreach (GridViewRow row in grdvw.Rows)
           {
               //CheckBox cb = (CheckBox)row.FindControl("chkDel");
               CheckBox cb = (CheckBox)grdvw.Rows[row.RowIndex].FindControl("chkDel");
               if (cb.Checked)
               {
                   //int id =Convert.ToInt32( grdvw.DataKeys[row.RowIndex]["Emp_no"].ToString);
                   int Emp_no = Convert.ToInt32(grdvw.DataKeys[row.RowIndex].Values[0].ToString());

                   cmd = new SqlCommand("Delete from employee where Emp_no=" + Emp_no, con);
                   cmd.ExecuteNonQuery();
                   con.Close();
                   BindData();
                   lblMsg.Text = "Selected Records Deleted";

               }
           }

       }

       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }

   }

I am getting an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "
and only single record is deleted
Posted
Updated 22-Jul-12 20:22pm
v2
Comments
dimpledevani 23-Jul-12 2:23am    
what does the method binddata() do??

Try this:
C#
int Emp_no = Convert.ToInt32(grdvw.DataKeys[row.RowIndex].Value);

Instead of
C#
int Emp_no = Convert.ToInt32(grdvw.DataKeys[row.RowIndex].Values[0].ToString());


Refer:
Delete rows from datgridview and database access[^]
 
Share this answer
 
Comments
priti2010 23-Jul-12 2:35am    
Now i am getting this error

Unable to cast object of type 'System.Collections.Specialized.OrderedDictionary' to type 'System.IConvertible'.
Prasad_Kulkarni 23-Jul-12 2:39am    
Try this:
int Emp_no = int.Parse(grdvw.CurrentCell.RowIndex.ToString());
string Emp_noString = grdvw[0, currentRow].Value.ToString();
Emp_noInt = int.Parse(Emp_noString );
Hi,
Try this:
C#
con.ConnectionString = ConString;
con.Open();
foreach (GridViewRow row in grdvw.Rows)
{
   //CheckBox cb = (CheckBox)row.FindControl("chkDel");
   CheckBox cb = (CheckBox)grdvw.Rows[row.RowIndex].FindControl("chkDel");
   if (cb.Checked)
   {
        int Emp_no = int.Parse(GridView1.DataKeys[row.RowIndex].Value.ToString());
        cmd = new SqlCommand("Delete from employee where Emp_no='" + Emp_no + "'", con);
        cmd.ExecuteNonQuery();
        //con.Close(); this should not come here.
        BindData();
        lblMsg.Text = "Selected Records Deleted";
    }
}
con.Close();


--Amit
 
Share this answer
 
v4
C#
con.ConnectionString = ConString;
           con.Open();

You have open the connnection before foreach loop and you are closing the connection after deleting one record.

Open the connection in same foreach loop
 
Share this answer
 
Check that you have provided the field in DataKeyNames property of GridView
 
Share this answer
 

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