The
(DateTime?)null
option is close - the code will compile, but the parameter won't be passed to the query. If the parameter had a default value of
Null
, the code would work; otherwise, you'll get a missing parameter error.
In order to pass
Null
as the parameter value, you have to use
DBNull.Value
. However, if your ternary expression has return values of
DBNull
and
DateTime
, the compiler won't be able to determine the return type of the expression.
The trick is to force the return type to be
object
, by explicitly boxing the
DateTime
value.
(The overhead of boxing the value is irrelevant, since it's being passed to a method parameter of type object
, so it would need to be boxed anyway.)
This should work:
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? DBNull.Value : (object)Convert.ToDateTime(review_date));