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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
kdwoodCommented:
Strange....try this:

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

0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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
Snarf0001Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmyAuthor Commented:
Great!  Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.