[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

URGENT: Printing error when printing a empty field.

Could someone please help me urgently!

I am trying to print the addresses of clients on an Access Database using VB.net.  But when one of the fields is empty I get an error.  I've tried putting


if not client("Add5") = ""
and
if not client("add5") = nothing

and cant avoid the error

any ideas?

Thanks
Bsturge
0
bsturge
Asked:
bsturge
1 Solution
 
tsayCommented:
Try checking for DBNull values.

If client("Add5") <> "" and client("Add5") <> DBNull.Value then ...

HTH
0
 
lojk.Net and Infrastructure ConsultantCommented:
I usually add a seperate function.. Reduces much typing time later...

        Public Shared Function CheckNullString(ByVal stringToCheck As Object) As String
                Dim tret As String = ""
                Try
                        tret = CStr(stringToCheck)
                Catch ex As Exception
'catch the error if you like...
                End Try
                Return tret
        End Function

then..

Console.writeline (checknullstring(client("Add5")))
0
 
SteSiCommented:
try this:

IIf(VarType(client("Add5"))= VariantType.Null, "", client("Add5"))

This will return the value if it is not null and a blank string if it is null!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bsturgeAuthor Commented:

Tsay,

Thanks for your solution.  The problem is that the value is DBNull but when I put the suggested line:

If client("Add5") <> "" and client("Add5") <> DBNull.Value then ...

I get this error:

 Operator '<>' is not defined for types 'System.Object' and 'System.DBNull'. Use 'Is' operator to compare two reference types.

0
 
tsayCommented:
Oh,

then try this:
If client("Add5") <> "" and client("Add5) Is Not DBNull.Value then ...
0
 
tsayCommented:
Ok that's also wrong so I took the effort of actually opening Visual Studio,

it should be this:

If client("Add5") <> "" and Not client("Add5) Is DBNull.Value then ...
0
 
lojk.Net and Infrastructure ConsultantCommented:
Not only has it got to work but you have to type it over and over and over and over again...

My way keeps your inline code nice and neat... (and also allows you to refactor the CheckNullString Function Later). Just for reference i also have a CheckNullNumber function that does the same but... well get you get the idea..

Thanks for the points..
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now