error - 未将对象引用设置为对象的实例

时间:2016-05-12 13:06:02

标签: vb.net visual-studio-2010

我正在尝试创建一个票务系统,系统的这一部分将显示每个特定日期执行的所有行为。我还试图显示有多少票/座位。我有一个包含所有座位位置的数据库和一个布尔值,以显示它们是否被拍摄。

 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 个答案:

答案 0 :(得分:0)

为了摆脱这个错误,它很可能以某种方式oDataRowViewnull。在尝试将其转换为字符串之前,您需要检查以确保它不为null。做这样的事情:

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

至于为什么要为oDataRowView获取空值,这将需要您进一步调试。也许这可能是PostBack的一个问题(假设这是一个网页而不是winforms。