Solved

Returning NULL Values and how to handle it?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

16 Experts available now in Live!

Get 1:1 Help Now