2016-05-12 166 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 
Public sqlstatement As String 
Public connString As String = provider & datafile 
lbxActs.Items.Clear() 
    Dim oDataRowView As DataRowView 
    Dim 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.DataSource = ds.Tables("Dates") 
    lbxDates.DisplayMember = "ShowDate" 
    lbxDates.ValueMember = "ShowDate" 
    con.Close() 

    oDataRowView = CType(Me.lbxDates.SelectedItem, DataRowView) 
    sSelectedAssetType = oDataRowView("ShowDate").ToString 
    lbxActs.Items.AddRange(IO.File.ReadAllLines("Resources/" & sSelectedAssetType & ".txt")) 



    con.Close() 
    ds.Clear() 
    con.ConnectionString = connString 
    con.Open()           'Open connection to the database 

    sqlstatement = "SELECT * FROM [Seats" & sSelectedAssetType & "] WHERE [Available] = True " 


    da = New OleDb.OleDbDataAdapter(sqlstatement, con) 
    da.Fill(ds, "seats")       'Fill the data adapter 
    con.Close() 
    Dim recordCount, x As Short 
    recordCount = 0 
    x = 0 
    recordCount = ds.Tables("seats").Rows.Count 

    tbxLeftS.Text = recordCount 

該計劃的第一部分,將所有演出日期,列表框,然後根據其中的一個被點擊它顯示的行爲從我的文本文件不同的列表。程序的第二部分應該使用SQL語句來查找所有可用的座位位置。然後對記錄進行計數,然後在文本框中顯示數字。

Object reference not set to an instance of an object 

此錯誤出現4次,它突出了這一行:

sSelectedAssetType = oDataRowView("ShowDate").ToString 
+1

看起來像'oDataRowView(「ShowDate」)'是'沒有' – Ian

+0

也許沒有選擇 –

+0

我剛剛編輯的代碼,我忘了包括一些東西 – chandler

回答

0

爲了擺脫錯誤的,它極有可能以某種方式oDataRowViewnull。在嘗試將其轉換爲字符串之前,您需要檢查以確保其不爲空。做這樣的事情:

IF Not isdbnull(oDataRowView) 
oDataRowView = CType(Me.lbxDates.SelectedItem, DataRowView) 
End if 

至於爲什麼你得到一個空值oDataRowView,這將需要你進一步調試。也許這可能是一個與回發問題(假設這是一個網頁,而不是winforms)

+0

我用你的代碼替換了代碼,但是同樣的錯誤仍然出現? – chandler

+0

您是否使用過調試器來檢查值? – logixologist

相關問題