Solved

URGENT: Printing error when printing a empty field.

Posted on 2006-07-04
7
182 Views
Last Modified: 2010-04-23
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
Comment
Question by:bsturge
7 Comments
 
LVL 5

Expert Comment

by:tsay
ID: 17035917
Try checking for DBNull values.

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

HTH
0
 
LVL 9

Accepted Solution

by:
lojk earned 500 total points
ID: 17036006
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
 
LVL 1

Expert Comment

by:SteSi
ID: 17036248
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:bsturge
ID: 17036261

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
 
LVL 5

Expert Comment

by:tsay
ID: 17036272
Oh,

then try this:
If client("Add5") <> "" and client("Add5) Is Not DBNull.Value then ...
0
 
LVL 5

Expert Comment

by:tsay
ID: 17036291
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
 
LVL 9

Expert Comment

by:lojk
ID: 17037162
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question