我正在尝试调用填充数据表的函数,但它不起作用而且我迷路了。我收到一个功能错误。函数不会在所有代码路径上返回值。这是我的代码,请帮助谢谢。
Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
strPartNumber = txtPartNumber.Text
Dim CT As DataTable = Me.GetCompanyInfo(strPartNumber, "None")
dvCommon.DataSource = CT
dvCommon.DataBind()
End Sub
Private Function GetCompanyInfo(ByRef PartNumber As String, ByRef Version As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("spCommonInfo")
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PartNumber", PartNumber)
cmd.Parameters.AddWithValue("@Version", Version)
Using sda As New SqlDataAdapter(cmd)
Dim CT As New DataTable()
sda.Fill(CT)
End Using
End Using
End Using
End Function
答案 0 :(得分:0)
据我所知,您的方法GetCompanyInfo
会将数据表作为As DataTable
返回,但在代码中没有返回的地方。
您应该返回已填写的DataTable
Return CT
您的功能代码应为
Private Function GetCompanyInfo(ByRef PartNumber As String, ByRef Version As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Using con As New SqlConnection(constr)
Dim CT As New DataTable()
Using cmd As New SqlCommand("spCommonInfo")
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PartNumber", PartNumber)
cmd.Parameters.AddWithValue("@Version", Version)
Using sda As New SqlDataAdapter(cmd)
sda.Fill(CT)
End Using
End Using
End Using
Return CT
End Function