Zebra0.com

visual-basic database

If you run the program, you will see the table populated with the movies. Scroll to the bottom to the last line with an asterisk * and add a movie to the table, and make some other change to the data, then close the application. When you run it again the changes you made are not there.

Double click on the form to open the code view and you will notice that Form1_Load was created:
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 'MoviesDataSet.Movies' table. You can move, or remove it, as needed.
    Me.MoviesTableAdapter.Fill(Me.MoviesDataSet.Movies)
End Sub 'Form1_Load
This code is how the table gets populated. Next we will add a menu strip menu and build a menu with the menu items E&xit and &Save. Make the names for these items MnuExit and MnuSave. The method for propting on exit is the same as in the lesson on files. Add global variables Changed and Canceled, modify the code for form load, and add the code for save as shown below:
Public Class Form1
  Dim Changed, Canceled As Boolean
  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 'MoviesDataSet.Movies' table. 
    'You can move, or remove it, as needed.
    Me.MoviesTableAdapter.Fill(Me.MoviesDataSet.Movies)
    Me.MoviesDataSet.AcceptChanges()
    Changed = False
  End Sub 'Form1_Load
  
  Private Sub MnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MnuSave.Click
Save()
End Sub 'MnuSave_Click
Private Sub Save()
Try
Me.Validate()
Me.MoviesBindingSource.EndEdit()
Me.MoviesTableAdapter.Update(Me.MoviesDataSet.Movies)
MsgBox("Update successful")
Changed = False
Catch ex As Exception
MsgBox("Update failed")
End Try
End Sub 'Save End Class

NEXT: Data Tables