//deletes selected employee from database private void DeleteEmployee(int id) { try { using (var _context = new AdventureWorksLTContext()) { var employee = _context.Employee.Where(s => s.EmployeeID == id).First(); _context.Employee.Remove(employee); _context.SaveChanges(); Run(); } } catch { MessageBox.Show("Please try again."); } }
//creates new employee and saves it to databse from input from user. private void NewEmployee() { using (var _context = new AdventureWorksLTContext()) { Employee employee = new Employee(); employee.FirstName = FirstNameTextBox.Text; employee.LastName = LastNameTextBox.Text; employee.Prefix = PrefixComboBox.Text; employee.HomePhone = HomePhoneTextBox.Text; employee.MobilePhone = MobilePhoneTextBox.Text; employee.Skype = SkypeTextBox.Text; employee.PhotoPath = ImageTextBox.Text; employee.Address = AddressTextBox.Text; _context.Employee.Add(employee); _context.SaveChanges(); } }
//updates employee information in database on selected employee private void UpdateEmployeeById(int id) { using (var _context = new AdventureWorksLTContext()) { Employee employee = _context.Employee.FirstOrDefault(c => c.EmployeeID == id); employee.FirstName = FirstNameTextBox.Text; employee.LastName = LastNameTextBox.Text; employee.Prefix = PrefixComboBox.Text; employee.HomePhone = HomePhoneTextBox.Text; employee.MobilePhone = MobilePhoneTextBox.Text; employee.Skype = SkypeTextBox.Text; employee.PhotoPath = ImageTextBox.Text; employee.Address = AddressTextBox.Text; _context.SaveChanges(); } }