Deleting from an SQL database
-
Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys
-
Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys
Hi, replace your String Query to one given here. string query = "DELETE FROM tblUsers WHERE User_name = '" + txtDelete.Text + "'"; I this should work now. Also, Just check your connection is open before you assign it to myCommand. Thank you.
-
Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys
I case you can't see what's different in DotNetD's answer, he replaced the double quotes surrounding the textbox variable with single quotes. I strongly recommend that you check out this[^] article by Colin Angus Mackay before someone types ';truncate table tblusers; in your textbox and destroys your database. If you use parameters rather than direct text concatenation, you will avoid both the problem you are having now, and the vulnerability to sql injection attacks in your app. We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene -- modified at 10:07 Sunday 7th May, 2006
-
Hi, replace your String Query to one given here. string query = "DELETE FROM tblUsers WHERE User_name = '" + txtDelete.Text + "'"; I this should work now. Also, Just check your connection is open before you assign it to myCommand. Thank you.
Ohh thanks... I arranged the quotes and it worked out Thanks a lot!!