I have a table in my sql server 2008 database
COMMENT(idcom,content,username);
This table has 2 records which username = lecturer
Now I want to delete all comments which have username= lecturer:
First, I test the query in SQL Server:
DELETE FROM COMMENT WHERE USERNAME='lecturer' -> it works fine: 2 records were deleted.
Then I applied that query in my c# code :
public bool delete(string userdeleted)
{
string sql="DELETE FROM COMMENT WHERE USERNAME="+userdeleted; //userdeleted= lecturer
try
{
SqlConnection sqlconnection = new SqlConnection();
SqlCommand sqlcommand = new SqlCommand();
sqlconnection.ConnectionString = connstring;
sqlconnection.Open();
sqlcommand.Connection = sqlconnection;
sqlcommand.CommandType = CommandType.Text;
sqlcommand.CommandText = sql;
sqlcommand.ExecuteNonQuery();
sqlconnection.Close();
sqlcommand.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
When I built that code, program jump inside catch statement and return false???.
Try to debug, it has an error: $exception:{"Invalid column name 'lecturer'."} at line sqlcommand.ExecuteNonQuery();.
Help!!! Why the code doesnt work even it works fine in SQL SERVER???
usingfor theSqlConnectionandSqlCommandobjects. ie,using (var sqlConnection = new ...)