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...
Check for column name in a SqlDataReader object
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.
string ColumnValue; if (dr["ColumnName"] != null) ColumnValue = dr["ColumnName"].
if (myReader.HasRows) //The key Word is **.HasRows** { ltlAdditional.Text = "Contains data"; } else { ltlAdditional.Text = "Is null Or Empty"; }
if (myReader["Additional"] != DBNull.Value) { ltlAdditional.Text = "contains data"; } else { ltlAdditional.Text = "is null"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With