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


store html code in database in asp.net

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="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 :

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
  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...";
        }
    }
}
 
Tag : ASP.NET, C#
0 Komentar untuk "Store HTML Code in Database by Using ASP.NET C# Coding"

Back To Top