VB.net错误'Argument Null Exception未处理,列参数不能为null'

时间:2011-10-20 02:23:24

标签: vb.net ms-access treeview oledb

我正在尝试在VB.net中创建一个树视图,数据必须从MSAccess 2010数据库加载。当我尝试运行此程序时,我收到错误: Argument Null Exception未处理,'column'参数不能为null 并且程序崩溃。这是代码:

Imports System.Data.OleDb

Public Class frmRating

Private Sub frmRating_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Database Connections and Datasets
    Dim DSRating As DataSet
    Dim CNRating As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BlockHeadDatabase.accdb;Persist Security Info=False;")
    'DataAdapters to hold records
    Dim DARatings As New OleDb.OleDbDataAdapter("SELECT Rating FROM Rating", CNRating)
    Dim DATitle As New OleDb.OleDbDataAdapter("SELECT Title, Lending_Class FROM Title Order By Title", CNRating)
    Dim DADVD As New OleDb.OleDbDataAdapter("SELECT DVD_NO, Branch_Code FROM DVD", CNRating)

    'Instantiate the datasets, open the connection and fill the dataAdapters from the table
    DSRating = New DataSet()
    CNRating.Open()

    DARatings.Fill(DSRating, "Rating")
    DATitle.Fill(DSRating, "Title")
    DADVD.Fill(DSRating, "DVD")

    'Close the connection to the data store, free up the resources
    CNRating.Close()

    'Create a data relation objects between the data tables
    DSRating.Relations.Add("RatingtoTitle", DSRating.Tables("Rating").Columns("Rating"), DSRating.Tables("Title").Columns("Rating"))
    DSRating.Relations.Add("TitletoDVD", DSRating.Tables("Title").Columns("Title"), DSRating.Tables("DVD").Columns("Title"))

    Dim relation As DataRelation
    Dim table1Column As DataColumn
    Dim table2Column As DataColumn

    'retrieve the column
    table1Column = DSRating.Tables("Rating").Columns("Rating")
    table2Column = DSRating.Tables("Title").Columns("Rating")
    'Create a data relation objects between the data tables
    relation = New DataRelation("relation", table1Column, table2Column)
    DSRating.Relations.Add(relation)
    DSRating.Relations.Add("RatingtoTitle", DSRating.Tables("Rating").Columns("Rating"), DSRating.Tables("Title").Columns("Rating"))
    Try
        DSRating.Relations.Add("RatingtoTitle", DSRating.Tables("tblVehicle").Columns("Rego_No"), DSRating.Tables("tblRental").Columns("Rego_No"))

        MsgBox("Data relation completed")
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

Rating表格中没有Title列,Title表格中没有DVD列。

编辑:使用SELECT语句

添加*(所有列)而不是列列表
Dim DARatings As New OleDb.OleDbDataAdapter("SELECT * FROM Rating", CNRating)
Dim DATitle As New OleDb.OleDbDataAdapter("SELECT * FROM Title Order By Title", CNRating)
Dim DADVD As New OleDb.OleDbDataAdapter("SELECT * FROM DVD", CNRating)