2

I have a DateTime value that I will like to save to the database. The column allows null in the database. When I try to run the code below I get this error message.

Type of conditional expression cannot be determined because there is no implicit conversion between system.dbnull and system.datetime

How can I store null or empty value for a date column?

public class ExcelData
{
  public DateTime? DateValue1 { get; set; }
}

DateValue1 = col28Value == null ? DBNull.Value : Convert.ToDateTime(col28Value)
5
  • 2
    you dont need to set DbNull to parameter.Value, just set parameter.Value = null that should be enough Commented Jul 12, 2013 at 20:30
  • How? can you provide some code please? Commented Jul 12, 2013 at 20:31
  • he means instead of DbNull.Value, use null Commented Jul 12, 2013 at 20:32
  • What type is col28Value? Commented Jul 12, 2013 at 20:33
  • If the column is nullable on the db DateValue1 = null should do it Commented Jul 12, 2013 at 20:34

4 Answers 4

5

It's telling you that you can't declare a Nullable<DateTime> and assign it a DBNull.Value which is not the same as null

Your code should simply be:

DateValue1 = col28Value ==null? (DateTime?)null: Convert.ToDateTime(col28Value)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes; this is exactly what I was describing.
@SLaks: Ah yes, the whole "C# can't look at the output type when resolving types" annoyance.
Probably how I'd wind up doing it, but wow so ugly... Almost better to stick with if/else in this case.
3

The conditional operator requires that one of its types be convertible to the other.
It will not try to find the closest common base type.

Since DBNull and DateTime have nothing in common, it fails.

You need to cast either side to object.


However, that won't work either.
DBNull.Value is a special object used for certain APIs in System.Data.
It has nothing to do with nullable types.

Instead, you need use null itself.
Since null is not convertible to DateTime, you will also need to cast it to DateTime?. Alternatively, you can write new DateTime?(), which will create an empty (null) Nullable<DateTime> value.

1 Comment

A working example would make your answer complete, talking around nullables just makes it more complicated.
0

Work with SqlParameter and not their value like so..

SqlParameter sp = col28Value == null ? new SqlParameter("@whatever", DBNull.Value) : new SqlParameter("@whatever", col28Value);

1 Comment

This is unnecessarily verbose, unnecessary, and exactly the opposite of the question.
0

when you do this:

DateValue1 = col28Value == null ? DBNull.Value : Convert.ToDateTime(col28Value)

you may be doing :

DateValue1 = DbNull.Value;

which is not possible. So replace DbNull.Value with null

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.