Search code examples
c#castingdbnullexplicit-conversion

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.


Solution

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

    If curPrice is a property of a class then

    public decimal? curPrice
    {
       get;
       set;
    }