Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used

I'm getting this error:

Function 'getkey' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

to the following code:

 Public Function getkey(ByVal id As String)
            Dim cmd As SqlCommand
            Try
                cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@id", id)
                Dim r As SqlDataReader = cmd.ExecuteReader()
                If r.HasRows Then
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            Finally
                ' If Not cn Is Nothing Then cn.Close()
            End Try
        End Function

I tried all possible solutions and they didn't work. Any help would be appreciated.

like image 767
HelpASisterOut Avatar asked Sep 10 '13 14:09

HelpASisterOut


1 Answers

The Catch block doesn't return a value. Change it to where it returns a value, like so:

Public Function getkey(ByVal id As String)
        Dim cmd As SqlCommand
        Try
            cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@id", id)
            Dim r As SqlDataReader = cmd.ExecuteReader()
            If r.HasRows Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        Finally
            ' If Not cn Is Nothing Then cn.Close()
        End Try
    End Function
like image 189
Douglas Barbin Avatar answered Sep 30 '22 13:09

Douglas Barbin