• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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.
0
pradeepy
Asked:
pradeepy
  • 4
  • 3
1 Solution
 
sendmeblahCommented:
I think you need this as your command string:

string sSqlCommand = "insert into table values('" + firstchars + "')";
0
 
sendmeblahCommented:
Sorry replace the single quote with two single quotes:
firstchars.Replace("'",  "''" ).Trim();
0
 
pradeepyAuthor Commented:
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sendmeblahCommented:
Ok, third times a charm:
Place a backslash before the double quote
firstchars= firstchars.Replace("'",  "\"" ).Trim();
0
 
pradeepyAuthor Commented:
Hi sendmeblah ,

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

Thanks.
0
 
sendmeblahCommented:
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
0
 
pradeepyAuthor Commented:
Yes got it

Thank You.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now