1
  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' 不会在所有代码路径上返回值。在使用结果时,运行时可能会发生空引用异常。”

4

1 回答 1

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 语句中。

于 2013-09-04T05:34:24.593 回答