12244

ASP.NET-Saving Special Characters to Database

Question:

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#

Answer1:

Use parameterized queries.

<a href="http://aspnet101.com/aspnet101/tutorials.aspx?id=1" rel="nofollow">http://aspnet101.com/aspnet101/tutorials.aspx?id=1</a>

When rendering to the client, you should also use Server.HtmlEncode() to convert characters which have special meaning in HTML to numeric character references.

Answer2:

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

Answer3:

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 ?

Answer4:

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

Answer5:

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.

Answer6:

(Server.HtmlEncode(value)) worked !

Recommend

  • How to change the format of a column in GridView from c#
  • Spark View Engine with custom HTML Helpers
  • javafx 3d performance large data set
  • What is wrong in my MVC implementation?
  • SSRS 2008 - Sorting within a group
  • Rails 5 - Google Maps - Javascript error - initMap is not a function - fixing one js issue creates a
  • Regex for URL rewrite with optional query string parameters
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • is there a way to update filter with async data
  • Other than Linq to SQL does anything else consume INotifyPropertyChanging?
  • Where these are stored?
  • How to render a pixel array most efficiently to a window in c++?
  • Using Laravel 5.4 pusher
  • Switch to popup in python using selenium
  • Convert SQLite database to XML
  • How to assign byte[] as a pointer in C#
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • Handling un-mapped Rest path
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Master page gives error
  • PHP - How to update data to MySQL when click a radio button
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Apache 2.4 - remove | delete | uninstall
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • How to stop GridView from loading again when I press back button?
  • JaxB to read class hierarchy