Cannot implicitly convert type 'decimal?' to 'decimal'.

user1270384 picture user1270384 · May 9, 2012 · Viewed 35.1k times · Source

sdr is my sqldatareader and I want to check that the curPrice value which is of type decimal is null.

inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7);

This is the error message I am getting:

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

Where am I going wrong, please someone tell me.

Answer

ravuya picture ravuya · May 9, 2012

decimal? indicates that it's a nullable decimal; you have to use the Value property to get the actual value (if it exists, determined via HasValue).

I'm assuming curPrice is a non-nullable decimal, in which case you need to also figure out a better value to return than null from the true side of your ternary operator.