DateTime: Out of range dates & null

Posts   
 
    
jburns
User
Posts: 1
Joined: 23-Mar-2017
# Posted on: 23-Mar-2017 20:07:30   

Hello,

I'm having trouble with some code that's taking a user's input from a datetime textbox and updating an Entity's DateTime field.  The code works just fine with normal dates, but if the date is less than the minimum for SQL Server's datetime type (~1753 or so), the ORM appears to just insert NULL into the column (rather than throwing an error, as I'd expect).

The code looks something like this:

//if MyViewModel.MyDateField is more recent than 1973, this code works fine //if not, the assignment fails silently, and MyEntity.MyDateField is null MyEntity.MyDateField = MyViewModel.MyDateField; Adapter.Save(MyEntity);

Any ideas? I'm definitely not thrilled about needing to worry about this everywhere I'm using a DateTime...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Mar-2017 07:25:46   

This is more a SQL related issue. The problem resides in that .Net Datetime.MinValue.Value is not the same as System.Data.SqlTypes.SqlDateTime.MinValue.Value. So it defaults to NULL.

I would suggest to doing a custom field validation in your CommonEntityBase (so apply for all entities). In there you could check the type of the field and apply the appropriate rule to raise a validation error.

David Elizondo | LLBLGen Support Team