Find Topics

Saturday, January 21, 2012

How to Handle single quote in sql Insert statement in C#

0 comments

when you insert a ' (single quote) in sql server you will get an exception so if we have some text which have single quote and we want to insert it in that case we need to use replace method in c# by which we can replace ' (single quote). other benefits are you are more secure from sql injection attacks

You can use method like this.
suppose you enter a value with single quote in text box then use the code like this

string name = txtname.Text.Replace("'", "''");

//first param is a single quote within double quotes. second param is two single quotes within double quotes.

//Also, if this application needs security you should be aware of 'SQL Injection'


Do you like this article it Take 5 Second to Share with your Friends

Subscribe Email alerts to receive more Tech Updates to your Inbox!

Not Getting? Feel free to ask

Please Leave Comments your Comments are always Helpful. I will try to reply to your queries as soon as time allows.

Regards,
Nishant Srivastava

No Facebook Account! No worries you can add Comments Here!

Leave A Reply