cannot set column int to be null. please use dbnull instead in datarow

Viewed 3506

I am fetching data from datarow and facing issue when there is null value in a row column. How can I change the below mentioned code to read null values??

Datarow dr_Out[j] = dr1.Field<double?>("" + Convert.ToString(columnNames[j]) + ""); 

at this line when there is null value below error occur.

cannot set column int to be null. please use dbnull instead in datarow

after adding DBNull facing this

enter image description here

1 Answers

You can do as suggested by the error:

    dr_Out[j] = DBNull.Value;
    double? myValue = dr1.Field<double?>("" + Convert.ToString(columnNames[j]) + "");

    if(myValue != null)
    {
        dr_Out[j] = myValue;
    }
Related