user1270384 user1270384 - 1 year ago 72
C# Question

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

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 Source

either convert curPrice to nullable, or use .Value property of nullable types.

If curPrice is a property of a class then

public decimal? curPrice
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download