0

I have this line of code here:

command.Parameters["@DateCompleted"].Value = items[i].DateCompleted.Equals("01/01/0001 12:00:00 AM") ? null : items[i].DateCompleted;

but I got this error:

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

What I am trying to do is not use the 01/01/0001 Date and use null because the item is null.

Additional Code:

command.Parameters.Add("@DateCompleted", System.Data.SqlDbType.DateTime);
1
  • 1
    sounds like you need to take a look at the following Nullable<T> Structure Commented Nov 27, 2015 at 20:57

3 Answers 3

6

Simply cast the null to a DateTime?. Also, assuming DateCompleted is a DateTime (it should be), then don't compare against a string, but against DateTime.MinValue instead.

command.Parameters["@DateCompleted"].Value =
    items[i].DateCompleted.Equals(DateTime.MinValue) 
        ? (DateTime?) null
        : items[i].DateCompleted;
Sign up to request clarification or add additional context in comments.

Comments

0

Use DateTime?

command.Parameters["@DateCompleted"].Value = items[i].DateCompleted.Equals("01/01/0001 12:00:00 AM") ? (DateTime?)null : items[i].DateCompleted;

1 Comment

Go with Matt's: I agree with the DateTime.MinValue approach.
0

To use the ternary operator where one of the return types is null, the other return type has to be a nullable type, which DateTime is not, being a struct. What you could do is replace the null with default(DateTime):

DateTime value = someCondition ? default(DateTime) : items[i].DateCompleted;

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.