Wiggy2030
New member
If anyone can help me with this it would be great. I am doing an assignment for my college and i need to update some information to access using vb.net. I have tried everything and spent 6 hours trying to sort this out with no luck.
When i press my update button it crashes on the line da.Update(ds, "Staff")
My code is below
When i press my update button it crashes on the line da.Update(ds, "Staff")
My code is below
VB.NET:
Imports System.Data
Imports System.Data.OleDb
Public Class Staff
Inherits System.Windows.Forms.Form
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Staff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Refreshers.mdb"
con.Open()
sql = "SELECT * FROM tblStaff"
da = New OleDb.OleDbDataAdapter(sql, con)
da.TableMappings.Add("Table", "Staff")
da.Fill(ds, "Staff")
con.Close()
MaxRows = ds.Tables("Staff").Rows.Count
inc = -1
DataGrid1.DataSource = ds.Tables("Staff").DefaultView
DataGrid1.SetDataBinding(ds, "Staff")
txtFirstName.Text = ds.Tables("Staff").Rows(0).Item(1)
txtSurname.Text = ds.Tables("Staff").Rows(0).Item(2)
txtID.Text = ds.Tables("Staff").Rows(0).Item(0)
Catch
MsgBox("Could not connect to the Database, Make sure the spreadsheet is in the correct location, close the program and retry", _
MsgBoxStyle.Critical, "Missing Spreadsheet")
Finally
con.Close()
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Hide()
End Sub
Private Sub NavigateRecords()
txtFirstName.Text = ds.Tables("Staff").Rows(inc).Item(1)
txtSurname.Text = ds.Tables("Staff").Rows(inc).Item(2)
txtID.Text = ds.Tables("Staff").Rows(inc).Item(0)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.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(ByVal sender As System.Object, ByVal e As System.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 btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Staff").Rows(inc).Item(1) = txtFirstName.Text
ds.Tables("Staff").Rows(inc).Item(2) = txtSurname.Text
ds.Tables("Staff").Rows(inc).Item(0) = txtID.Text
da.Update(ds, "Staff")
MsgBox("Data Updated")
End Sub
End Class
Last edited by a moderator: