本文介绍了函数不会在所有代码路径上返回值.使用结果时,运行时可能会出现空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误:
更改为以下代码:
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.
推荐答案
Catch
块不返回值.将其更改为返回值的位置,例如:
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
这篇关于函数不会在所有代码路径上返回值.使用结果时,运行时可能会出现空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!