I\'m trying to do an INSERT into an already set DB in MS SQL Server, the DB server is in a shared hosting (godaddy).
And what i\'m trying to achieve is to store an a
Be aware that MS SQL Server's DateTime
and the C# DateTime
have different MinValues.
While MS SQL DateTime begins only at {1/1/1753 12:00:00 AM}
the C# DateTime start with {1/1/0001 12:00:00 AM}
.
There are 3 approaches that come to my mind:
Make sure in your code that the object that you want to save has a valid DateTime value set before saving it. The other posters already made some suggestions for this.
Add a default value to the column on the database side. To me, this seems to be the easiest and cleanest approach. You would neither need to change the data type nor would it affect your code and you would make sure that no invalid date would (or could) be saved.
If this is possible at your stage of development, you may change the data type of the column to DateTime2
, which allows for dates beginning with {1/1/0001 12:00:00 AM}
. MSDN documentation here.