Hi friends,
I am working on checkbox in GRIDVIEW. I want to delete all the selected records... I have this code below for your reference... Please help me to make it work ... Thanks....
protected void btnDelete_Click(object sender, EventArgs e)
{
string currentrow;
for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
{
CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
if (cb.Checked)
{
currentrow = GridView1.DataKeys[index].Value.ToString();
string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated Security=True";
string query = @"Delete FROM Categories where CategoryID='" + currentrow + "'";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query);
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
GridView1.DataSource = ds.Tables["Categories"];
GridView1.DataBind();
}
}
}
Loading
Sam HobbsPosted May 2, 2011, 12:54 AM
In other forums, a thread that says "urgent" in the subject might get less help since people that volunteer to help might want to avoid threads like that. Saying urgent implies that the person is in too much of a hurry to spend the time they need to spend to solve the problem.
Savita JoshiPosted May 1, 2011, 12:32 PM
Sorry for that!!!!!! Will take care of the rules next time.
Mahesh ChandPosted May 1, 2011, 9:50 AM
You need to make sure your title of the question is proper. Please help, urgent title does not make sense. To me, I have no idea unless I read your post.
Most of the questions are usually urgent here and that is why people post them here.
Thanks for playing rules!
Savita JoshiPosted May 1, 2011, 9:09 AM
Thanks a lot for the explanation...:)
Ravishankar SinghPosted May 1, 2011, 8:04 AM
Sorry for late reply,But Vupes already answered your question. :)
VulpesPosted May 1, 2011, 7:51 AM
Savita JoshiPosted May 1, 2011, 7:34 AM
Savita JoshiPosted May 1, 2011, 7:05 AM
It is the same code ie the link provided by you.. I thought to test that and accordingly modify in my page but the code in the link you have provided is not deleting the selected records... If that works i can use that where i want...
Ravishankar SinghPosted May 1, 2011, 6:54 AM
Call gridview bind outside forloop and after for loop. And I suggest you to debug you code and see the flow step by step using F11. You get the point where your method not exactly working. Could you please show all methods that you used? I mean your code.
Savita JoshiPosted May 1, 2011, 6:47 AM
I have called that method in the click event of button... Why are the selected rows not getting deleted?
Ravishankar SinghPosted May 1, 2011, 6:43 AM
Call to rebind gridview with database on btnDelete_Click() method.It will fetch the item in gridview from database(You wont get deleted data).
Please let me know if its not working. :)
Savita JoshiPosted May 1, 2011, 6:34 AM
Now its not giving error but selected rows are not getting deleted... The link u provided,i copied that code and checked in another page...
Ravishankar SinghPosted May 1, 2011, 6:16 AM
Debug your solution by using breakpoint in each steps.
Ine more thing that i forgot to see in your code is that you have not used con.open() to enter in database.Hope this you will modify.
Also see this sample:
SqlCommand cmd1 = new SqlCommand(strInsert, objConn);
objConn.Open();
cmd1.ExecuteNonQuery();
objConn.Close();
Change this as...
SqlCommand cmd1 = new SqlCommand()
cmd1.Connection = objConn;
cmd1.CommandText = strDelete;
objConn.Open();
cmd1.ExecuteNonQuery();
objConn.Close();
If you see, these two scenarios both are same. What i am thinking is your query is having wrong sql.
While debugging just check strDelete, take that value and try to execute the sql itself in database.
Ravishankar SinghPosted May 1, 2011, 6:09 AM
MSDN is already having the solution for this error:
here you can see the solution of error.
http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/d6a74623-89b0-4b9a-ad07-4b8e1a79015c/
I guess there is some problem with database execution process. Rest is fine. Check this one or otherwise post you full code that you changed. I will modify and send it back to you.Hope you will get the idea :)
Savita JoshiPosted May 1, 2011, 5:40 AM
I tried ur suggestion but it did not work.
@Ravishankar,
I used that code,changed my code to the code in the link u have given but it is giving this error now:
ExecuteNonQuery: CommandText property has not been initialized
Ravishankar SinghPosted May 1, 2011, 4:56 AM
You are almost right. Just verify your code with this link. This almost similar to your code. Let me know if you need help in understanding the linked code. :)
Note:Don't forget to Bind your GridView with data after you call the delete method to reflect the changes in the GridView.
VulpesPosted May 1, 2011, 4:35 AM
Savita JoshiPosted May 1, 2011, 3:15 AM
As i told u earlier,i am getting this error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Kirtan PatelPosted May 1, 2011, 3:00 AM
foreach(GridViewRow r in DataGridView1.Rows)
{
int rowindex = r.RowIndex ;
CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
if (cb.Checked)
{
currentrow = GridView1.DataKeys[index].Value.ToString();
string constr = "Data Source=OM;Initial Catalog=TestDB;Integrated Security=True";
string query = "Delete FROM Categories where CategoryID='" + currentrow + "'";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query,con);
cmd.ExecuteNonQuery();
/* Rebind your GridView */
SqlCommand comm = new SqlCommand("select * from Categories", con);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Savita JoshiPosted May 1, 2011, 2:34 AM
Its still not working..
Savita JoshiPosted May 1, 2011, 1:02 AM
I am also getting this error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Kirtan PatelPosted May 1, 2011, 12:38 AM
{
string currentrow;
for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
{
CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
if (cb.Checked)
{
currentrow = GridView1.DataKeys[index].Value.ToString();
string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated Security=True";
string query = @"Delete FROM Categories where CategoryID='" + currentrow + "'";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query,con);
cmd.ExecuteNonQuery();
/* Rebind your GridView */
SqlCommand comm = new SqlCommand("select * from Categories", con);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}