Search code examples
c#sqlparsingdbnullformatexception

Parsing a DBNULL value into double


I use the following line to convert the datarow value into double.

double.parse(Convert.ToString(datarow));

If the datarow is DBNULL, I am getting the following exception:

'double.Parse(Convert.ToString(data))' threw an exception of type 'System.FormatException'

How to handle this one without using tryparse.


Solution

  • Another alternative would be to check if the datarow is DBNull:

    double d = datarow is DBNull ? 0 : double.Parse(Convert.ToString(datarow));
    

    This way, you do not need to check for DBNull.Value