Solved

how to delete from code behind

Posted on 2006-06-19
3
645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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