private void button7_Click(object sender, EventArgs e) { using (database = new HBSDataContext()) { try { Address address = new Address(); address.StreetName = txtStreetName.Text; address.Suburb = txtSuburb.Text; address.City = txtCity.Text; address.Province = txtProvince.Text; address.PostalCode = txtCode.Text; NextOfKin nextOfKin = new NextOfKin(); nextOfKin.NkName = txtNOK.Text; nextOfKin.NkPhoneNumber = txtNOKPhone.Text; Gender gender = database.Genders.Single(x => x.GenderId == int.Parse(cmbGender.SelectedValue.ToString())); EmployeeType employeeType = database.EmployeeTypes.Single(x => x.EmployeeTypeId == int.Parse(cmbType.SelectedValue.ToString())); Employee employee = new Employee(); employee.Name = txtName.Text; employee.Surname = txtSurname.Text; employee.Email = txtEmail.Text; employee.PhoneNumber = txtPhone.Text; database.Addresses.InsertOnSubmit(address); database.NextOfKins.InsertOnSubmit(nextOfKin); employee.Address = address; employee.NextOfKin = nextOfKin; employee.Gender = gender; employee.EmployeeType = employeeType; database.Employees.InsertOnSubmit(employee); database.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { } this.Close(); mainForm.loadEmployees(); MessageBox.Show("New Employee Added"); } }
public void deleteEmployee(int index) { try { using (database = new HBSDataContext()) { var q = database.Employees.Where(x => x.EmployeeId == int.Parse(index.ToString())).Single <Employee>(); database.GetTable <Address>().DeleteOnSubmit(q.Address); database.GetTable <NextOfKin>().DeleteOnSubmit(q.NextOfKin); database.GetTable <Employee>().DeleteOnSubmit(q); database.SubmitChanges(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { loadEmployees(); } }