Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 388
user1270384 Avatar asked May 09 '12 20:05

user1270384


1 Answers

inrec.curPrice = sdr.GetValueOrDefault(0m)

Since the left side (Price) does not allow for null then you cannot set its value to something that could be null. Therefore, use .GetValueOrDefault(decimal defaultValue) to return a default value when null.

like image 177
LazyDog Avatar answered Sep 18 '22 07:09

LazyDog