AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Gridview with checkbox
Posted by boda.raju on  6/18/2008 8:39:27 AM Category: ADO.NET
Add to my favorites
Email to friend
i am placed checkbox in gridview through source code ,and take delete button outside if i click on checkbox then press delete button that row delete in database, how is it possible ?
(plz send ans:)
delete code:
foreach (GridViewRow row in GridView1.Rows)
{

CheckBox cb = (CheckBox)row.FindControl("cb");
if (cb .Checked )
{
//how to write delete code (plz write wht iam write, iam write delte code but it error)
}

Post Your Reply Post New Question

 Viewer's Reply
Delete Code is Here
Answered By Moderator1 on 6/20/2008 8:46:00 PM
foreach (GridViewRow row in GridView1.Rows)
{

CheckBox cb = (CheckBox)row.FindControl("cb");
if (cb .Checked )
{
string sql="Delete from yourtable where id='"& row.cells[0].Text &"'";
SqlCommand cmd=new SqlCommand(sql,connobject);
cmd.ExecuteNonQuery();
}
}

this above code inside the block is only a sample code to guide you. Same like that you do the real one.
Thanks.
 
Aishwary Give You Correct Answer
Answered By Aishwary Singh on 1/12/2009 1:33:17 AM
foreach(GridViewRow row in GridView1.Rows)
{
CheckBox chk=(CheckBox)row.FindControl("chkSelect");
int ID=Convert.ToInt32(GridView1.DataKeys[row.DataItemIndex].Value);
SqlConnection con=new SqlConnection("Server=;DataBase=;Trusted_Connection=yes");
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.Parameters.Add("@ID",SqlDbType.Int).Value=ID;
cmd.CommandText="DELETE FROM Table Where ID=@ID";
cmd.connection.Open();
cmd.ExecuteNonQuery();
cmd.connection.Close();
}
 
Aishwary Give You Correct Answer
Answered By Aishwary Singh on 1/12/2009 1:35:07 AM
foreach(GridViewRow row in GridView1.Rows)
{
CheckBox chk=(CheckBox)row.FindControl("chkSelect");
if(chk.Checked==True)
{
int ID=Convert.ToInt32(GridView1.DataKeys[row.DataItemIndex].Value);
SqlConnection con=new SqlConnection("Server=;DataBase=;Trusted_Connection=yes");
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.Parameters.Add("@ID",SqlDbType.Int).Value=ID;
cmd.CommandText="DELETE FROM Table Where ID=@ID";
cmd.connection.Open();
cmd.ExecuteNonQuery();
cmd.connection.Close();
}
}
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Introduction to Software Development