How can I convert DBNULL or Empty string to Double in one line of code?

87 views Asked by At

I wonder if there is a inline/single line way to convert a DBNULL or Empty String value to double or int in C#. I'm using Visual Studio 2008.

My example table

Id Value
1 NULL

This null value column might contain empty strings in some tables. Precisely, I want to convert empty string or NULL to Double.

I'm using ADODB.Recordset to retrieve data from the table.

I tried this line of code

Double v = Double.Parse((MyDataSet.Fields["Amount"].Value??"0").ToString());

This converts the NULL to double but if the result value contains an empty string, it shows an error

input string was not in a correct format

Now what I'm doing is this using ternary operator, I believe this is really bad.

Pseudo:
If value=="" then
  Parse double value
Else if Value==Null Then
  Parse Zero(0)
Else
  Parse Actual column value to variable
End if

0

There are 0 answers