Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 617
Laziale Avatar asked Jan 29 '13 19:01

Laziale


1 Answers

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

like image 113
Justin Avatar answered Sep 22 '22 19:09

Justin