为什么我的VB.NET功能会显示警告

时间:2013-09-04 04:20:25

标签: sql-server vb.net-2010

  Public Function Delete_GST_Entry(ByVal GE_ID As Integer) As DataTable
    Try
        Using con As New SqlConnection(DF_Class.GetConnectionString())
            Dim ds As DataTable = New DataTable
            con.Open()
            Dim command As SqlCommand = New SqlCommand("Delete_GSTEntry", con)
            command.Parameters.AddWithValue("GE_ID", GE_ID)
            command.CommandType = CommandType.StoredProcedure
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(command)
            Dim table As DataTable = New DataTable
            adapter.Fill(ds)
            Return ds 
            con.Close()
        End Using
    Catch ex As SqlException
        MessageBox.Show(ex.Message, "Data Input Error")

    End Try

End Function

我收到警告说NULL reference could occur。我在做什么错误?

警告:

  

“函数'Delete_GST_Entry'不会在所有代码路径上返回值。   结果为运行时,可能会发生空引用异常   用过的。 “

1 个答案:

答案 0 :(得分:0)

方法的所有出口点(返回路径)必须返回方法签名指定的内容。

像这样重写:

Public Function Delete_GST_Entry(ByVal GE_ID As Integer) As DataTable
   Dim dt As DataTable = New DataTable

   Try
        Using con As New SqlConnection(DF_Class.GetConnectionString())
             con.Open()
            Dim command As SqlCommand = New SqlCommand("Delete_GSTEntry", con)
            command.Parameters.AddWithValue("GE_ID", GE_ID)
            command.CommandType = CommandType.StoredProcedure
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(command)
            adapter.Fill(dt)
            con.Close()
        End Using
    Catch ex As SqlException
        MessageBox.Show(ex.Message, "Data Input Error")
    End Try

    Return dt    ' Note will be empty if stored proc call fails...
End Function

理想情况下,您应该将连接包装在using语句中。