Hello:
I have a sql command to execute but some times there is a single quote in the textBox and it messes me up. How can I allow to save the single quote to the sql table?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nainil148 reputationPosted Mar 18, 2010, 11:49 AM EDT
string userString = textBox1.Text;
string replacedString = userString.Replace("'", "''");
SQL:
replacedString = Replace(userString,"'","''")
Hope this helps.
Nainil148 reputationPosted Mar 18, 2010, 11:27 AM EDT
Whenever you have single quotes, replace them with 2 single quotes. That should work.
GustavoRank #1,990 · 732 reputationPosted Mar 18, 2010, 11:29 AM EDT
Yes, if it was a string. But it will be some text that the user entered. Is there a way to do it automatically when I put it into the sql command to save it?
Nainil148 reputationPosted Mar 18, 2010, 11:40 AM EDT
You can either use .NET string.Replace() method to replace any single quotes with 2 single qoutes or you can use T-SQL's Replace function. Since the value being inserted is passed in as a SqlParameter.Value property, you can run the Replace function through it and ensure that any string being passed to the stored procedure is clean. Hope this helps.
GustavoRank #1,990 · 732 reputationPosted Mar 18, 2010, 11:41 AM EDT
Can you give me a sample code of the 2 types?
GustavoRank #1,990 · 732 reputationPosted Mar 18, 2010, 11:51 AM EDT
Yes, thank you.