DBNull
can't be cast or parsed to double
(or int
, decimal
, etc), so you have to check if datarow
is DBNull
before trying to parse it. It should be a oneliner using the ternary operator:
doubleValue = datarow == DBNull.Value ? 0.0 : double.Parse(Convert.ToString(datarow));