Question

I have a .NET Web Forms app. It references WCF services, which use NHibernate, to handle data access. The backend is SQL Server.

In TableA I have a nullable column "Notes" varchar(MAX) If I understand correctly, this column should be able to hold 2^28 characters.

I just received a call from a user today stating that her notes are getting truncated when she clicks "Save" in my application. I am able to reproduce this issue. She has entered 3960 characters into that field so far and she has 2 more sentences to add. She types them in, clicks "Save" and of the two sentences she just added, only 32 characters remain in the TextArea.

I have been stepping through my code trying to find where this data is getting truncated. I'm at a loss. I stepped through all the way to the _session.SaveOrUpdate(item); call in the Repository. I put a breakpoint on the call to SaveOrUpdate() and took a peek at the Notes property of my object. The entire string was still there. I hit F5 to continue, no exception was thrown. My notes appeared truncated in the web application. I looked at the record in the database, the Notes were truncated. They are always truncated at the same position, almost like I'm hitting a character limit.

public void Update(T item)
{
    try
    {
        _session.BeginTransaction();
        _session.SaveOrUpdate(item);
        _session.Transaction.Commit();
    }
    catch (Exception)
    {
        _session.Transaction.Rollback();
        throw;
    }
}

I also tried:

public void Update(T item)
{
    try
    {
        _session.BeginTransaction();
        _session.Update(item);
        _session.Transaction.Commit();
    }
    catch (Exception)
    {
        _session.Transaction.Rollback();
        throw;
    }
}

Curve ball: In SSMS I can compose an UPDATE statement and assign the entire notes string I want to insert to the "Notes" field. After executing that UPDATE statement my entire notes string is in the "Notes" field in TableA.

Any ideas why my data is getting truncated when I insert/update via my web application?

Update:

I ran SQL Profiler and saw that my Notes string was truncated in the UPDATE statement.

I exported my NHibernate mappings so I could see what was getting generated:

<property name="Notes" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <column name="Notes" />
</property>

Not very helpful. I'll look into the NHibernate related suggestions next.

Update Again: Thank you icemanind and Peter Gluck for leading me in the right direction. I changed the column to nvarchar(max) and modified my NHibernate mapping

Map(x => x.Notes)
    .Length(4001);

I am now able to add all of the notes, via the web app, that my user was trying to add. I need to test the resolution some more, but I'm confident enough to mark this question "Answered".

Was it helpful?

Solution

I use the following syntax to define an NVARCHAR(MAX) column when mapping with Fluent NHibernate:

const int NVarCharMax = 4001;  // force NHibernate to allocate max length for nvarchar data  
Map(x => x.ColumnName).Length(NVarCharMax);

This article describes the solution in more detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top