Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.MinValue and SqlDateTime overflow

I don't want to validate txtBirthDate so I want to pass DateTime.MinValue in database.

My code:

 if (txtBirthDate.Text == string.Empty)     objinfo.BirthDate = DateTime.MinValue;  else      objinfo.BirthDate =  DateTime.Parse(txtBirthDate.Text); 

DateTime.MinValue return Date = {1/1/0001 12:00:00 AM}

I got a SQL Error:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

I under stand it but I don't understand why DateTime.MinValue return invalid date time which is unable to insert in database.How to handle this type of situation?

like image 510
4b0 Avatar asked Mar 01 '13 12:03

4b0


1 Answers

Very simple avoid using DateTime.MinValue use System.Data.SqlTypes.SqlDateTime.MinValue instead.

like image 178
Sankar M Avatar answered Oct 22 '22 05:10

Sankar M