Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 How to write Delete multiple items with checkbox

Author  Topic 

krishnet
Starting Member

29 Posts

Posted - 2007-05-16 : 06:15:44
Hello frdz,

I want to delete the multiple items from the gridview with the help of the checkbox.
I have the linkbutton for the delete in the gridview which performs the delete operation getting data with the sqldatasource from sqlserver 2005

This is the asp.net C# 2005 code which i have done for the above:

private void DeleteSelectedCompany()
{
string id = "";
bool chkBox = false;

foreach (GridViewRow row in CompanyGridView.Rows)
{

CheckBox deleteChkBxItem = (CheckBox)row.FindControl("check1");
if (deleteChkBxItem.Checked)
{
chkBox = true;
string companyid = CompanyGridView.DataKeys[row.RowIndex].Value.ToString();
DELETE from company "WHERE id = '" + id + "'" ;

}
}

}

Pls can anyone rectify what's wrong in the coding.
Is the delete query written for deleting the multiple items is correct ?
It is giving me the error over there.


I want to also know where to call this function and how ??

Thanxs in advance for ur help if provided.

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-05-16 : 08:13:56
??? does that code even compile? You have to create a database connection and a sqlcommand and execute it to do the delete, you can't just embed SQL code into your C# code, they are two completely different things.

A good tutorial on interacting with sql server from C# using ADO.NET is here, you should really go through the whole thing:

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx

and also, never concatenate an "ID" to a sql string like that; always use parameters:

http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx




- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-05-16 : 10:44:31
Thanxs for ur suggestion regarding the connection to database i m retrieving the data thru sqldatasource which does not requires any sqlcommand i think my dear frd.
I m not using objectdatasource.

If at all i m wrong then i will see as u said.
But,tell me that sqldatasource needs any connection it requires only the connectionstring to connect to database as i know.

So,if i m not wrong then pls rectify and tell what's missing and how to write the query of delete using sqldatasource.
The simple delete query is running with the help of sqldatasource but can't delete the multiple items with the help of the checkbox.
Go to Top of Page
   

- Advertisement -