Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

StackOverflow https://stackoverflow.com/questions/22331210

  •  13-06-2023
  •  | 
  •  

Question

I think I am suppose to use something like this but I am not sure how to modify it to work for DateTime because upon doing so it tells me DateTime can not be null. So how can I change this or is there a better method to resolve this issue? Thank you in advance.

What I'm trying to set

startDateTime = RadDateTimePickerBegin.SelectedDate;

What I think it should look similar to

RadDateTimePickerBegin.SelectedDate == What goes here(string.Empty) ? (object)DBNull.Value : RadDateTimePickerBegin.SelectedDate);
Was it helpful?

Solution

It looks like you're using a RadDateTimePicker control, in which case the SelectedDate property is not a DateTime value. It's a Nullable<DateTime>. That means you should probably do this:

 startDateTime = RadDateTimePickerBegin.SelectedDate ?? default(DateTime);

Just be careful: if no date was selected, you'll end up with a January 1st, 0001 as your date, and not DBNull like you expect.

Moreover, I see you casting to Object here, which tells me you're probably about to make a big mistake. It looks like the startDateTime is going to be assigned to the value of an SqlParameter object, and in order for this variable to be able to hold items of both the DateTime and DBNull types it must have a type of Object. If you later create your SqlParameter using the AddWithValue() method, the method will likely not guess the parameter type correctly, because all it will see is the Object type. When this happens you can get severe performance penalties, as the type mismatch can force per-row conversions in the database or prevent good use of indexes.

If you're really set on your current path, you could do this instead:

 startDateTime = (RadDateTimePickerBegin.SelectedDate.HasValue)?
           (Object)RadDateTimePickerBegin.SelectedDate.Value
          :(Object)DBNull.Value;

But again: I strongly advise against it.

OTHER TIPS

It's not clear where DBNull comes into the picture, but if you're trying to set a non-nullable DateTime to the value of a nullable DateTime then you'll need to supply a default value in the case of a null. You can use the null coalescing operator for this:

someNonNullableDateTime = someNullableDateTime ?? DateTime.MinValue;

The idea is that someNullableDateTime might contain a null value at runtime, and someNonNullableDateTime can't be assigned a null value. Since the compiler can't enforce this, it throws a compilation error. So you give the runtime code an option to either use the value or, if there's no value, use a default.

If you're okay with default(DateTime) if the nullable is null then:

DateTime startDateTime = RadDateTimePickerBegin.SelectedDate.GetValueOrDefault();

Alternatively if you want another value e.g. DateTime.MaxValue when the nullable is null then you can pass in your default value:

DateTime startDateTime = nullableDate.GetValueOrDefault(DateTime.MaxValue);

DateTime can't be null. It's default is DateTime.MinValue.

if (YourDate.HasValue)
{
   RadDateTimePickerBegin.SelectedDate =  YourDate;
}

or

adDateTimePickerBegin.SelectedDate= YourDate?? DateTime.MinValue;

Assuming startDateTime is a DateTime and RadDateTimePickerBegin.SelectedDate is a DateTime?:

startDateTime = RadDateTimePickerBegin.SelectedDate.HasValue
    ? RadDateTimePickerBegin.SelectedDate.Value
    : DateTime.MinValue;

Alternatively, make startDateTime nullable.

DateTime? selected = RadDateTimePickerBegin.SelectedDate;

if(selected.HasValue)
{
  startDateTime = selected.Value;
}

You need to make sure there actually is a selected datetime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top