Shyju
Shyju

Reputation:

ASP.NET-Saving Special Characters to Database

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#

Upvotes: 0

Views: 5849

Answers (6)

nikhil
nikhil

Reputation:

(Server.HtmlEncode(value)) worked !

Upvotes: 0

JasonTrue
JasonTrue

Reputation: 19644

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.

Upvotes: 1

FlySwat
FlySwat

Reputation: 175733

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.

Upvotes: 0

Matthew Cole
Matthew Cole

Reputation: 1339

Are you encoding the value when you write it out? (Server.HtmlEncode(value))

Upvotes: 0

Shyju
Shyju

Reputation:

Ok.Eventhough i saved in the DB.I need to display this back to a text box.Then the page is breaking. Ex: I have saved Student name as Ani"s and when i am displayin gthis

How to get rid of this problem ?

Upvotes: 0

Keltex
Keltex

Reputation: 26446

Hard to answer without much details. But usually the best bet is parametrized queries.

Upvotes: 0

Related Questions