Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlStr = " Delete from FilelibF where IdAttachBord = + GridFile.SelectedRows[0].Cells[0].Value + ";
sqlcmd = new SqlCommand(SqlStr, cn);
sqlcmd.ExecuteNonQuery();



when i want to delete some file in my program it says : Incorrect syntax near '0'. what is that mean and how to fix it ?!!
Posted
Updated 26-Dec-15 2:47am
v4

1 solution

The GridFile.SelectedRows[0].Cells[0].Value part is inside the quotes, so it gets treated as a part of the query. You have to close the quotes before the + operator.

However, string concatenation for SQL queries is a bad idea: you're vulnerable for SQL injection[^]. Use parameterized queries instead: they fix the vulnerability and make your query more readable.
C#
SqlStr = "Delete from FilelibF where IdAttachBord = @SelectedCell";
sqlcmd = new SqlCommand(SqlStr, cn);
sqlcmd.Parameters.Add(new SqlParameter("SelectedCell", GridFile.SelectedRows[0].Cells[0].Value));
sqlcmd.ExecuteNonQuery();
 
Share this answer
 
Comments
brandon1999 26-Dec-15 9:00am    
ok, thank u so much.
Thomas Daniels 26-Dec-15 9:01am    
You're welcome!
brandon1999 26-Dec-15 9:02am    
can u fix this too please

http://www.codeproject.com/Questions/1067194/how-to-fix-this-error
Thomas Daniels 26-Dec-15 9:11am    
I've looked at it but I'm not sure... I would know how to convert a string to a byte array, but perhaps that isn't the best solution: you appear to need a byte array, so why store it as a string in the database? However, I don't work with databases very much, so I wouldn't be able to give a detailed explanation on that. And perhaps there is even another, better, solution that I'm not aware of.
brandon1999 26-Dec-15 9:35am    
thanks i've been fixed it.

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