handle null value from query

Posted on 2011-10-25
Last Modified: 2012-05-12
"SELECT dbo.wmFgAvailDateAtDestFn(fg.fg_num,fg.region,fg.manuf_date) FROM wmFg AS fg (NOLOCK)WHERE fg.fg_num ='" & fgnum & "'"
is returning null so my code doesn't like it.  
Question by:Wigging
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    Check for DBNull in your code, or you can use the IsNull SQL function to provide some default value as a return from your query.

    Author Comment

    This is what i am trying to do but is not working

      Function validatefgdate(ByVal fgnum As String) As String
                strSQLclass = "SELECT dbo.wmFgAvailDateAtDestFn(fg.fg_num,fg.region,fg.manuf_date) FROM wmFg AS fg (NOLOCK)WHERE fg.fg_num ='" & fgnum & "'"
                Dim cmd As New SqlCommand(strSQLclass, connUSSL4)
                If cmd.ExecuteScalar() Is DBNull.Value Then
                    validatefgdate = "1/1/1999"
                    validatefgdate = cmd.ExecuteScalar().ToString()
                End If

            Catch ex As Exception
                sbody = "strSQLclass=" & strSQLclass & ", custnum=" & fgnum
                handleError("validatefgdate", ex.Message, sbody)
                If connUSSL4.State = ConnectionState.Open Then connUSSL4.Close()
            End Try
        End Function
    LVL 74

    Accepted Solution

    Ah. I think with ExecuteScalar you would check for Nothing, not DBNull.Value.

    If cmd.ExecuteScalar() IsNot Nothing Then

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Sorry, use Is, not IsNot for your particular scenario  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now