Buffer overflow with Decimal datatype in SQL SERVER

My POCO declares this field:

public decimal ValoreMax { get; set; }

the corresponding table’s field on SqlServer 2016 is:

VALOREMAX (decimal(38,6))

then I populate the table using the POCO created as follow:

new POCO {ValoreMax = Decimal.MaxValue}

management studio shows the value I inserted.

Now when I get the POCO from the table using a db.Select a buffer overflow raises and the field value returned is 0, this is the trace:

2017-05-18 11:52:39.141 ERROR [thread: STP SmartThreadPool Thread #1] ServiceStack.OrmLite.OrmLiteWriteCommandExtensions - System.OverflowException: Overflow di conversione.
in System.Data.SqlClient.SqlBuffer.get_Decimal()
in System.Data.SqlClient.SqlBuffer.get_Value()
in System.Data.SqlClient.SqlDataReader.GetValueFromSqlBufferInternal(SqlBuffer data, _SqlMetaData metaData)
in System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
in System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
in ServiceStack.OrmLite.OrmLiteConverter.GetValue(IDataReader reader, Int32 columnIndex, Object[] values)
in ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateWithSqlReader[T](T objWithProperties, IOrmLiteDialectProvider dialectProvider, IDataReader reader, Tuple`3[] indexCache, Object[] values)

The sql server dialect is 2016 but the same happens for 2012 and standard (no number) dialects.

Note.
If I switch to PostgreSQL database all is working great, no issue, pgAdmin show me the correct value and the POCO returned by select is right.

Any ideas?

The .NET SqlClient throws this Exception an when trying to call IDataReader.GetValues() when a record contains a Max Decimal:

   at System.Data.SqlClient.SqlBuffer.get_Decimal()
   at System.Data.SqlClient.SqlBuffer.get_Value()
   at System.Data.SqlClient.SqlDataReader.GetValues(Object[] values)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.GetValues(IDataReader reader, Object[] values)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateWithSqlReader[T](T objWithProperties, IOrmLiteDialectProvider dialectProvider, IDataReader reader, Tuple`3[] indexCache, Object[] values)

So OrmLite then falls back to perform individual column fetches which throws the same Exception:

   at System.Data.SqlClient.SqlBuffer.get_Decimal()
   at System.Data.SqlClient.SqlBuffer.get_Value()
   at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
   at ServiceStack.OrmLite.OrmLiteConverter.GetValue(IDataReader reader, Int32 columnIndex, Object[] values)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateWithSqlReader[T](T objWithProperties, IOrmLiteDialectProvider dialectProvider, IDataReader reader, Tuple`3[] indexCache, Object[] values)

Basically doesn’t look like .NET’s SqlClient can read a max decimal value.

Postgre provider works fine.

I used decimal as Microsoft suggests.
Does SQL Server decimal datatype is incompatible with .net?
It sounds like Microsoft is incompatible with itself.

Does OrmLite can cope with this issue?

Postgres is completely unrelated because it’s using a different npgsql ADO.NET Provider, SQL Server uses the .NET SqlClient to communicate with SQL Server.

Does OrmLite can cope with this issue?

OrmLite is physically unable to read the value from SQL Server’s ADO.NET Provider.

Not good.

I think I cannot inject in OrmLite a third party provider.

I must to redesign my app.

Thanks anyway.

It’s possible to obtain exception notifications when this happens?

Handlers I added for UncaughtExceptionHandlers and ServiceExceptionHandlers are never called.

Can I configure OrmLite to throw exception when something goes wrong in the SqlClient?

The exception occurred in OrmLite which by default swallows data read exceptions and logs it instead in order to return as much data as possible. So it would be visible if you had logging enabled.

I’ve just added an option where you can get OrmLite to throw on Exceptions like this in this commit with:

OrmLiteConfig.ThrowOnError = true;

This change is available from v4.5.9 that’s now available on MyGet