2.7.5. Inserting and Updating Data
In this section the changes made to Course objects bound are saved to the DataGridView control to the database and also run the completed Course Manager application.
Save changes made to objects
- 
            In the Toolbox , expand Common Controls , drag the Button control to the CourseViewer form designer, change the name of the control to saveChanges , and change the Text value to Update . 
- 
            In the CourseViewer form designer, double-click the saveChanges control. This creates the saveChanges_Click event handler method. 
- 
            Paste the following code that saves object changes to the database. Visual Basic Try ' Save object changes to the database, display a message, ' and refresh the form. schoolContext.SaveChanges() MessageBox.Show("Changes saved to the database.") Me.Refresh() Catch ex As Exception MessageBox.Show(ex.Message) End Try C# try { // Save object changes to the database, display a message, // and refresh the form. schoolContext.SaveChanges(); MessageBox.Show("Changes saved to the database."); this.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message); }
- 
            In the closeForm_Click event handler method, type the following code. This code disposes of the object context before the form is closed. Visual Basic ' Dispose the object context. schoolContext.Dispose() C# // Dispose the object context. schoolContext.Dispose();
Build and run the Class Scheduling application
- 
            From the Debug menu, select Start Debugging or Start Without Debugging , to build and run the application. 
- 
            When the form loads, select a department from the ComboBox control to display the courses that belong to that department. Figure 2.115. ComboBox   
 
- 
            In the DataGridView , update course information or add a new course and then click Update to save changes to the database and displays a message box that declares the number of saved changes. Figure 2.116. DataGridView   
 
The process is now complete
