handle null value from query

"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.  
Who is Participating?
käµfm³d 👽Commented:
Ah. I think with ExecuteScalar you would check for Nothing, not DBNull.Value.

If cmd.ExecuteScalar() IsNot Nothing Then

Open in new window

käµfm³d 👽Commented:
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.
WiggingAuthor Commented:
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
käµfm³d 👽Commented:
Sorry, use Is, not IsNot for your particular scenario  = )
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.