if (dtable.Rows[0]["VAT"] != System.DBNull.Value) //Exception here while it returns DBNULL dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]); else dreturns = 0;
Error : System.NullReferenceException: Object reference not set to an instance of an object.
dtable.Rows.Count > 0
Count
dreturns = 0; if (dtable.Rows.Count > 0) { if (dtable.Rows[0]["VAT"] != System.DBNull.Value) dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]); }
dreturns = 0; if ( dtable != null && dtable.Rows.Count > 0 ) { if (dtable.Rows[0]["VAT"] != System.DBNull.Value) dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]); }
if (!DBNull.Value.Equals(dtable.Rows[0]"VAT"]) dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]); else dreturns = 0;
Try comparing the value of the column to the DBNull.Value value to filter and manage null values in whatever way you see fit.
foreach(DataRow row in dtable.Rows) { object value = row["VAT"]; if (value == DBNull.Value) // do something else // do something else }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)