使用vb.NET我想通過一個數據集從數據庫中提取數據,但運行按鈕的代碼時,我得到這個錯誤:時將引發NullReferenceException
「類型的未處理的異常「系統。 NullReferenceException'發生在Full Program.exe中
附加信息:未將對象引用設置爲對象的實例。
這是我的代碼,它有什麼問題?
Public Class mod_data
Dim inc As Integer
Dim con As OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim MaxRows As Integer
Private Sub mod_data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dbProvider = "PROVIDER=Microsoft.ACE.12.0;"
dbSource = "\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb;"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblComponent_List"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Component_List")
con.Close()
MaxRows = ds.Tables("Component_List").Rows.Count
inc = -1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txtComponent_ID.Text = ds.Tables("Component_List").Rows(1).Item(0)
txtComponent_Name.Text = ds.Tables("Component_List").Rows(1).Item(1)
'(...)
End Sub
'(...)
End Sub
@Crono
它仍然扔了同樣的錯誤。這是新代碼:
仍然無法正常工作。這是新代碼:
公共類mod_data
Dim inc As Integer
Dim con As OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim MaxRows As Integer
Private Sub mod_data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ds = New DataSet("Component_DatabaseDataSet3")
con = New OleDb.OleDbConnection("\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb")
dbProvider = "PROVIDER=Microsoft.ACE.12.0;"
dbSource = "\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb;"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblComponent_List"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Component_List")
con.Close()
MaxRows = ds.Tables("Component_List").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtComponent_ID.Text = ds.Tables("Component_List").Rows(inc).Item(0)
txtComponent_Name.Text = ds.Tables("Component_List").Rows(inc).Item(1)
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
ElseIf inc = -1 Then
MsgBox("No Records Yet")
ElseIf inc = 0 Then
MsgBox("First Record")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
ds.Tables("Component_List").Rows(inc).Item(0) = txtComponent_ID.Text
ds.Tables("Component_List").Rows(inc).Item(1) = txtComponent_Name.Text
MsgBox("Data updated")
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Component_List").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "Component_List")
NavigateRecords()
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txtComponent_ID.Text = ds.Tables("Component_List").Rows(1).Item(0)
txtComponent_Name.Text = ds.Tables("Component_List").Rows(1).Item(1)
End Sub
末級
這就是那種你可以用簡單的調試會話解決得更快的事情,朋友。 :) – Crono
在調試器中逐步執行代碼。當發生異常時,哪個對象爲null(VB中爲'Nothing')?你在哪裏初始化了那個對象? – David