Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com

Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
When I tried to save DateTime data to SQL database, I've got 
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
I've had to create a function
object SafeSqlDateTime(DateTime dtValue)
{
object theValue = dtValue;
if (dtValue <(DateTime) SqlDateTime.MinValue) // January 1, 1753.
{
   theValue =
null;
}
return theValue;
For opposite SqlDateTime to DateTime Conversion see
http://msdn.microsoft.com/en-us/library/aa326475(VS.71).aspx
 
Posted on Sunday, May 3, 2009 9:13 PM .Net Framework , SQL Server | Back to top


Comments on this post: Safe Save DateTime to SQL Database.

# re: Safe Save DateTime to SQl Database.
Requesting Gravatar...
If you're using DateTime.MinValue for the same reason eric is in that blog post you referenced, why don't you just use a Nullable<DateTime> instead?
Left by Max Schmeling on May 05, 2009 3:31 AM

# re: Safe Save DateTime to SQl Database.
Requesting Gravatar...
Max,
To use Nullable<DateTime> (or DateTime? ) is a good idea in general, but I have legacy code, that uses DateTime with valid data most of the time, and only in special cases it is optional and initialized as 1/1/0001
Left by Michael Freidgeim on May 05, 2009 8:08 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net