Syntax Problem Using IsNull

Hi,

I'm trying to handle null values in a field that are causing the following error:
     Cast from type 'DBNull' to type 'String' is not valid

I've tried variations on this:
     Me.txtTextBox.Text = IIF(IsNull(dr("FieldName")),"",dr("FieldName"))

But get build errors stating that IsNull is not declared.

How do I check for nulls?

(VB.Net 2003, SQL Server back end)

Thanks!


LVL 61
mbizupAsked:
Who is Participating?
 
fullcontactConnect With a Mentor Commented:
Tried something like this?

Me.txtTextBox.Text = "" & dr("FieldName")
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Hi Miriam

You can simulate the Access Nz function by having the below in a separate code module

Public Function Nz(vValue as Variant, vValueIfNull as Variant) as

If IsNull(vValue) then
   if IsNull(vValueIfNull) then
      Nz = 0
   else
      Nz = vValueIfNull
   end if
else
   Nz = vValue
end if

End Function

0
 
mbizupAuthor Commented:
Hi FullContact,

That works like a charm and uses a lot fewer words!

Thanks a bunch!
0
 
mbizupAuthor Commented:
Jim,
Missed your post.  Thanks for the reply too.
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.