How to avoid Null-value error with empty string field?

I'm using data retrieved with SQLDataReader.  Some values are Null (such as second address line), so I get the SQLNullValueException error.  Fair enough.  But how do I head that off?

Among other things, I've tried:
If Not String.IsNullOrEmpty(datareader.GetString(6))
If Not Len(datareader.GetString(6)) = 0
If Not IsDBNull(datareader.GetString(6))

Everything I try still throws this error.
AmyAsked:
Who is Participating?
 
Snarf0001Connect With a Mentor Commented:
As i said, just change the datareader.IsDBNull to just an integer:

datareader = cmd.ExecuteReader
If datareader.HasRows Then
    While datareader.Read
        If Not (datareader.IsDBNull(6)) Then
            strText &= " " & datareader.GetString(6)
        End If
      End While
End If
0
 
kdwoodCommented:
How about something like this:

IIF(IsDBNull(datareader.GetString(6)) = True, '', datareader.GetString(6))

Give that a try, I think it will work.

Regards,

Keith
0
 
AmyAuthor Commented:
I altered it a little bit to put the results where I need them:
strTextLine &= IIf(IsDBNull(datareader.GetString(6)) = True, "", " " & datareader.GetString(6))

But I get the same error.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
kdwoodCommented:
Strange....try this:

If Not (datareader.IsDBNull(datareader.GetString(6)) Then

0
 
AmyAuthor Commented:
Same error.
0
 
kdwoodCommented:
Hmm..I'm stumped.  I will see what I can find.  Sorry.
0
 
AmyAuthor Commented:
I don't know if this helps, but this is what I get in the Immediate window:

? datareader.GetValue(6)
{System.DBNull}
    System.DBNull: {System.DBNull}
0
 
Snarf0001Commented:
Use the GetValue in your initial dbnull test.

If Not (datareader.IsDBNull(datareader.GetValue(6)) Then

When you called GetString in the first one:
If Not (datareader.IsDBNull(datareader.GetString(6)) Then

The system is already trying to convert the dbnull value to a string and failing before it does the comparison.
0
 
kdwoodCommented:
Ah...yes I missed that.  I think you can use GetOrdinal as well.

Good catch Snarf!
0
 
AmyAuthor Commented:
Now I get a different error:  InvalidCastException
0
 
Snarf0001Commented:
Sorry, just use:

datareader.IsDBNull(6)

The reader's IsDBNull is just expecting an ordinal, and will retrive the value itself.
Alternatively, you could use:

Not IsDBNull(datareader.GetValue(6))
0
 
kdwoodCommented:
You will need to post more of your code so we can see what's going on.  
0
 
AmyAuthor Commented:
datareader is a SQLDataReader.

I think these are all the lines that really deal with this.  The rest is merely handling other fields, rearranging strings, and writing to files.
datareader = cmd.ExecuteReader
If datareader.HasRows Then
    While datareader.Read
        If Not (datareader.IsDBNull(datareader.GetValue(6))) Then
            strText &= " " & datareader.GetString(6)
        End If
	End While
End If

Open in new window

0
 
AmyAuthor Commented:
Great!  Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.