Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

Currently i have one form name Add Category where i have one field name : Add Category_textbox: where i insert alpha characters (i.e. a-z OR A-Z).All the alpha character successfully stores in database. but when i try with another character apostrophe (') to be inserted in database.example: O'Really then the message comes like this:


Incorrect syntax near 'really'.
unclosed quotation mark after the character string".


i am using sql server as database.

plz help me out
Posted

The single quote is its own escape character. So you would use 2 in a
string, to signify to treat it as 1 literal single quote.
But instead I suggest you to use parameterized queries to avoid this problem.
You should always use it to prevent SQL injection
attacks that is the most important reason for it.

The solution to your problem is in this link[^].

But check out these links as well;
SQL injection[^]
Using SQL Escape Sequences[^]
how does one escape special characters when writing sql queries[^]
 
Share this answer
 
v2
You can create an insert query with defining parameters in sqlcommand:
SQLCommand cmd = new SQLCommand("Insert into Category(id, name) values(@id, @name)" , con);


Where con is the SQLConnection object.

You can define parameters as follows:
SQLParamter param1 = new SQLParamter("@id","1");
SQLParamter param2 = new SQLParamter("@name","O'Really");


Add the parameters in above SQLCommand:
cmd.Parameters.add(param1);
cmd.Parameters.add(param2);


Execute the command:
cmd.ExecuteNonQuery();


Hope this will help you.
 
Share this answer
 
If you are inserting a single quote into a sql server database, you need to pair it with a second single quote to escape it (only one single quote will be stored). So if you are trying to insert "string'", you need to change it to "string''".
 
Share this answer
 
You could try 'double single-quoting' :)

Try this (notice the double single-quote):

INERT INTO YourTable(Column1) VALUES(O''Really)

Hope it helps.

http://stackoverflow.com/questions/2029307/t-sql-escape-quote-character[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900