Assigning `null` value to Nullable with single line ‘if’

You need to cast null to DateTime?:

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value 
    ? (DateTime?)null 
    : Convert.ToDateTime(dr["AppDate"]); 

This is because of the way the compiler determines the resulting type of the conditional operator; the behavior is by design:

Either the type of first_expression and second_expression must be the
same, or an implicit conversion must exist from one type to the other.

Since null by itself is of null type and thus there is no conversion from or to it, you need to help the compiler by casting.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)