Ejemplo n.º 1
0
 private void buttonSave_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         using (ERPEF entity = new ERPEF())
         {
             if (labelId.Content != null && labelId.Content.ToString() != "")
             {
                 int employeeId = Convert.ToInt32(labelId.Content);
                 var employee   =
                     entity.Employees.SingleOrDefault(
                         n => n.EmployeeId == employeeId
                         );
                 employee.FirstName   = textBoxFirstName.Text;
                 employee.LastName    = textBoxLastName.Text;
                 employee.Address     = textBoxAddress.Text;
                 employee.DateOfBirth = DateOfBirth.SelectedDate;
                 employee.Telephone   = textBoxTelephone.Text;
                 employee.Cellphone   = textBoxCellphone.Text;
                 employee.Email       = textBoxEmail.Text;
                 employee.StateCode   = Convert.ToString(comboBoxStates.SelectedValue);
                 if (entity.SaveChanges() > 0)
                 {
                     MessageBox.Show("Success to update employee!");
                 }
                 else
                 {
                     MessageBox.Show("Failed to update employee or nothing changes!");
                 }
             }
             else
             {
                 var employee = new Employee()
                 {
                     FirstName   = textBoxFirstName.Text,
                     LastName    = textBoxLastName.Text,
                     Address     = textBoxAddress.Text,
                     DateOfBirth = DateOfBirth.SelectedDate,
                     Telephone   = textBoxTelephone.Text,
                     Cellphone   = textBoxCellphone.Text,
                     Email       = textBoxEmail.Text,
                     StateCode   = Convert.ToString(comboBoxStates.SelectedValue)
                 };
                 entity.Employees.Add(employee);
                 if (entity.SaveChanges() > 0)
                 {
                     MessageBox.Show("Success to add new employee!");
                 }
                 else
                 {
                     MessageBox.Show("Failed to add new employee or duplicate data!");
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Ejemplo n.º 2
0
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Employee employee = (Employee)listViewData.SelectedItem;

            if (employee != null &&
                MessageBox.Show("Are you sure to delete?", "?",
                                MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                using (ERPEF entity = new ERPEF())
                {
                    int employeeId = Convert.ToInt32(labelId.Content);
                    employee = entity.Employees.SingleOrDefault(
                        n => n.EmployeeId == employeeId
                        );
                    entity.Employees.Remove(employee);
                    if (entity.SaveChanges() > 0)
                    {
                        MessageBox.Show("Success to delete employee");
                        buttonDisplay_Click(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Failed to delete employee!");
                    }
                }
            }
        }