Hi ,
Recently , we have a requirement to save the html tag contends into database using asp.net with example .
For Eg:
Code
Privacy Policy
Technology and its subsidiaries and affiliates (hereinafter collectively referred to as “Suntoyo”, "we", "our", "us")the above text is example for text with html tags .Ayone suggest with samle code .
THANKS in ADVANCE,
Karthikeyan .K


Jaish MathewsRank #225 · 11K reputationPosted Dec 7, 2024, 4:53 AM EST
NB: When seeking assistance in any professional community, it's important to demonstrate your efforts and contributions toward resolving the issue. Instead of directly asking for code or solutions, share what you've tried so far and specify where you're encountering challenges. This approach fosters meaningful collaboration and shows your commitment to problem-solving.
To save HTML content into a database using ASP.NET, you can use the following approach. The main steps are:
Database Table
Create a table
HtmlContentwith the following schema:CREATE TABLE HtmlContent ( Id INT IDENTITY(1,1) PRIMARY KEY, HtmlData NVARCHAR(MAX) NOT NULL );Entity Framework Model
If you're using Entity Framework, add the following entity to your DbContext:
public class HtmlContent { public int Id { get; set; } public string HtmlData { get; set; } }Add this to your
DbContext:public DbSet HtmlContents { get; set; }Run a migration or update the database if needed.
ASP.NET MVC Controller
Here's an example of how to save the content to the database:
public class HtmlContentController : Controller { private readonly MyDbContext _dbContext; public HtmlContentController() { _dbContext = new MyDbContext(); // Initialize DbContext (Dependency Injection recommended) } [HttpPost] public ActionResult SaveHtmlContent(string htmlContent) { if (string.IsNullOrEmpty(htmlContent)) { return Json(new { success = false, message = "Content cannot be empty" }); } var content = new HtmlContent { HtmlData = htmlContent }; _dbContext.HtmlContents.Add(content); _dbContext.SaveChanges(); return Json(new { success = true, message = "Content saved successfully", contentId = content.Id }); } }Razor View
You can create a simple form to send the HTML content to the server:
@{ ViewBag.Title = "Save HTML Content"; } Save HTML Content Privacy Policy Technology and its subsidiaries and affiliates (hereinafter collectively referred to as “Suntoyo”, "we", "our", "us") SaveSaving Using ADO.NET (Alternative Approach)
If you're not using Entity Framework, you can directly save the content using ADO.NET:
public void SaveHtmlToDatabase(string htmlContent) { string connectionString = "YourConnectionStringHere"; using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "INSERT INTO HtmlContent (HtmlData) VALUES (@HtmlData)"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@HtmlData", htmlContent); connection.Open(); command.ExecuteNonQuery(); } } }Call this method:
SaveHtmlToDatabase("Privacy Policy... ");Notes:
NVARCHAR(MAX)for large HTML content.Jenith ThakkarRank #789 · 2.9K reputationPosted Dec 7, 2024, 5:23 AM EST
To save HTML content (including tags) into a database using ASP.NET, you can follow these steps:
Design your database: Use a column of type
NVARCHAR(MAX)orTEXTin your database table to store the HTML content.Create a Web Form or API: Use ASP.NET Web Forms or MVC to accept HTML content from a form.
Save the HTML content: Use ADO.NET, Entity Framework, or Dapper to save the HTML content into your database.
Sample Code in ASP.NET Web Forms
Database Table Example
CREATE TABLE HtmlContent ( Id INT IDENTITY PRIMARY KEY, Content NVARCHAR(MAX) );Frontend: ASP.NET Web Form
Code-Behind: Save HTML to Database
using System; using System.Data.SqlClient; using System.Configuration; public partial class SaveHtml : System.Web.UI.Page { protected void btnSave_Click(object sender, EventArgs e) { string htmlContent = txtHtmlContent.Text; // Connection string from Web.config string connectionString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { string query = "INSERT INTO HtmlContent (Content) VALUES (@Content)"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Content", htmlContent); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } Response.Write("HTML content saved successfully!"); } }Web.Config Connection String
Explanation
txtHtmlContent).btnSave_Clickevent captures the HTML content.HtmlContenttable in the database.NVARCHAR(MAX).Notes
Amit MohantyRank #112 · 20K reputationPosted Dec 9, 2024, 1:52 AM EST
Create a table in MySQL with a column that can store HTML content. Use the
TEXTorLONGTEXTdata type, depending on the size of your HTML data.CREATE TABLE HtmlContent ( ID INT AUTO_INCREMENT PRIMARY KEY, Content LONGTEXT NOT NULL );Create a HtmlContentHandler class which will handle the insert html data to database.
using System; using System.Data; using System.Data.SqlClient; using MySql.Data.MySqlClient; public class HtmlContentHandler { private string connectionString = "server=localhost;database=YourDatabase;user=YourUser;password=YourPassword;"; public void SaveHtmlContent(string htmlContent) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { string query = "INSERT INTO HtmlContent (Content) VALUES (@Content)"; using (MySqlCommand command = new MySqlCommand(query, connection)) { command.Parameters.AddWithValue("@Content", htmlContent); connection.Open(); command.ExecuteNonQuery(); } } } }Now you can call the
SaveHtmlContentmethod to save the HTML content into the database:HtmlContentHandler handler = new HtmlContentHandler(); string htmlData = @" Privacy Policy Technology and its subsidiaries and affiliates (hereinafter collectively referred to as “Suntoyo”, 'we', 'our', 'us') "; handler.SaveHtmlContent(htmlData);