In this article I will explain with an example, how to save (insert) and retrieve (display) HTML Tags (Code) from database in ASP.Net using C# and VB.Net.
Database
Below is the schema of the Table that will be used for saving the Rich Text HTML content. It is recommended to use NVARCHAR datatype for saving HTML tags (codes).
Note: The SQL for creating the database is provided in the attached sample code.
HTML Markup
The following HTML Markup consists of a Multiline TextBox and is made a Rich TextBox using the TinyMCE RichTextEditor plugin and a Button.
<script type="text/javascript" src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({ selector: 'textarea', width: 500 });
</script>
<asp:TextBox ID="txtHTMLContent" runat="server" TextMode="MultiLine"/>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Saving (Inserting) HTML Tags (Code) to database in ASP.Net
The following event handler is executed when the Submit Button is clicked. The Rich Text HTML content is fetched from the TextBox and is inserted into the database table.
C#
protected void Submit(object sender, EventArgs e)
{
string query = "INSERT INTO [HTMLContent] VALUES (@Content)";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Content", txtHTMLContent.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Default.aspx");
}
}
}
VB.Net
Protected Sub Submit(sender As Object, e As EventArgs)
Dim query As String = "INSERT INTO [HTMLContent] VALUES (@Content)"
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(conString)
Using cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Content", txtHTMLContent.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Response.Redirect("~/Default.aspx")
End Using
End Using
End Sub
Retrieving (Displaying) HTML Tags (Code) from database in ASP.Net
Inside the Page Load event of the page, the Rich Text HTML content is fetched from the database table and is displayed within the TinyMCE Rich TextBox.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "SELECT [Content] FROM [HTMLContent]";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.Read())
{
txtHTMLContent.Text = sdr["Content"].ToString();
}
sdr.Close();
}
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim query As String = "SELECT [Content] FROM [HTMLContent]"
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(conString)
Using cmd As New SqlCommand(query, con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
txtHTMLContent.Text = sdr("Content").ToString()
End If
sdr.Close()
End Using
con.Close()
End Using
End Using
End If
End Sub
Screenshots
Saving and Retrieving HTML Tags (Code) from database
HTML Tags (Code) saved in database
Errors
Downloads