Link to home
Start Free TrialLog in
Avatar of pradeepy
pradeepy

asked on

Replacing singlequotes with double quotes

Hi,
 i have an SQL insert statement which inserts a string into the database. However my string contains single quotes. Iam trying to replace the single quotes with double quotes for proper insertion into sql server.

 firstchars= firstchars.Replace("'",  " " " ).Trim();

insert into table values(firstchars);

Iam getting an error as new line constant expected.

Can anyone tell me what the correct code is??

Any furthur comments and suggestions are welcome.

Thanks in advance.
Avatar of sendmeblah
sendmeblah

I think you need this as your command string:

string sSqlCommand = "insert into table values('" + firstchars + "')";
Sorry replace the single quote with two single quotes:
firstchars.Replace("'",  "''" ).Trim();
Avatar of pradeepy

ASKER

yes sendmeblah ,
 iam using same syntax but my firstchars contain single quotes. So iam trying to replace single quotes with double quotes.

Can you give me the correct syntax of enclosing a double quotes within a string.

Thanks.

ASKER CERTIFIED SOLUTION
Avatar of sendmeblah
sendmeblah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi sendmeblah ,

does the code firstchars= firstchars.Replace("'",  "\"" ).Trim(); replaces single quotes with \"
or with only "

Thanks.
The \" is an escape character that means place a literal double quote.  Try this:

firstchar = "This string ' has a quote";
firstchars= firstchars.Replace("'",  "\"" ).Trim();

Console.WriteLine(firstchars);

and in the Output window you will see:
This string " has a quote
Yes got it

Thank You.