Solved

Returning NULL Values and how to handle it?

Posted on 2014-03-06
4
192 Views
Last Modified: 2014-03-07
I know this is a Database 101 quesiton, but when I return a recordset from a DB Table and a column is NULL, what is the best way to handle it before loading the value into a control, such as a textbox?

I was thinking about just writing a routine like this:

    Public Function CheckForNULLs(ByVal fld As Object) As String
        Try
            If IsDBNull(fld) Then
                Return ""
            End If

        Catch ex As Exception
            EH.ErrorMessage = "CheckForNULLs() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Function

Open in new window

Would this work?
0
Comment
Question by:BlakeMcKenna
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39910574
The "best way to handle it" is going to depend on your business need and your code structure. You may have a convention whereby you use null as a sentinel value; or you may want to set the property to some default value if it comes back from the DB as null.
0
 

Author Comment

by:BlakeMcKenna
ID: 39910684
In the case that I am using it for, it will always load the data into a variable be it string or numeric...
0
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 39911531
Public Shared Function GetDBString(ByVal o As Object, Optional ByVal strDBNull As String = "") As String
    If o Is Nothing Then Return strDBNull
    If IsDBNull(o) Then Return strDBNull Else Return o.ToString
End Function

Open in new window


Using:
TextBox1.Text = GetDBString(myDataRow(fieldName), "Missing")

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39912542
Thanks...that worked!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…

792 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