views:

5534

answers:

6

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

A: 

First of all, you probably want to check for a DBNull not a regular Null.

Or you could look at the IsDBNull method

Joe Philllips
In this database table there are 15 rows. 14 of them return data however the 15th ("Additional") doesn;t always contain data. How would .HasRows work with the valid rows?
Jason
+3  A: 
if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}
Robert Durgin
Thanks for your response Robert,I tried your suggested code however it still gives me "System.IndexOutOfRangeException: Additional" because in this case "Additional" is null
Jason
+1  A: 

In addition to the suggestions given, you can do this directly from your query like this -

SELECT ISNULL([Additional], -1) AS [Additional]

This way you can write the condition to check whether the field value is < 0 or >= 0.

Kirtan
I usually follow this pattern since NULL represents nothing in a DB.
Saif Khan
A: 

AMG - Sorry all, was having a blond moment. The field "Additional" was added to the database after I had initially designed the database.

I updated all my code to use this new field, however I forgot to update the actual datareader code that was making the call to select the database fields, therefore it wasn't calling "Additional"

Jason
+1  A: 

@Joe Philllips

SQlDataReader.IsDBNull(int index) requires the ordinal number of the column. Is there a way to check for nulls using Column Name, and not it's Ordinal Number?

Shiva
i guess I could use if (readerObject["ColumnName"] != DBNull.Value)if I wanted to use column name instead of the ordinal number. Although I am not sure if there is a performance improvement (or degradation) between this approach and the IsDBNull(columnOrdinalNumber) approach.-Shivahttp://mycodetrip.com/
Shiva
A: 

I haven't used DataReaders for 3+ years, so I wanted to confirm my memory and found this. Anyway, for anyone who happens upon this post like I did and wants a method to test IsDBNull using the column name instead of ordinal number, and you are using VS 2008+ (& .NET 3.5 I think), you can write an extension method so that you can pass the column name in:

public static class DataReaderExtensions
{
    public static bool IsDBNull( this IDataReader dataReader, string columnName )
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

Kevin

Kevin Nelson