Parsing a DBNULL value into double

Ven*_*kat 2 c# sql parsing dbnull formatexception

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

double.parse(Convert.ToString(datarow));
Run Code Online (Sandbox Code Playgroud)

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.

Ian*_*Ian 7

另一种选择是检查datarow是否DBNull

double d = datarow is DBNull ? 0 : double.Parse(Convert.ToString(datarow));
Run Code Online (Sandbox Code Playgroud)

这样,您就不需要检查DBNull.Value