I'm debugging an intermittent truncation error in my application. The error in my logs looks like this:
System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated. Generated: Tue, 02 Nov 2010 03:55:18 GMT
NHibernate.Exceptions.GenericADOException: could not insert:
[DataModel.Product][SQL: INSERT INTO [Product] (Fields) VALUES (?, ?,...);
select SCOPE_IDENTITY()] --->
System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
Notice that the error does not provide parameter values for the sql that threw the error, only placeholders: (?, ?,...). Is there a way to get these from nHibernate? Something like this:
try {
...
Session.Flush(); // throws
}
catch (GenericADOException ex) {
// want to get the bad parameter values,
// so I can re-throw a more helpful exception
}
Most of the time, this string or binary data truncated.... comes from Nhibernate Ado exception when data being fed has more characters than the field length. e.g. if a table db field is nvarchar(50), but an effort is being made to insert more than 50 characters, this exception occurs. solution is to either increase the table Db field length or reduce the data characters being inserted.
You could setup an IPreInsertEventListener that loops through the string properties prior to insertion, checking their length.
If one of the properties is too long, you can log the name of the property and its value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With