Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have comment textbox.And it is working perfectly.when i enter text in textbox of comment data inserted sucessfully in database table.But if i use 's in writing comment in textbox,it will give me error.I know perfectly that it is error because of 's.what coding is require to solve this problem?
SqlCommand cmd = new SqlCommand("insert into table (name,address,comment) values('" + txtname.text + "','" + txtaddress.text + "','"+txtcomment.text+"')", cnn);
Posted

1 solution

Basically, you need to double it up.

Try something like:

C#
txtcomment.Text.Replace("'", "''");


That is very simplistic and you should research the reasons behind this and come up with a solution that fits your specific needs.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-13 17:02pm    
My 5, for leaving OP some room for further work. :-)
—SA
fjdiewornncalwe 17-Jan-13 17:13pm    
+5.
adriancs 17-Jan-13 22:11pm    
To understand the need of doing this,
Try execute this:

string name = "anyname";
string address = "anyaddress";
string comment = "anycomment'); DROP TABLE IF EXISTS `table`; SELECT ('surprise";

SqlCommand cmd = new SqlCommand("insert into table (name,address,comment) values('" + name + "','" + address + "','" + comment + "')", cnn);

It's called SQL Injection.
Member 9511889 19-Jan-13 1:01am    
where this code i have to write?
R. Giskard Reventlov 19-Jan-13 10:03am    
It's in the solution. Think about it.

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