1

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)                           
                    });
                }
3
  • 3
    Because you're trying to define birthdate as either DBNull or DateTime. 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... Commented Jan 14, 2016 at 23:21
  • I am trying it in .net 4.5 and saving it in sql server 2012. I am trying to do this to avoid "0001-01-01" in Database. In C#, if I try to convert null value to Datetime it will give you default date. So now I am checking it before saving. If null pass DBNull or Datetime. Commented Jan 14, 2016 at 23:32
  • are you using Entity Framework or plain ADO.Net? Commented Jan 14, 2016 at 23:46

3 Answers 3

4

A DateTime and DBNull aren't comparable objects. Both sides of the : need to be of the same type.

Try casting to object

var birthdate = ( BirthDate == null) ? (object)DBNull.Value : (object)Convert.ToDateTime(BirthDate);

Addditionally you can use !BirthDate.HasValue (instead of BirthDate == null).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for suggestion but when I tried this getting error as "Cannot implicitly convert type 'object' to 'System.DateTime?'. An explicit conversion exists (are you missing a cast?)"
@user1893874 Seems to work ok for me - dotnetfiddle.net/o3dany - did you (object) both sides?
This is my code "var birthdate = (BirthDate == null) ? (object)DBNull.Value : (object)Convert.ToDateTime(BirthDate)" getting same error
@user1893874 is there anything I've missed in that basic fiddle I put together?
1

Just use the following code if you are using plain ADO.Net to save to database.

 if(BirthDate.HasValue)
 {
    //say you have a SqlCommand object called cmd
    cmd.Parameters.Add("@birthDate", BirthDate.Value);
 }
  else  
 {
 //say you have a SqlCommand object called cmd
    cmd.Parameters.Add("@birthDate",null);
}

However, if you are using Entity Framework then let EF handle what to save if the value of BirthDate is null, and you should not be interfering in that by deciding whether to save DBNull.Value or not. So just get rid of the var birthDate logic and let EF do it's job.

UPDATE 1

change the line below to the one following it.

BirthDate = (subject.BirthDate == null) ? DBNull.Value : Convert.ToDateTime(subject.BirthDate);

Change to

BirthDate = subject.BirthDate;

7 Comments

Can you post your code that is actually updating the database? There is no need to use var birthDate. Just the set the entity properties and call SaveChanges method.
Let EF handle what to save if the value of BirthDate is null, and you should not be interfering in that by deciding whether to save DBNull.Value or not.
Why are you trying set it to DBNull.Value? EF takes care of that, so I am really confused what you are trying to do.
Please find the edited code above where BirthDate is System.DateTime?
Just use BirthDate = subject.BirthDate; in your loop and let EF do it`s job. You don't need to bother about setting value to DBNull.Value in EF. That is why we use frameworks like EF since they handle a lot of these tasks behind-the-scene.
|
0

I just answered a similar question about a string, but the same solution should work for a nullable DateTime, though maybe use .HasValue instead of == null:

What in the DBNull

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.