These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Error: Save HTML (WYSIWYG) into MS-SQL (Text, XML or NText)
PostPosted: Mon Jun 30, 2008 1:43 pm 
Newbie

Joined: Mon Jun 30, 2008 12:56 pm
Posts: 6
Description of the problem
I'm using a What-you-see-is-what-you-get (WYSIWYG) editor to allow user to enter HTML content and save it into the body column in the database. For some reason, I'm getting this exception every time I copy and paste


MS-SQL 2005: Schema
subject nvarchar(50)
subtitle nvarchar(50)
posted datetime
category nvarchar(50)
body next (tried text and XML; both deliver the same result)

Hibernate version:
nhibernate.dll is 2.0.0.2001

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="WSLCB" namespace="CMS">
<class name="ArticleM" table="tbl_article">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="Id" sql-type="Int64" not-null="true"/>
<generator class="increment"/>
</id>

<!-- Article. -->
<property name="Subject">
<column name="Subject" not-null="true" />
</property>
<property name="Subtitle" />
<property name="Body">
<column name="body" sql-type="StringClob" not-null="true"/>
</property>
<property name="Posted" />
<property name="Category" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
--- Save function ---
ArticleM article = new ArticleM();
article.Subject = tbBody.Text;
article.Subtitle = tbSubtitle.Text;
article.Body = tbBody.Text;
article.Category = ddlCategory.SelectedValue;
article.Posted = DateTime.Today;

ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
session.Save(article);
tx.Commit(); <=== WHERE Exception occurs (see next bullet)

--- ArticleM.cs ---
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CMS
{
public class ArticleM
{
private string id;
private string subject;
private string subtitle;
private string body;
private DateTime posted;
private string category;
public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Subject
{
get { return subject; }
set { subject = value; }
}
public virtual string Subtitle
{
get { return subtitle; }
set { subtitle = value; }
}
public virtual string Body
{
get { return body; }
set { body = value; }
}
public virtual DateTime Posted
{
get { return posted; }
set { posted = value; }
}
public virtual string Category
{
get { return category; }
set { category = value; }
}
}
}


Full stack trace of any exception that occurs:

String or binary data would be truncated.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

Source Error:


Line 262: ITransaction tx = session.BeginTransaction();
Line 263: session.Save(article);
Line 264: tx.Commit();
Line 265: }
Line 266: protected void btnSave_Click(object sender, EventArgs e)


Source File: C:\src\wslcb\cms\article.aspx.cs Line: 264

Stack Trace:


[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +925466
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800118
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +149
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:196
NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\AdoNet\NonBatchingBatcher.cs:39
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:2588

[GenericADOException: could not insert: [CMS.ArticleM][SQL: INSERT INTO tbl_article (Subject, Subtitle, body, Posted, Category, Id) VALUES (?, ?, ?, ?, ?, ?)]]
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:2600
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:2935
NHibernate.Action.EntityInsertAction.Execute() in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Action\EntityInsertAction.cs:51
NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Engine\ActionQueue.cs:130
NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Engine\ActionQueue.cs:113
NHibernate.Engine.ActionQueue.ExecuteActions() in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Engine\ActionQueue.cs:146
NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:246
NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:19
NHibernate.Impl.SessionImpl.Flush() in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1186
NHibernate.Transaction.AdoTransaction.Commit() in c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:163
WSLCB.Article.saveArticle() in C:\src\wslcb\cms\article.aspx.cs:264
WSLCB.Article.btnSave_Click(Object sender, EventArgs e) in C:\src\wslcb\cms\article.aspx.cs:268
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

Name and version of the database you are using:
MS-SQL 2005 (9.0.3054)

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 5:56 pm 
Newbie

Joined: Mon Jun 30, 2008 11:43 am
Posts: 7
Assuming there is no problem with the body part (as that is ntext and declared as StringClob) have you checked that subject, subtitle and category are not overflowing, as they are limited to 50 characters.

If you specify Length=50 for those properties in the mapping file nhibernate might be able to tell you which property is too large rather than waiting for the sql server to reject the data.

Also, if you're using Sql Server 2005 and don't need to worry so much about backwards compatibility, ntext has been deprecated in favour of using nvarchar(max).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 1:23 am 
Newbie

Joined: Mon Jun 30, 2008 12:56 pm
Posts: 6
I think it's the fact that I'm entering html tags into the body column that is causing the problem because if I enter test for all the columns, it works just fine. No overflow at all. If I copy and paste HTML from any web page into the body (WYSIWYG editor) I get this error message. For some reason, the StringClob is not working. Could it be caching?

KeeperOfTheSoul wrote:
Assuming there is no problem with the body part (as that is ntext and declared as StringClob) have you checked that subject, subtitle and category are not overflowing, as they are limited to 50 characters.

If you specify Length=50 for those properties in the mapping file nhibernate might be able to tell you which property is too large rather than waiting for the sql server to reject the data.

Also, if you're using Sql Server 2005 and don't need to worry so much about backwards compatibility, ntext has been deprecated in favour of using nvarchar(max).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 4:53 am 
Newbie

Joined: Mon Jun 30, 2008 11:43 am
Posts: 7
It worked fine for me, though I notice you have:
Code:
<property name="Body">
    <column name="body" sql-type="StringClob" not-null="true"/>
</property>


sql-type is supposed to be the database's column type when you want to override nhibernate's default mapping, so sql-type would be ntext or nvarchar(max).

I think you want a mapping more like this:
Code:
<property name="Body" column="body" type="StringClob" not-null="true" />


Note, type and not sql-type.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 2:49 pm 
Newbie

Joined: Mon Jun 30, 2008 12:56 pm
Posts: 6
Just out of curiousity. Can you insert the following string into "body":

<a href="www.google.com">google TEST</a>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 1:10 pm 
Newbie

Joined: Mon Jun 30, 2008 12:56 pm
Posts: 6
Thanks for all the help KeeperOfTheSoul.

Here is what happened, I have just noticed that the form action is saving the body as title. Now everything worked PERFECT! Thanks a lot for your feedback!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.