错误 - 未为命令对象设置命令文本

时间:2016-06-07 10:21:45

标签: vb.net

我正在创建一个程序来预订学校表演的门票,这部分代码显示从数据库到列表框的日期,然后它获取所选值并检查具有该名称的数据库以获取可用席位。

Public ds As New DataSet 'used to store the basic elements of the database
Public con As New OleDb.OleDbConnection 'used to connect to the database
Public provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Public datafile As String = "Resources/database.accdb" 'database location and version
Public da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlstatement, con)
Public sqlstatement As String
Public connString As String = provider & datafile
Public UserbeingEdited As String
Public sSelectedAssetType As String


 ds.Clear()
    con.ConnectionString = connString
    con.Open()
    sqlstatement = "SELECT ShowDate FROM AvailableDates"
    da.Fill(ds, "Dates")

    lbxDates.ValueMember = "ShowDate"
    lbxDates.DisplayMember = "ShowDate"
    lbxDates.DataSource = ds.Tables("Dates")
    con.Close()

Private Sub lbxDates_SelectedValueChanged(sender As Object, e As EventArgs) Handles lbxDates.SelectedValueChanged
    Dim oDataRowView As DataRowView = CType(Me.lbxDates.SelectedItem, DataRowView)
    lbxActs.Items.Clear()
    lbxActs.Items.AddRange(IO.File.ReadAllLines("Resources/" & sSelectedAssetType & ".txt"))
    sSelectedAssetType = oDataRowView("ShowDate").ToString
    For Each btn As Control In Seating_Plan.Controls
        If checkSeats(btn.Name()) = "True" Then
            SeatCount = SeatCount + 1
        End If
    Next

我一直收到此错误,我不知道如何修复它,请帮助:)

1 个答案:

答案 0 :(得分:1)

创建对象后,无法更改字符串对象的内容。 如果要使用变量来存储SQL查询,请创建变量并在将其赋值给OleDbDataAdapter之前设置其值。

Public ds As New DataSet 'used to store the basic elements of the database
Public con As New OleDb.OleDbConnection 'used to connect to the database
Public provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Public datafile As String = "Resources/database.accdb" 'database location and version
Public da As OleDb.OleDbDataAdapter 
Public sqlstatement As String
Public connString As String = provider & datafile
Public UserbeingEdited As String
Public sSelectedAssetType As String


    ds.Clear()
    con.ConnectionString = connString
    con.Open()
    sqlstatement = "SELECT ShowDate FROM AvailableDates"
    da = new OleDb.OleDbDataAdapter(sqlstatement, con)
    da.Fill(ds, "Dates")

    lbxDates.ValueMember = "ShowDate"
    lbxDates.DisplayMember = "ShowDate"
    lbxDates.DataSource = ds.Tables("Dates")
    con.Close()