Example #1
0
    /// <summary>
    /// Update the related data tables by writing codes manually
    /// </summary>
    private void departmentBindingNavigatorSaveItem2_Click(object sender,
                                                           EventArgs e)
    {
        this.Validate();
        this.departmentBindingSource.EndEdit();

        // Add this line of code in the codes automatically created
        this.courseBindingSource.EndEdit();

        // Writing codes manually to update the related data tables

        // Get all the deleted data rows in the Course table
        SQLServer2005DBDataSet.CourseDataTable deletedCourses =
            (SQLServer2005DBDataSet.CourseDataTable)sQLServer2005DBDataSet.
            Course.GetChanges(DataRowState.Deleted);

        // Get all the new data rows in the Course table
        SQLServer2005DBDataSet.CourseDataTable newCourses =
            (SQLServer2005DBDataSet.CourseDataTable)sQLServer2005DBDataSet.
            Course.GetChanges(DataRowState.Added);

        // Get all the modified data rows in the Course table
        SQLServer2005DBDataSet.CourseDataTable modifiedCourses =
            (SQLServer2005DBDataSet.CourseDataTable)sQLServer2005DBDataSet.
            Course.GetChanges(DataRowState.Modified);

        try
        {
            // Remove all deleted coures from the Course table.
            if (deletedCourses != null)
            {
                courseTableAdapter.Update(deletedCourses);
            }

            // Update the Department table.
            departmentTableAdapter.Update(sQLServer2005DBDataSet.Department);

            // Add new courses to the Course table.
            if (newCourses != null)
            {
                courseTableAdapter.Update(newCourses);
            }

            // Update all modified courses.
            if (modifiedCourses != null)
            {
                courseTableAdapter.Update(modifiedCourses);
            }

            // Accept all the changes of the Strong Typed DataSet
            sQLServer2005DBDataSet.AcceptChanges();
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Update failed for " + ex.Message);
        }
        finally
        {
            // Cleanup the data rows objects
            if (deletedCourses != null)
            {
                deletedCourses.Dispose();
            }
            if (newCourses != null)
            {
                newCourses.Dispose();
            }
            if (modifiedCourses != null)
            {
                modifiedCourses.Dispose();
            }
        }
    }