Solved

Returning NULL Values and how to handle it?

Posted on 2014-03-06
4
190 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 74

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 27

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now