I cannot figure where I'm messing up at on this one. I'm not sure why I'm running into this error. The code is bugging out at:
That table only has 3 fields, and ID field (which is autonumber), First (character) and Last (character).
Here is my code. Can someone point me to where I'm messing up at? Thanks.
VB.NET:
da.Update(ds, "Employee")
Here is my code. Can someone point me to where I'm messing up at? Thanks.
VB.NET:
Public Class frmEmployee
Inherits System.Windows.Forms.Form
' Setup data connections
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim MaxRows As Integer ' Will hold Maximum # of Rows
Dim inc As Integer ' Used for navigating through dataset
Dim int As Integer
Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = c:\maintenance\maintenance.mdb"
con.Open()
sql = "Select * From Employee"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Employee")
con.Close()
If ds.Tables("Employee").Rows.Count > 0 Then
With dgvEmp
.DataSource = ds.Tables("Employee")
End With
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
MaxRows = ds.Tables("Employee").Rows.Count ' Stores how many rows are in the DataSet
inc = MaxRows
' Quick validation to make sure employee name is entered
If txtFirst.Text = "" Then
MsgBox("First Name Missing")
txtFirst.Focus()
Exit Sub
End If
If txtLast.Text = "" Then
MsgBox("Last Name Missing")
txtLast.Focus()
Exit Sub
End If
Dim cb As New OleDb.OleDbCommandBuilder(da) ' Command Builder
Dim dsNewRow As DataRow ' DataRow Object - needed for adding new rows to DataSet
dsNewRow = ds.Tables("Employee").NewRow() ' Creates the new DataRow object, stores it in dsNewRow variable
dsNewRow.Item("First") = txtFirst.Text
dsNewRow.Item("Last") = txtLast.Text
ds.Tables("Employee").Rows.Add(dsNewRow) ' This method adds the Row to the DataSet
ds.Tables("Employee").Rows(inc).Item("First") = txtFirst.Text
ds.Tables("Employee").Rows(inc).Item("Last") = txtLast.Text
da.Update(ds, "Employee") ' This line here is what actually updates the database
MsgBox("Employee Saved")
ds.Tables("Employee").Rows.Clear()
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = c:\maintenance\maintenance.mdb"
con.Open()
sql = "Select * From Employee"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Employee")
con.Close()
If ds.Tables("Employee").Rows.Count > 0 Then
With dgvEmp
.DataSource = ds.Tables("Employee")
End With
End If
txtFirst.Text = ""
txtLast.Text = ""
txtFirst.Focus()
End Sub
End Class
Last edited: