数据集创建时出错

时间:2013-02-04 12:36:23

标签: vb.net

我在运行时遇到此函数的错误

Private Sub adultPay()
sql = "SELECT adultRate FROM roomType WHERE roomType = " & rumType.ToString & ""
connectDatabase()
Using da As New OleDb.OleDbDataAdapter(sql, con)
Using ds As New DataSet
Try
da.Fill(ds, "AdultRate") 'error here
Dim adultRate As String = ds.Tables("AdultRate").Rows(0).Item(0).ToString
Dim adultTotal As Integer = txtNoOfAdults.Text * Val(adultRate)
txtAdultTotal.Text = adultTotal.ToString
Catch ex As Exception
MessageBox.Show(ex.ToString, "error")
Clipboard.SetDataObject(ex)
End Try
DisconnectDB()
End Using
End Using
End Sub

当成人人数被放置时,该函数应该改变成人总文本框的值。 错误如下

System.Data.OleDb.OleDbException: No value given for one or more required parameters.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at Albergo.frmCheckIn.adultPay() in C:\Users\gman\Documents\Visual Studio 2008\Projects\Albergo\Albergo\frmCheckIn.vb:line 111

第111行是我们发现的子行 da.Fill(ds," AdultRate")

请帮忙

2 个答案:

答案 0 :(得分:0)

您将表的名称作为WHERE条件传递

它应该是:

"SELECT adultRate FROM roomType WHERE roomType_columnName = " & rumType.ToString & ""

答案 1 :(得分:0)

我实际上发现了问题所在。房型需要作为字符串放置,例如

sql = "SELECT adultRate FROM roomType WHERE roomType = '" & rumType.ToString & "'"

而不是

sql = "SELECT adultRate FROM roomType WHERE roomType = " & rumType.ToString & ""

感谢大家的帮助