Please tell me how can save a string with special characters to DB.Special characters may contatin single quotes/double quotes etc.. I am using ASP.NET with C#
+1
A:
Use parameterized queries.
http://aspnet101.com/aspnet101/tutorials.aspx?id=1
When rendering to the client, you should also use Server.HtmlEncode() to convert characters which have special meaning in HTML to numeric character references.
JasonTrue
2008-11-11 04:28:14
A:
Hard to answer without much details. But usually the best bet is parametrized queries.
Keltex
2008-11-11 04:28:41
A:
Are you encoding the value when you write it out? (Server.HtmlEncode(value))
Matthew Cole
2008-11-11 04:38:23
Not Yet,If i am using Server.HtmlEncode, Can i escape?
2008-11-11 04:40:26
A:
Using (SqlConnection conn = new SqlConnection(connstr))
{
Using (SqlCommand command = new SqlCommand("INSERT INTO FOO (col) VALUES (@arg)"))
{
command.Connection = conn;
command.Parameters.AddWithValue("@arg",SpecialCharsString);
command.ExecuteNonQuery();
}
}
Reading it out should not be breaking your output at all, if it is, its not the database code doing it.
FlySwat
2008-11-11 04:42:08