• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

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.  
0
Wigging
Asked:
Wigging
  • 3
2 Solutions
 
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.
0
 
WiggingAuthor Commented:
This is what i am trying to do but is not working

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


        Catch ex As Exception
            sbody = "strSQLclass=" & strSQLclass & ", custnum=" & fgnum
            handleError("validatefgdate", ex.Message, sbody)
        Finally
            If connUSSL4.State = ConnectionState.Open Then connUSSL4.Close()
        End Try
    End Function
0
 
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

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

Featured Post

Industry Leaders: 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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now