Parsing a DBNULL value into double

前端 未结 3 1782
后悔当初
后悔当初 2021-01-25 06:17

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

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

If the datarow

3条回答
  •  臣服心动
    2021-01-25 06:51

    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));
    

提交回复
热议问题