Search code examples
c#asp.netdecimal

Check if decimal value is null


I would like to check if the decimal number is NULL or it has some value, since the value is assigned from database in class object:

public decimal myDecimal{ get; set; }

and then I have

myDecimal = Convert.ToDecimal(rdrSelect[23].ToString());

I am trying:

if (rdrSelect[23] != DBNull.Value)
                    {
                        myDecimal = Convert.ToDecimal(rdrSelect[23].ToString());
                    }

But I am getting this:

the result of the expression is always 'true' since a value of type 'decimal' is never equal to null

How can I check if that decimal number has some value?


Solution

  • A decimal will always have some default value. If you need to have a nullable type decimal, you can use decimal?. Then you can do myDecimal.HasValue