Handling a null value
-
I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like:
dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} }
Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks. -
I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like:
dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} }
Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks. -
I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like:
dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} }
Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks.You can handle that as below: if (dr["fieldname"] != null) { //do something }
Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!
-
You can handle that as below: if (dr["fieldname"] != null) { //do something }
Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!