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

12,563

You did not set a value for SolvedDate, so the default value which is 01\01\01 will be sent to the database, which causes the error.

If you want to have SolvedDate with null value, you should define it as Nullable date(DateTime?) because DateTime is a struct and cannot be set to null.

Share:
12,563
furkan
Author by

furkan

Updated on June 04, 2022

Comments

  • furkan
    furkan about 2 years

    I am trying to insert data from another database and this code was working until today. Now it doesn't anymore.

    Here is the code:

    var itemMember = Db.Members.FirstOrDefault(p => p.Id == CurrentMember.Id);
    var itemSelectedName = Db.Topics.FirstOrDefault(p => p.Id == TopicId);
    var itemMap = Db.Reeves.FirstOrDefault(p=> p.Member.Id == CurrentMember.Id);
    var itemReeve = Db.Reeves.FirstOrDefault(p => p.Member.Id == CurrentMember.Id);
    var item = new Problem
          {
              Name = input.Name,
              Text = input.Info,
              Topic = itemSelectedName,
              WhoIs = itemMember,
              Province = itemMap.Province,
              District = itemMap.District,
              Town = itemMap.Town,
              Reeve=itemReeve,
              Created=DateTime.Now,
              Solved = false,
              Read=false
          };
    
    Db.Problems.Add(item);
    Db.SaveChanges();
    

    There is nothing null in the code and it throws an error:

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

    What should I do ?

    Also this error shows up now:

    System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.

    but as I said it was working till today :/

    Here is view model:

    [Display(Name = "Başlık"), Required, StringLength(50)]
    public string Name { get; set; }
    
    [Display(Name = "Bilgi")]
    [UIHint("TinyMCE")]
    public string Info { get; set; }
    
    [Required(ErrorMessage = "Gerekli Alan")]
    [Display(Name = "Konu")]
    [UIHint("DropDownList")]
    [AdditionalMetadata("DataController", "Problem")]
    [AdditionalMetadata("DataAction", "ProblemDrop")]
    public int? TopicId { get; set; }
    

    Here is model

    public class Problem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
        public short Sira { get; set; }
        public DateTime Created { get; set; }
        public virtual Topic Topic { get; set; }
        public virtual Member WhoIs { get; set; }
        public virtual Province Province { get; set; }
        public virtual Town Town { get; set; }
        public virtual District District { get; set; }
        public virtual Reeve Reeve { get; set; }
        public Boolean Solved { get; set; }
        public DateTime SolvedDate { get; set; }
        public virtual Member WhoSolved { get; set; }
        public Boolean Read { get; set; }
    }
    

    Solved: Date is null could it be the problem ?