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.
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.
Sorry replace the single quote with two single quotes:
firstchars.Replace("'", "''" ).Trim();
firstchars.Replace("'", "''" ).Trim();
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi sendmeblah ,
does the code firstchars= firstchars.Replace("'", "\"" ).Trim(); replaces single quotes with \"
or with only "
Thanks.
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(firstcha rs);
and in the Output window you will see:
This string " has a quote
firstchar = "This string ' has a quote";
firstchars= firstchars.Replace("'", "\"" ).Trim();
Console.WriteLine(firstcha
and in the Output window you will see:
This string " has a quote
ASKER
Yes got it
Thank You.
Thank You.
string sSqlCommand = "insert into table values('" + firstchars + "')";