VB.net 错误“参数空异常未处理,列参数不能为空”
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Title
表中没有Rating
列,DVD
表中也没有Title
列。编辑:使用 SELECT 语句添加 * (所有列)而不是列列表,
There is no
Rating
column inTitle
table and noTitle
column inDVD
table.EDIT : Add * (all columns) instead of column list with SELECT statement,