Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a datareader is null or empty

I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time empty or null.

I am trying to write code that checks if this field isnull. The logic behind this is: If the field "Additional" contains text then display the info otherwise hide the field.

I have tried:

if (myReader["Additional"] != null) {     ltlAdditional.Text = "contains data"; } else {      ltlAdditional.Text = "is null"; } 

The above code gives me this error:

Exception Details: System.IndexOutOfRangeException: Additional

Any help would be greatly appreciated...


See Also:

Check for column name in a SqlDataReader object

like image 262
Jason Avatar asked Apr 18 '09 04:04

Jason


People also ask

How do I know if my Datareader has rows?

The HasRows property returns information about the current result set. If the DataTableReader contains multiple result sets, you can examine the value of the HasRows property immediately after you call the NextResult method in order to determine whether the new result set contains rows.

How do I check if a Datareader has a column?

string ColumnValue; if (dr["ColumnName"] != null) ColumnValue = dr["ColumnName"].


2 Answers

if (myReader.HasRows) //The key Word is **.HasRows**  {      ltlAdditional.Text = "Contains data";  }  else  {         ltlAdditional.Text = "Is null Or Empty";  } 
like image 22
Kemal Can ÖZÇELİK Avatar answered Sep 21 '22 18:09

Kemal Can ÖZÇELİK


if (myReader["Additional"] != DBNull.Value) {     ltlAdditional.Text = "contains data"; } else {      ltlAdditional.Text = "is null"; } 
like image 200
Robert Durgin Avatar answered Sep 22 '22 18:09

Robert Durgin