I have a nullable Datetime property
public DateTime? BirthDate { get; set; }
While saving it to Database - datetime field want to check for null
If it is BirthDate is null save Null in Database or Datetime value.
var birthdate = ( BirthDate == null) ? DBNull.Value : Convert.ToDateTime(BirthDate)
Why it is throwing conversion error?
Please suggest me
Edited
foreach (var subject in Subjects) { dbContext.Subjects.Add(new Subjects() { BirthDate = (subject.BirthDate == null) ? DBNull.Value : Convert.ToDateTime(subject.BirthDate) }); }
birthdateas eitherDBNullorDateTime. Those types are not compatible. What framework are you using to save to the database? It's very unlikely you will need to manage this yourself...