Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to delete from code behind

Posted on 2006-06-19
3
Medium Priority
?
659 Views
Last Modified: 2008-03-03
How would I write a function to delete from a sql table in the code behind?

Would this work?

protected void DeleteRecordByID(string EMail)
{
String objConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
String strSQL = "Delete from dbname.dbo.tblname where email ='" + EMail + "'";
SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);
DataSet dataSet = new DataSet();
objAdapter.Fill(dataSet, "elist");
}
0
Comment
Question by:fwsteal
  • 2
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16934999
It would but its a bit overkill.

protected void DeleteRecordByID(string EMail)
{
    String objConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
    String strSQL = "Delete from dbname.dbo.tblname where email ='" + EMail + "'";

    SqlCommand cmd = new SqlCommand(strSQL, objConnection);
    objConnection.Open();

    cmd.ExecuteNonQuery();

    cn.Close();
}
0
 

Author Comment

by:fwsteal
ID: 16935231
i got errors on:

SqlCommand cmd = new SqlCommand(strSQL, objConnection);
1. The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments
2. Argument '2': cannot convert from 'string' to 'System.Data.SqlClient.SqlConnection'

objConnection.Open();
'string' does not contain a definition for 'Open'

objConnection.Close();
'string' does not contain a definition for 'Close'

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16935253
oops, didn't read your code properly. It was the "obj" prefix on your connection string that threw me:

protected void DeleteRecordByID(string EMail)
{
    String strConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
    String strSQL = "Delete from dbname.dbo.tblname where email ='" + EMail + "'";

    SqlConnection objConnection = new SqlConnection(strConnection);
    SqlCommand cmd = new SqlCommand(strSQL, objConnection);
    objConnection.Open();

    cmd.ExecuteNonQuery();

    objConnection.Close();
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question