VB.net 错误“参数空异常未处理,列参数不能为空”

发布于 2024-12-11 02:15:02 字数 2385 浏览 0 评论 0原文

我正在尝试在 VB.net 中创建树视图,必须从 MSAccess 2010 数据库加载数据。当我尝试运行此程序时,出现错误:参数空异常未处理,“列”参数不能为空并且程序崩溃。这是代码:

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

I am trying to create a treeview in VB.net, the data has to be loaded from MSAccess 2010 database. When I try to run this program I get error : Argument Null Exception was unhandled, 'column' argument cannot be null and the program crashes. Here is the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2024-12-18 02:15:02

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

编辑:使用 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)

There is no Rating column in Title table and no Title column in DVD table.

EDIT : Add * (all columns) instead of column list with SELECT statement,

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文