There are 4 fields in my database(exlcuding the primary key)...only 3 fields can be successfully updated. the "Section" field returns an error pointing to da.Update(ds, "moreforengineers")?
the following is my code
Public Class Register
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub NavigateRecords()
SectionTextBox.Text = ds.Tables("moreforengineers").Rows(inc).Item("Sect ion")
LastNameTextBox.Text = ds.Tables("moreforengineers").Rows(inc).Item("LNam e")
FirstNameTextBox.Text = ds.Tables("moreforengineers").Rows(inc).Item("FNam e")
MobileNoTextBox.Text = ds.Tables("moreforengineers").Rows(inc).Item("Mobi leNo")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MoreforengineersDataSet.Register' table. You can move, or remove it, as needed.
ClearButton.Enabled = False
RegisterButton.Enabled = False
loaddb()
End Sub
Private Sub loaddb()
con.Close()
Me.RegisterTableAdapter.Fill(Me.MoreforengineersDa taSet.Register)
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Users\Geraldine\Documents\Visual Studio 2010\Projects\moreforengineers\moreforengineers\mo reforengineers.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM Register"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "moreforengineers")
inc = 0
NavigateRecords()
con.Close()
MaxRows = ds.Tables("moreforengineers").Rows.Count
End Sub
Private Sub RegisterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterButton.Click
RegisterButton.Enabled = True
AddButton.Enabled = False
UpdateButton.Enabled = False
DeleteButton.Enabled = False
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("moreforengineers").NewRow()
dsNewRow.Item("Section") = SectionTextBox.Text
dsNewRow.Item("LName") = LastNameTextBox.Text
dsNewRow.Item("FName") = FirstNameTextBox.Text
dsNewRow.Item("MobileNo") = MobileNoTextBox.Text
ds.Tables("moreforengineers").Rows.Add(dsNewRow)
loaddb()
da.Update(ds, "moreforengineers")
MsgBox("New Record added to the Database")
AddButton.Enabled = True
UpdateButton.Enabled = True
DeleteButton.Enabled = True
CancelButton.Enabled = True
End If
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
SectionTextBox.Clear()
FirstNameTextBox.Clear()
LastNameTextBox.Clear()
MobileNoTextBox.Clear()
End Sub
Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.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 LastButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LastButton.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub FirstButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstButton.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub RegisterBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.RegisterBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Moreforenginee rsDataSet)
End Sub
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("moreforengineers").Rows(inc).Item("Sect ion") = SectionTextBox.Text
ds.Tables("moreforengineers").Rows(inc).Item("LNam e") = LastNameTextBox.Text
ds.Tables("moreforengineers").Rows(inc).Item("FNam e") = FirstNameTextBox.Text
ds.Tables("moreforengineers").Rows(inc).Item("Mobi leNo") = MobileNoTextBox.Text
da.Update(ds, "moreforengineers")
MsgBox("Data updated")
loaddb()
End Sub
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
RegisterButton.Enabled = True
AddButton.Enabled = False
UpdateButton.Enabled = False
DeleteButton.Enabled = False
ClearButton.Enabled = True
SectionTextBox.Clear()
FirstNameTextBox.Clear()
LastNameTextBox.Clear()
MobileNoTextBox.Clear()
End Sub
Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
RegisterButton.Enabled = False
AddButton.Enabled = True
UpdateButton.Enabled = True
DeleteButton.Enabled = True
ClearButton.Enabled = False
CancelButton.Enabled = False
inc = 0
NavigateRecords()
End Sub
Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
Else
ds.Tables("moreforengineers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
loaddb()
da.Update(ds, "moreforengineers")
End If
End Sub
End Class