Solved

how to delete from code behind

Posted on 2006-06-19
3
641 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 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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