Solved

how to delete from code behind

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now