Sunday, February 19, 2012

Aritmetic Overflor on READING a date value

I have a date in my SQL Server Database of 3/12/2204. This is, of course, 200 years after today and is not a correct value.

However, when my repeater is trying to display the records in that table, I get the following error:

"Arithmetic overflow error converting expression to data type datetime."

Here is the stack trace:

[SqlException: Arithmetic overflow error converting expression to data type datetime.]
System.Data.SqlClient.SqlDataReader.Read() +157
System.Data.Common.DbEnumerator.MoveNext() +44
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +504
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +48
System.Web.UI.WebControls.Repeater.DataBind() +23

Now, none if this is in my own code, and so it is difficult to put any decent error-trapping. This is all in the built-in ASP.NET Repeater and SqlDataReader classes.

Why on Earth would there be an arithmetic overflow here? Note that the original table this is coming from has the date stored in the SqlDbType.DateTime format! In other words, why is there any conversion needed?

Perhaps it is converting the data from SqlDbType to DbType, but even then, what is so difficult about dealing with the year 2204? According to BOL, the DateTime data type should support up to the year 9999!

Note that when I access the table through Enterprise Manager and change the date, the error disappears. I tried a number of over-and-under tests, and I get this error on any date after 3/19/2151!

Does ASP.NET not support dates after that? I doubt it, but does anyone have an explanation for this?

Thanks,
JKalmost sounds like internally it's storing it as an unsigned integer (like milliseconds since a certain time), although I agree this seems buggy. this could be SQL Server data provider specific. are you sure you aren't using a smalldatetime, that's only valid up to 2079. don't think SQL Server would even let you enter 2204 in a smalldatetime column though.

wonder if this explains it (from BOL)

Values with the datetime data type are stored internally by Microsoft SQL Server as two 4-byte integers. The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system reference date. Values for datetime earlier than January 1, 1753, are not permitted. The other 4 bytes store the time of day represented as the number of milliseconds after midnight.

just thinking out loud...

cs|||Thanks,

I had originally thought that the smalldatetime might have been part of it when I first got this overflow error a few weeks ago.

What I did was change all of my table columns from smalldatetime to datetime, just to be on the safe side.

The problem seemed to disappear at that time, but then came back today, when I was able to test it a bit more thoroughly.

The real oddity is that SQL Server itself permits the value in the database. It reads, writes and displays the date fine with no error.

But when the SqlDataReader class pulls the next record in the data set containing that information, it runs into this arithmetic overflow.

Any other ideas?
JK

No comments:

Post a Comment