Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
how to delete database row at single click my code didnt work.

  protected void Delete_Click(object sender, EventArgs e)

{
SqlConnection conn = new     SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
string sqlStatement = "DELETE * userdata";
SqlCommand com = new SqlCommand(sqlStatement, conn);
conn.Close();
           
}
Posted
Comments
Member 10558090 3-Nov-15 9:03am    
DELETE statement require a condition.

For example: DELETE FROM [yourTableName] WHERE youcolumn = youvalue.

If you want to delete all data into your table, without remove columns, you can use TRUNCATE statement.

Well for a start that is not going to delete a row, but every row.

And you're not calling

C#
com.ExecuteNonQuery();


to do the actual execution.
 
Share this answer
 
string sqlStatement = "DELETE * userdata";


you can use

string sqlStatement = "DELETE From tableName";
(or)
string sqlStatement = "DELETE From userdata";
 
Share this answer
 
v2
XML
I guess you want to delete all rows from table so correct the command first as
<pre lang="c#">protected void Delete_Click(object sender, EventArgs e)

{
SqlConnection conn = new     SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
string sqlStatement = "DELETE * from  userdata";
SqlCommand com = new SqlCommand(sqlStatement, conn);
cmd.ExecuteNonQuery();//have to execute above command query
conn.Close();

}</pre>

Reply if any query or mark as solution
 
Share this answer
 
Here[^] a pretty good solution/explanation.
 
Share this answer
 
Use Where condition ....

string sqlStatement = "DELETE from userdata whsre ID_of_the_row=the rows id u need to delete ";
SqlCommand com = new SqlCommand(sqlStatement, conn);
Com.ExecuteNonQuery(); 
 
Share this answer
 
v4
Comments
phil.o 5-Nov-15 7:17am    
Absolutely not. The syntax is DELETE FROM [Table], not DELETE * [Table].
Arasappan 5-Nov-15 7:52am    
Phil by mistake i forget to replace the *
Arasappan 5-Nov-15 7:54am    
dont know y they are downvoting
protected void Delete_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
string sqlStatement = "DELETE * userdata";
SqlCommand com = new SqlCommand(sqlStatement, conn);
com.ExecuteNonQuery();
conn.Close();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900