user1270384 user1270384 - 2 months ago 9
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

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;
}