Click here to Skip to main content
15,920,664 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
See more:
Hi frds,
I have problem while save the string with quotation mark like 30's Cotton. I want to save this string.

My save query

SqlCommand com = new SqlCommand("insert into Table_Name values('" + textBox57.Text +"')", con);

but i got error. how can i solve this error.
Please reply me as soon as possible.
Thank you
Posted
Comments
Gautham Prabhu K 7-Nov-15 10:55am    
Can you share the error message you got?
Its little hard to guess what went wrong?

I find it best to use HTML Encode and Decode. In fact, for security reasons, you should always use HTMLEncode and HTMLDecode when saving large areas of text to stop XSS scripting and SQL Injection.

C#
SqlCommand com = new SqlCommand("insert into Table_Name values('" +  Server.HtmlEncode(textBox57.Text +"')", con));


To Save to Database:
https://msdn.microsoft.com/en-us/library/w3te6wfz%28v=vs.110%29.aspx

To Display from Database:
https://msdn.microsoft.com/en-us/library/7c5fyk1k%28v=vs.110%29.aspx
 
Share this answer
 
Your code is vulnerable to SQL Injection.[^]
You can prevent sql injection as well as solve your problem by using either Stored Procedure or Parameterized Query.

Try this-
SQL
SqlCommand com = new SqlCommand("insert into Table_Name values(@MyValue)", con);
cmd.Parameters.AddWithValue("@MyValue", textBox57.Text);


Check following article for further details-
Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

Hope, it helps :)
 
Share this answer
 
Use SQL Parameter to solve the issue
Parameter can send your exact code to sql
like

SQL
insert into MyTable values (@id, @name) 


And

C#
int id = 1;
string name = "30's Cotton";
SqlCommand command = new SqlCommand(commandString, connection);
command.Parameters.AddWithValue("id", id);
command.Parameters.AddWithValue("name", name);
command.ExecuteNonQuery();


--
Happy Codding
 
Share this answer
 
v2
Use double single quote. Example given below

SQL
insert into [tbl_Test]
values(2,'30''s')
 
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