Пример #1
0
        // Save any additions, modifications and deletions to the database.
        private void employeeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            // Validate entries and end any edits.
            this.Validate();
            this.employeeBindingSource.EndEdit();

            // Try to save all changes. If any columns are empty display an error message and don't save.
            // If ID is changed, notify user that this is not a valid operation.
            // If a pay rate is entered greater, than database support then notify user.
            // Display error message if other error occurs.
            try
            {
                dbcontext.SaveChanges();
            }
            catch (DbEntityValidationException)
            {
                MessageBox.Show(this, "Columns cannot be empty", "Entity Validation Exception",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.InvalidOperationException)
            {
                MessageBox.Show(this, "Cannot modify employee ID.\n"
                                + "Please create a new employee with the desired ID and then delete the old one",
                                "Invalid Operation",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException)
            {
                MessageBox.Show(this, "Cannot pay an employee this rate.\n"
                                + "The max allowable pay is 999999.99.",
                                "Invalid Operation",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(this, e1.Message,
                                "Exception",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Display the latest infomation contained in the Database.
            RefreshDB();
        }