Syntax error while using a '  
Author Message
.net sukbir





PostPosted: Visual C# General, Syntax error while using a ' Top

Dear All,

My problem is very simple. First I have a simple query as below

Query1 = "Update tblID " +

"Set value='"+value1"'"+

"Where 'table1'.tableName='table1'";

Query2 = "Insert into tableQueries " +

"Set query='"+Query1 +"''";

The problem here is when I run my query which gives a syntax error due to the symbol ' which I used in Query1. So I tried using \' but it doesnt work too. Can somebody pls help me



Visual C#3  
 
 
Markku Behm





PostPosted: Visual C# General, Syntax error while using a ' Top

Hi

Here is valid strings.

Query1 = "Update tblID " +
"Set value='"+ value1 + "'"+ // Missing + -mark
"Where 'table1'.tableName='table1'";

Query2 = "Insert into tableQueries " +
"Set query='" +Query1 +" ' "; // Only one ' -mark

Yours

Markku


 
 
.net sukbir





PostPosted: Visual C# General, Syntax error while using a ' Top

Dear Markku,

I have tried your method but yet I still get an error in Query 2. The error some this like this it says at 'table1' '. The error is due to the second quote which is actually closing the Query1 in Query2. Do you have any idea how to solve this problem.


 
 
Markku Behm





PostPosted: Visual C# General, Syntax error while using a ' Top

Hi

Sorry. I don't look is that valid SQL syntax. You could try this:

Query2 = "Insert into tableQueries " +

"Set query=\"" +Query1 +" \" ";

Markku


 
 
James Curran





PostPosted: Visual C# General, Syntax error while using a ' Top

C# (and C & C++) use \' to indicate a literal single quote.

SQL uses '' (that's two single quotes)

Quote1 = Quote1.Replace("'", "''");

// Thats Replace paren, double-QUote, single-quote, double-quote, comma,

// double-quote, single-quote, single-quote, double-quote, paren



 
 
.net sukbir





PostPosted: Visual C# General, Syntax error while using a ' Top

Dear Markku,

I have managed to do it but not putting a double quote just a single quote after backslash \'+Query1+\'. Actually what is the reason of the backslash here and why if I do the same in the Query1 it doesnt work. Thanks markku.


 
 
.net sukbir





PostPosted: Visual C# General, Syntax error while using a ' Top

Dear Markku,

I have managed to do it but not putting a double quote just a single quote after backslash \'+Query1+\'. Actually what is the reason of the backslash works here and why if I do the same in the Query1 it doesnt work. Thanks markku.


 
 
.net sukbir





PostPosted: Visual C# General, Syntax error while using a ' Top

Dear James,

So you mean to say that if I want to show as a C# string then I use a \' is it So if I want to use it as sql statement then I should use two single quotes is it. But surprisingly the solution which markku gave with \' also works. I am quite confuse now which I am actually suppose to use for sql. Thanks to you also james.