4

Some tables I am dealing with have null values and are throwing errors. So far ive tried a few solutions to deal with the nulls with no success.

Here are the code samples from my efforts so far;

If (r("datemodified").Equals(DBNull.Value)) Then
                datemodified = String.Empty
            Else
                datemodified = (r("datemodified"))
            End If

and;

If r.HasRows Then
                datemodified = (r("datemodified"))
            Else
                datemodified = String.Empty
            End If

and;

 If r("datemodified") = Nothing Then
                datemodified = String.Empty
            Else
                datemodified = (r("datemodified"))
            End If

and;

If r.IsDBNull("datemodified") Then
                datemodified = String.Empty
            Else
                datemodified = (r("datemodified"))

and via sql;

Select isnull(datemodified, '')

The end result is an IndexOutOfRangeException.

here is the sql;

select datemodified, maintainedby, email, hitcount from grouping where id = @footid

ps, i have ran the query and it is working ok (i.e all the cols exist)

3
  • datemodified (if i comment that line out, then it immediately throws another on the next line which is maintainedby) The reason for the nulls is when people update pages they do not always fill out the details. Personally I would disallow nulls, but the db is legacy. Commented Feb 9, 2010 at 10:32
  • Can you add your SQL that you are using to your question? Commented Feb 9, 2010 at 10:44
  • I asked about your SQL because as Kosala pointed out you would get an IndexOutOfRange Exception if you don't alias the column. Commented Feb 9, 2010 at 11:06

2 Answers 2

5

To handle the null value in the code you can use the IsDBNull method:

Dim index As Integer = r.GetOrdinal("datemodified")
If r.IsDBNull(index) Then
   datemodified = String.Empty
Else
   datemodified = r(index)
End If

To handle the null value in the SQL you have to give the field a name, to be able to access it by name in the data reader:

select datemodified = isnull(datemodified, '')
Sign up to request clarification or add additional context in comments.

2 Comments

THanks for the assistance. After trying both of these methods, the problem is still occuring.
Then the problem is somewhere else... Do you call Read before trying to get data from the data reader? Check the value of r.FieldCount to see if you actually have any columns in the result, and r.GetOrdinal("datemodified") to see if the field is one of them.
2

The IndexOutofRangeException is becoz of the Column you are trying to access does not exists in the result set.

When you are accessing the Ordinal, or the Column via SqlDataReader, you must specify the Column Index, or the Column Name. In your scenario, you need to specify an Alias for the SQL Column returning.

SELECT ISNULL(datemodified, '') AS [datemodified]

1 Comment

Thanks for the help. After editing i have; select ISNULL(datemodified, '') AS datemodified, maintainedby, email, hitcount from grouping where id = @footid . Unfortunately the exception is still happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.