Nullable DateTime conversion [duplicate]

You might want to do it like this:

DateTime? lastPostDate =  (DateTime?)(reader.IsDbNull(3) ? null : reader[3]); 

The problem you are having is that the ternary operator wants a viable cast between the left and right sides. And null can’t be cast to DateTime.

Note the above works because both sides of the ternary are object’s. The object is explicitly cast to DateTime? which works: as long as reader[3] is in fact a date.

Leave a Comment

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