Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

how to delete from code behind

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");
}
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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();
}
Avatar of fwsteal
fwsteal

ASKER

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'

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial