Public Class frmAddB
Dim con As New OleDb.OleDbConnection
Dim sql As String
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
Private Sub frmAddB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ATE THESS\Documents\School xD\2-1\VB\VB.accdb"
Try
con.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
frmBookMngmt.Show()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
sql = "INSERT INTO [All] (ISBN, Author, Title, PlaceOfPublication, Publisher, Location, Edition, CallNumber) VALUES ('" & txtISBN.Text & "', '" & txtAuthor.Text & "', '" & txtTitle.Text & "', '" & txtPlace.Text & "', '" & txtPub.Text & "', '" & txtLoc.Text & "', '" & txtEd.Text & "', '" & txtCallNo.Text & "')"
da.InsertCommand = New OleDb.OleDbCommand(sql, con)
da.InsertCommand.ExecuteNonQuery()
MsgBox("Book Added!")
frmBookMngmt.updateDataGridValues()
End Sub
End Class
每当我点击“保存按钮”时,会出现一个窗口
出现WindowsApplication1.exe中出现未处理的“System.NullReferenceException”类型异常
附加信息:对象引用未设置为对象的实例。“
,它指向
行da.InsertCommand = New OleDb.OleDbCommand(sql, con)
任何人都可以帮助我吗?感谢
答案 0 :(得分:1)
在您的示例中,您似乎永远不会实例化变量da。
那么也许你也应该尝试改变变量的范围。 我想知道每次要访问数据库时都要创建一个新连接。 在您的代码中,如果例如frmAddB_Load失败,它可能会处于不可用状态。