Hey guys, help please...
i am using CKEditor in ASP.Net Website, i want to store the value of CKEditor to the database.
but it's giving errors while storing.
Here is my aspx code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeFile="new_article.aspx.cs" Inherits="post_article" validateRequest="false" %>
<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="~/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="~/ckeditor/adapters/jquery.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="content">
<asp:TextBox ID="txtTitle" CssClass="txtTitle" runat="server" placeholder="Enter title here..."></asp:TextBox>
<p style="width:80%; margin:10px;">
<CKEditor:CKEditorControl ID="txtPost" Height="200" BasePath="~/ckeditor/" runat="server" Toolbar="Basic" ClientIDMode="Inherit">
</CKEditor:CKEditorControl>
</p>
<asp:Button ID="btnPost" CssClass="btn" runat="server" Text="Post" OnClick="btnPost_Click" />
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
</div>
</asp:Content>
here is my code behind:
protected void btnPost_Click(object sender, EventArgs e)
{
string str = HttpUtility.HtmlEncode(txtPost.Text);
if (txtTitle.Text == "" || txtPost.Text == "")
{
lblStatus.Text = "Title or Post Content is Missing !";
}
else
{
string insertSQL = "INSERT INTO user_posts (";
insertSQL += "post_title, post_data, post_date, user_id) ";
insertSQL += "VALUES (";
insertSQL += "@post_title, @post_data, GETDATE(), @user_id) ";
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\Database.mdf;Integrated Security=True;MultipleActiveResultSets=True");
SqlCommand cmd = new SqlCommand(insertSQL, con);
cmd.Parameters.AddWithValue("@post_title", txtTitle.Text);
cmd.Parameters.AddWithValue("@post_data", str);
cmd.Parameters.AddWithValue("@user_id", "1");
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
lblStatus.Text = "Article Posted Successfully !";
}
catch (Exception err)
{
lblStatus.Text = err.Message;
}
}
}
here i tried:
string str = txtPost.Text
to store the data in database,
but In the post_data column of the database only the following text is posted:
CKEditor.NET.CKEditorControl
i encoded the data, as shown in code.
string str = HttpUtility.HtmlEncode(txtPost.Text);
but still In the post_data column of the database only the following text is posted:
CKEditor.NET.CKEditorControl
Tried all the database fields like varchar(MAX), nvarchar(MAX), text() but all in vain. the error is same always, please guide me for this.
i tried using txtPost.Value, but value is not a member of CKEditor namespace.
here is my DB structure:
CREATE TABLE [dbo].[user_posts] (
[post_id] INT IDENTITY (1, 1) NOT NULL,
[post_title] VARCHAR (50) NOT NULL,
[post_data] VARCHAR (MAX) NOT NULL,
[post_date] DATE NOT NULL,
[user_id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([post_id] ASC),
FOREIGN KEY ([user_id]) REFERENCES [dbo].[user_details] ([user_id])
);