Storing HTML Code in SQL Server with C# Coding in ASP.NET

In this article, we'll explore how to store HTML tags in SQL Server using C# coding in an ASP.NET application. This process involves adding two controls to our page:

  1. TextBox or TextArea Control: Used to enter HTML tags or code.
  2. Button Control: Triggers the storage process when clicked.

Let's dive into the details:

Create Database:


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

HTML Design:

For this, we create an ASP.NET page with a TextBox for the title, a TextArea for HTML code, and a Button to submit the code.


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table>
         <tr>
             <td>Title : </td>
             <td>
                 <asp:TextBox ID="txttitle" runat="server"></asp:TextBox>
             </td>
         </tr>
         <tr>
             <td colspan="2">
                 <asp:TextBox ID="txtcode" runat="server" TextMode="MultiLine" Width="400" Height="250"></asp:TextBox>
             </td>
         </tr>
         <tr>
             <td colspan="2">
                 <asp:Button ID="btnCode" runat="server" Text="Post" OnClick="btnCode_Click"/>
             </td>
         </tr>
     </table>
    </div>
        <br />
        <asp:Label ID="lbldisplay" runat="server"></asp:Label>
    </form>
</body>
</html>

Web Config:

Add the following httpRuntime element in the web.config to allow HTML code submission.


<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" />
    </system.web>

</configuration>

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);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lbldisplay.Text = "Code Inserted Successfully...";
        }
    }
}

This setup allows users to input HTML code via the provided controls and store it in the SQL Server database when the "Post" button is clicked. The validateRequest="false" attribute in the page directive ensures that HTML code is accepted. The httpRuntime configuration further supports HTML submissions. With this, you can seamlessly integrate HTML storage into your ASP.NET application.

Post a Comment

0 Comments