Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,
I'm having a very long SQL statement which gets created in code depending on the filter values the user sets.
Unforuntatelly the SQL statement was not logged when the exception accured and now my only thing I can hold on is the error message from System.Data.SqlClient.SqlException: " Incorrect syntax near ' ' ".
I know this is very vague, but probably someone of you has an idea what to check on this exception.
"Error at blank"... Hehe, nice played SQL, I have no idea :P

Thanks, Markus
Posted
Comments
Kornfeld Eliyahu Peter 7-Jan-14 5:24am    
Show us the SQL statement...
NeonMika 7-Jan-14 5:39am    
Sorry, I can't because it's business code...
The "GetQuery"-method is about 500 lines of code and mostly contains of "if the filter is set add following line to the query" like
if (Translator.CurrentLanguageID == LanguageID.DE)
{
query = query.Replace("%%Language%%", "GD_Translation.TranslatedTextDE");
}

Furthermore the problem is that I don't have the exact executed SQL (I don't know which filters were set)

I'm sorry that i cannot post code, probably someone has some basic idea what an errer "near ' '" could point to.
Kornfeld Eliyahu Peter 7-Jan-14 5:44am    
Sorry but don't think someone can help. It seems like you need an expert, who can spot the problem with no or minimum help from the error, but there is nothing to spot...
Karthik_Mahalingam 7-Jan-14 5:26am    
post your code as well.
Richard MacCutchan 7-Jan-14 5:34am    
This is a rather pointless question. You have no code or debug information and yet you expect someone to guess what is wrong with your SQL statement. Go back to the person who forgot to put the logging in the code at this point and show them the error of their ways.

Without the SQL that generated it, we can't even begin to be definitive about a solution.
But...the error message would indicate that you are concatenating strings to form an SQL statement, which would also explain why you can't show it to us.

Don't. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - it will improve your code reliability, readability, and safety, as well as probably curing your problem at the same time.
 
Share this answer
 
Comments
NeonMika 7-Jan-14 5:45am    
Thanks for your help. Yes, it's some legacy code and concatinates strings to generate the query.
I think it's the best way to rebuild the whole query with a parameterized query.
OriginalGriff 7-Jan-14 5:46am    
Definately!
I like my databases, and I want to keep them intact... :laugh:
You can refer this link for escape sequence in sql for single quotes. This is the problem you are facing.

http://stackoverflow.com/questions/1586560/how-do-i-escape-a-single-quote-in-sqlserver[^]
 
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