Click here to Skip to main content
15,891,739 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Here is my code:
SQL
SqlCommand cmda = new SqlCommand("Delete ID,DrugName,PurchasePrice,SellPrice,Stock from AddProduct where DrugName ='"+ comboBox1.SelectedItem +"' ",conn);
            cmda.Connection = conn;
            conn.Open();
            cmda.ExecuteNonQuery();
            conn.Close();

I click the delete button so error is occur. And Error is (Incorrect syntax near ',').

Please Help What shoulid i do then error is not occur?
Posted
Updated 17-Aug-12 15:07pm
v2
Comments
AspDotNetDev 17-Aug-12 21:11pm    
Your code is vulnerable to SQL injection: http://xkcd.com/327/ . Use parameterized queries instead: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx .
AspDotNetDev 19-Aug-12 0:07am    
Ignore this comment. I am just testing if I get a notification when I reply to my own comment.
Arsalaan Ahmed 17-Aug-12 21:15pm    
Thank u Problem Solved,,, Ap ke btane se phele,,,,
RaisKazi 17-Aug-12 21:21pm    
Please do not use native language and be respectful to the forum which offering help to you.
Arsalaan Ahmed 13-Jul-13 5:59am    
bhai kia negative language use ki hai... sirf yehi to kaha hai ke ap ke btanae se phele mera kam ho gya..
to is me mene kia galat bat ki hai

Remove colunm names from your "Delete" query. Using "Delete" statement you delete entire row of corresponding records, so it does not require column names.

Have a look at below link for proper syntax of "Delete" statement.
http://www.w3schools.com/sql/sql_delete.asp

I would recommend you to use parameterized queries instead of concatenating user-input to your plain sql query.

http://www.csharp-station.com/Tutorial/AdoDotNet/lesson06
 
Share this answer
 
v2
You can't delete the values in specific rows of a table. You are deleting the entire row (if you want to erase specific fields, you'd need to do an UPDATE). Do this instead:
C#
SqlCommand cmd = new SqlCommand("DELETE FROM AddProduct WHERE DrugName = @DrugName", conn);
cmd.Parameters.AddWithValue("DrugName", comboBox1.SelectedItem);

Here are some other issues with your code:

  • When you don't use parameterized queries, you are opening your code up to SQL injection (that's bad).
  • You don't need to set the connection on the command if you pass the connection to the command via the constructor.
 
Share this answer
 
v2

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