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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
NainilPosted Mar 18, 2010, 3:49 PM
string userString = textBox1.Text;
string replacedString = userString.Replace("'", "''");
SQL:
replacedString = Replace(userString,"'","''")
Hope this helps.
GustavoPosted Mar 18, 2010, 3:51 PM
Yes, thank you.
GustavoPosted Mar 18, 2010, 3:41 PM
Can you give me a sample code of the 2 types?
NainilPosted Mar 18, 2010, 3:40 PM
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.
GustavoPosted Mar 18, 2010, 3:29 PM
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?
NainilPosted Mar 18, 2010, 3:27 PM
Whenever you have single quotes, replace them with 2 single quotes. That should work.