0

I know this question has been asked a bunch of times on StackOverflow, but none of the answers seem to fit my situation, or they just tell them to allow NULL fields in their DateTime (which I don't want to do).

Here is the code:

public async Task<int> CreateJobAsync(JobViewModel jvm)
    {
        Job j = new Job();
        j.Name = jvm.Name;
        j.UserId = jvm.UserId;
        j.ClassDefinition = jvm.ClassDefinition;
        j.DaysToRun = jvm.DaysToRun;
        j.ToEmail = jvm.ToEmail;
        j.Active = true;
        j.CreatedDate = DateTime.Now;
        j.ModifiedDate = DateTime.Now;

        context.Jobs.Add(j);
        var result = await context.SaveChangesAsync();

        return result;
    }

All my fields in the Job class are DateTime as are the object in the database. The date values are not NULL. When I run Sql Profiler, it doesn't even show that a database call was made.

The Error occurs on the await call and it is:

conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

This seems like a fairly easy example and I do need these values to be not null. Any other ideas???

L_7337
  • 2,650
  • 28
  • 42

2 Answers2

2

DateTime is a value type in .Net and will never be null.

Now, being said that, the default value for DateTime is DateTime.MinValue in C# which equals to 01/01/0001, and all DateTime fields get this value by default, unless otherwise specified! But, this date is out of SQLServer date ranges, for which minimum date is 1/1/1753. Probably that's why you are getting this exception.

One way to make your code usable will be to use SqlDateTime.MinValue instead of DateTime.MinValue.

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • In my code, I am setting the values to DateTime.Now and I've even tried hard-coding today's date, but still get the same error. I'm not sure why it would use use DateTime.MinValue. The point out never getting a null value is valid so I'll try changing my database. – L_7337 Sep 27 '15 at 04:39
  • @L_7337 Somewhere in the code, some datetime field might be un-initialized, or ill-formatted. There are host of ways to counter this - please search SO with "conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value" for multiple existing answers. – Arghya C Sep 27 '15 at 04:46
2

There must be other field(s) in Job that are of type DateTime that are not being set in your code. This unset property will default to DateTime.MinValue which is 1-Jan-0001. The SQL type DATETIME only supports dates with a year greater than 1752. Hence, the reason for your exception.

The easiest solution is to change the database to use DATETIME2 and not DATETIME. See also DateTime2 vs DateTime in SQL Server

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Let me work on researching this. After seeing your answer, I reviewed Job.cs and there is another class inside it. And, THAT class has a DateTime in it. – L_7337 Sep 27 '15 at 04:53
  • That was it. The class referenced inside Job had a DateTime that was defaulting to DateTime.MinValue. Thanks! – L_7337 Sep 27 '15 at 05:17