Using vb.NET I am trying to pull data from a database via a dataset but when running the code for the button, I get this error:
"An unhandled exception of type 'System.NullReferenceException' occurred in Full Program.exe
Additional information: Object reference not set to an instance of an object."
Here is my code, what is wrong with it?
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
It is still throwing up the same error. This is the new code:
Still not working. This is the new code:
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
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
End Class