Solved

Returning NULL Values and how to handle it?

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

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!

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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