Operator '??' cannot be applied to operands of type 'System.DateTime'

20,798

Solution 1

?? is the null-coalescing operator.
It doesn't make sense to apply it to a value that cannot be null.

Solution 2

The nullcoalescing operator cannot be applied by default on a type that is inherently non-nullable like DateTime. If you wish to use it anyway, you'll have to foresee using DateTime as a nullable type, using e.g. DateTime? dt;

Solution 3

Your r.ServDate property needs to be nullable:

public DateTime? ServDate { get; set; }

DateTime by default is not nullable

Solution 4

You can either change the type to nullable as suggested in other answers, or add a helper method like this to your code:

private bool IsDefault<T>(T value)
{
    if (value == null)
        return false;
    return value.Equals(default(T));
}

Then change the code to this:

myIfxCmd.Parameters[1].Value = IsDefault<DateTime>(r.ServDate) ? (object)DBNull.Value : (object)r.ServDate;

Solution 5

DateTime is a ValueType. Value type aren't nullable - they always carry some value.

This is the same for integers, doubles, etc.

The Operator you are using checks if the value is null, so it isn't necessary.

However, you might consider checking the value against a base value, like Min. You have actually to consider what is the default value of this variable, if that is acceptable, and then check against it.

Again, consider an int. You don't check it against null, but you may check it against zero or negatives.

Share:
20,798
Anyname Donotcare
Author by

Anyname Donotcare

Updated on October 29, 2020

Comments

  • Anyname Donotcare
    Anyname Donotcare over 3 years

    I get the following error :

    Operator '??' cannot be applied to operands of type 'System.DateTime'
    

     foreach (EndServReward r in reward)
                                {
                                    if (con.State == ConnectionState.Closed)
                                    {
                                        con.Open();
                                    }
                                    myIfxCmd.Parameters[0].Value = r.EmpNum ;
                                    myIfxCmd.Parameters[1].Value = (r.ServDate) ?? DBNull.Value;
                                }
    

    where reward is List<EndServReward> reward,why this happens ,and how to fix it ?

  • Anyname Donotcare
    Anyname Donotcare over 10 years
    Why r.ServDate can't be null ?!! i want to check if those are empty properties or not set (filled) with values
  • Hanky Panky
    Hanky Panky over 10 years
    @just_name This is why it cant be null : msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx
  • Anyname Donotcare
    Anyname Donotcare over 10 years
    then what if i not set those properties and insert in the db ,Does this make any errors ?
  • Corak
    Corak over 10 years
    DateTime is a struct which is guaranteed to be initialized with all zeros. All zeros equals DateTime.MinValue (0000-01-01) which is what will be inserted in the database.
  • Hans Kesting
    Hans Kesting over 10 years
    @Corak - SqlServer will not accept a year before 1753 in a datetime column. It will reject a DateTime.MinValue (0001-01-01)
  • Corak
    Corak over 10 years
    @HansKesting - You're right, of course. I'm too used to work with datetime2, I guess. So DateTime.MinValue is "what will be attempted to be inserted in the database."