2016-06-07 53 views
0

我正在創建一個程序來爲學校表演預訂門票,這部分代碼將數據庫中的日期顯示到列表框中,然後獲取選定的值並檢查帶有該名稱的數據庫用於可用座位。錯誤 - 沒有爲命令對象設置命令文本

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 

我不斷收到這個錯誤,我不知道如何解決它,請幫助:)

回答

0

創建對象後的字符串對象的內容不能改變。 如果您想使用變量來存儲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,cn) 
    da.Fill(ds, "Dates") 

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

謝謝。現在工作! – chandler