Answered by:
How to sql html code in sql database?

Question
-
User-1136231507 posted
Hi,
I've created a texbox in asp.net and used tiny_mce java changed it to richtextBox and now I want to save it to sql database and retrieve this saved information to asp page
how to save it and retrieve info And which data type I have to use in sql
plz help, THANKS
Sunday, September 14, 2014 1:51 AM
Answers
-
User281315223 posted
If memory serves, you should still be able to access the content via the Text property as you might expect within ASP.NET. It may look a little strange since it will likely be HTML, however you should be fine with accessing it that way. You will need to adjust some settings at either the Page-level to prevent ASP.NET from seeing the HTML as malicious, which can be done by by adding the validateRequest attribute into the Page directive for the page that contains your TextBox :
<!-- Include this to disable Request Validation in a specific Page --> <@ Page validateRequest="false" %>
Additionally, you can handle this at the application level within your web.config by using either :
<configuration> <system.web> <!-- The following disables validation for the Application --> <pages validateRequest="false" /> </system.web> </configuration>
or :
<configuration> <system.web> <httpRuntime requestValidationMode="2.0" /> </system.web> </configuration>
Actually Saving Your Data
With regards to saving the content within your database, you'll just need to build a column (a large nvarchar(x), nvarchar(max) or text column would suffice depending on the size you are expecting) and then you'll use something like the following to open your connection and insert our value :
using(SqlConnection yourConnection = new SqlConnection("Your Database Connection String")) { // Build your query (to insert your HTML) var query = "INSERT INTO YourTable (YourHTMLColumn) VALUES (@YourHTMLContent)"; // Build a command to execute using(var yourCommand = new SqlCommand(query, yourConnection)) { // Open your connection yourConnection.Open(); // Add your HTML content from your TextBox as a parameter yourCommand.Parameters.AddWithValue("@YourHTMLContent", YourHTMLTextBox.Text); // Execute your query yourCommand.ExecuteNonQuery(); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 14, 2014 9:05 AM
All replies
-
User-821857111 posted
It's just text, so you insert it into a database in the same way you would any other text. If you expect the amount of text to potentially be large, use nvarchar(max). The only issue is that ASP.NET prevents the posting of HTML values by default, so you have to disable request validation. How you do that depends on which ASP.NET framework you are using: http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx.
Sunday, September 14, 2014 5:28 AM -
User281315223 posted
If memory serves, you should still be able to access the content via the Text property as you might expect within ASP.NET. It may look a little strange since it will likely be HTML, however you should be fine with accessing it that way. You will need to adjust some settings at either the Page-level to prevent ASP.NET from seeing the HTML as malicious, which can be done by by adding the validateRequest attribute into the Page directive for the page that contains your TextBox :
<!-- Include this to disable Request Validation in a specific Page --> <@ Page validateRequest="false" %>
Additionally, you can handle this at the application level within your web.config by using either :
<configuration> <system.web> <!-- The following disables validation for the Application --> <pages validateRequest="false" /> </system.web> </configuration>
or :
<configuration> <system.web> <httpRuntime requestValidationMode="2.0" /> </system.web> </configuration>
Actually Saving Your Data
With regards to saving the content within your database, you'll just need to build a column (a large nvarchar(x), nvarchar(max) or text column would suffice depending on the size you are expecting) and then you'll use something like the following to open your connection and insert our value :
using(SqlConnection yourConnection = new SqlConnection("Your Database Connection String")) { // Build your query (to insert your HTML) var query = "INSERT INTO YourTable (YourHTMLColumn) VALUES (@YourHTMLContent)"; // Build a command to execute using(var yourCommand = new SqlCommand(query, yourConnection)) { // Open your connection yourConnection.Open(); // Add your HTML content from your TextBox as a parameter yourCommand.Parameters.AddWithValue("@YourHTMLContent", YourHTMLTextBox.Text); // Execute your query yourCommand.ExecuteNonQuery(); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 14, 2014 9:05 AM