• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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.
0
AmyFPA
Asked:
AmyFPA
  • 6
  • 5
  • 3
1 Solution
 
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
 
AmyFPAAuthor 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AmyFPAAuthor Commented:
Same error.
0
 
kdwoodCommented:
Hmm..I'm stumped.  I will see what I can find.  Sorry.
0
 
AmyFPAAuthor 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
 
AmyFPAAuthor 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
 
AmyFPAAuthor 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
 
AmyFPAAuthor Commented:
Great!  Thank you!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now