Store HTML Code in Database by Using ASP.NET C# Coding

store html code in database in

In this article i'm going to explain how to store HTML Tags in SQL Server With C# Coding in ASP.NET. For that first we need to add two control
1.) TextBox or Textarea Control - which is used to enter html tags or code in it.
2.) Button Control - which is used to store what ever value we entered in above TextBox (i.e HTML Code ) that data stored in SQL SERVER after clicking this button.
for more info we see practically in below sections

Create Database

     Create Table StoreHTML(
      title varchar(50),
      htmldata varchar(max)

HTML Design

In HTML Design Page we need to add some code i.e validateRequest="false" in @page page directives other wise its won't work. why we need to add ValidateRequest to know more Click Here

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeBehind="WebForm1.aspx.cs" Inherits="StoreHTMLCodetoDatabase.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
             <td>Title : </td>
                 <asp:TextBox ID="txttitle" runat="server"></asp:TextBox>
             <td colspan="2">
                 <asp:TextBox ID="txtcode" runat="server" TextMode="MultiLine" Width="400" Height="250"></asp:TextBox>
             <td colspan="2">
                 <asp:Button ID="btnCode" runat="server" Text="Post" OnClick="btnCode_Click"/>
        <br />
        <asp:Label ID="lbldisplay" runat="server"></asp:Label>

Web Config :

why we are adding httpruntime in web.config to know more about info, please read this article Click Here

<?xml version="1.0"?>

  For more information on how to configure your ASP.NET application, please visit

      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" />


C# Coding : Button Click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace StoreHTMLCodetoDatabase
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        protected void btnCode_Click(object sender, EventArgs e)
            SqlConnection con = new SqlConnection("Data Source=.; User ID=sa; Password=tiger; Database=master;");
            SqlCommand cmd = new SqlCommand("Insert into testcode values('" + txttitle.Text + "','" + txtcode.Text + "')", con);
            lbldisplay.Text = "Code Inserted Successfully...";

Post a Comment