1

Cant understand the reason for this bug.

The conversion of a datetime 2 data type to a datetime data type resulted in an out-of-range value.

The data type in Sql server for this column is DateTime and Not Null is selected while creating the column.

On debugging systems datetime is saved in the column exception occurs in SaveChanges.

On searching I got this

Stack Over Flow

The Conversion.. But didn't helped in my situation.

Model

public class Student
{  
    [Key]
    public int StudentId { get; set; }
    public DateTime CreatedDate { get; set; }

}

Action

public ActionResult Save(ViewModels.Student student)
{
        var model = new ViewModels.Student();
        model.CreatedDate = System.DateTime.Now;
        db.Student.Add(student);
        db.SaveChanges();
        return View(model);
}
Community
  • 1
  • 1
Dave
  • 263
  • 6
  • 23
  • 6
    Your saving `student`, not `model` and presumably `student.CreatedDate` is the default `DateTime.MinDate` (which is out of range) –  Jul 02 '16 at 12:17
  • @Stephen i tried student.CreatedDate = System.DateTime.Now then too the same error,some thing I am missing. – Dave Jul 02 '16 at 12:30
  • That should work fine if your setting it before you save `student` (assuming there are no other `DateTime` fields) –  Jul 02 '16 at 12:33
  • @Stephen Muecke it worked. – Dave Jul 02 '16 at 12:42

1 Answers1

2

SqlDateTime.MinValue is January 1, 1753 while DateTime.MinValue is January 1, 0001

You need to handle this and make sure it is higher than the minimum value when you do the save

Mikael Puusaari
  • 854
  • 1
  • 10
  • 14