SqlDateTime Overflow Error

[NOTE:  This was originally posted at DevAuthority.com on 6/15/2005]

<note to self>

I have run into this problem a few times, but for some reason can never seem to remember the resolution.  Therefore I decided to note it here for future reference.When trying to insert a null value for a nullable datetime field in sql 2K, I end up getting the SqlDateTime Overflow error.  This appears to be a problem with a difference between the definition of the Date and Datetime datatypes in .net and sql2K. The way I found to solve this (there may be better and I would love to hear if there are) is:

if myDate = DateTime.MinValue then
    myParamater.Value = System.DBNull.Value
    myParamater.Value = myDate
end if

Please weigh in if you have any additional insight or a better solution, but this does seem to work.

</note to self>


Comments (1) -

I have found it useful to create an attribute that specifies a certain value for mapping to System.DBNull.Value to decorate fields/properties to.  Then, my stored procedure object can automatically assign the correct database value for my member value.

e.g. DbNullValue(Int32.MinValue),

DbNullValue(typeof(DateTime),"1/1/0001 12:00:00 AM")

Pingbacks and trackbacks (5)+

Add comment