Reputation: 41
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
Upvotes: 3
Views: 10164
Reputation: 4868
I always define my classes to accept nulls.
eg:
public DateTime? updatedTime { get; set; }
and compare for nulls in the code.
dr["updatedTime"] = (e.updatedTime is null) ? (object)DBNull.Value : (object)e.updatedTime.Value;
this way it forces a "proper" null suitable for DBs.
Upvotes: 1
Reputation: 1186
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;
}
Upvotes: 4