private void btnSave_Click(object sender, RoutedEventArgs e) { try { if (lblFName.Content == "?" || lblLName.Content == "?" || lblMI.Content == "?" || lblEmail.Content == "?" || lblSuffix.Content == "?" || lblEmail.Content == "?" || String.IsNullOrWhiteSpace(txtFName.Text) || String.IsNullOrWhiteSpace(txtLName.Text) || String.IsNullOrWhiteSpace(cmbPosition.Text) || String.IsNullOrWhiteSpace(cmbDept.Text)) { System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (status == "Add") { DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to add this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == System.Windows.Forms.DialogResult.No) { return; } if (txtFName.Text == "" || txtLName.Text == "" || cmbPosition.Text == "" || cmbDept.Text == "") { System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Regex.IsMatch(txtFName.Text, @"^[a-zA-Z]+$")) { } using (var ctx = new finalContext()) { int pID = ctx.Positions.Where(x=>x.PositionName==cmbPosition.Text).First().PositionID; Employee emp = new Employee { FirstName = txtFName.Text, LastName = txtLName.Text, PositionID = pID, Suffix = txtSuffix.Text, MI = txtMI.Text, Active = true, Department = cmbDept.Text, Email = txtEmail.Text, Photo = ConvertImageToByteArray(selectedFileName) }; var query = from con in ctx.TempAdresses select con; foreach (var item in query) { EmployeeAddress add = new EmployeeAddress { Street = item.Street, City = item.City, Province = item.Province }; ctx.EmployeeAddresses.Add(add); } var query1 = from con in ctx.TempContacts select con; foreach (var item in query1) { EmployeeContact con = new EmployeeContact { Contact = item.Contact }; ctx.EmployeeContacts.Add(con); } AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Employee " + txtFName.Text + " " + txtMI.Text + " " + txtLName.Text + " " + txtSuffix.Text }; ctx.AuditTrails.Add(at); ctx.Employees.Add(emp); ctx.SaveChanges(); System.Windows.MessageBox.Show("Employee has been successfully added", "Information", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } } else { DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to update this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == System.Windows.Forms.DialogResult.No) { return; } using (var ctx = new finalContext()) { int pID = ctx.Positions.Where(x => x.PositionName == cmbPosition.Text).First().PositionID; var emp = ctx.Employees.Find(uId); emp.FirstName = txtFName.Text; emp.LastName = txtLName.Text; emp.PositionID = pID; emp.Suffix = txtSuffix.Text; emp.MI = txtMI.Text; emp.Department = cmbDept.Text; emp.Email = txtEmail.Text; if (isChanged == true) { emp.Photo = ConvertImageToByteArray(selectedFileName); } AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Employee " + txtFName.Text + " " + txtMI.Text + " " + txtLName.Text + " " + txtSuffix.Text }; ctx.AuditTrails.Add(at); ctx.SaveChanges(); System.Windows.MessageBox.Show("Employee has been successfully updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } } } catch (Exception ex) { System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } }
private void btnAddAddress_Click(object sender, RoutedEventArgs e) { try { if (btnAddAddress.Content.ToString() != "Add") { if (lblStreet.Content == "?" || lblProvince.Content == "?" || lblCity.Content == "?" || String.IsNullOrWhiteSpace(txtStreet.Text) || String.IsNullOrWhiteSpace(txtProvince.Text) || String.IsNullOrWhiteSpace(txtCity.Text)) { System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } } if (btnAddAddress.Content.ToString() == "Add") { grpAddress.Visibility = Visibility.Visible; btnAddAddress.Content = "Save"; btnEdtAddress.Content = "Cancel"; btnEdtAddress.IsEnabled = true; btnDelAddress.Visibility = Visibility.Hidden; tbAddress.IsEnabled = false; tbContact.IsEnabled = false; tbInfo.IsEnabled = false; } else if (btnAddAddress.Content.ToString() == "Save") { if (txtCity.Text == "" || txtProvince.Text == "" || txtStreet.Text == "") { System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } //for view if (status == "View") { using (var ctx = new finalContext()) { EmployeeAddress empAdd = new EmployeeAddress { Street = txtStreet.Text, Province = txtProvince.Text, City = txtCity.Text, EmployeeID = uId }; ctx.EmployeeAddresses.Add(empAdd); ctx.SaveChanges(); var add = from cn in ctx.EmployeeAddresses where cn.EmployeeID == uId select new { cn.EmpAddID, cn.Street, cn.Province, cn.City }; dgAddress.ItemsSource = add.ToList(); } reset(); return; } using (var ctx = new finalContext()) { TempAddress add = new TempAddress { Street = txtStreet.Text, Province = txtProvince.Text, City = txtCity.Text }; ctx.TempAdresses.Add(add); ctx.SaveChanges(); dgAddress.ItemsSource = ctx.TempAdresses.ToList(); } reset(); } else { //for view if (status == "View") { using (var ctx = new finalContext()) { var adds = ctx.EmployeeAddresses.Find(Convert.ToInt32(getRow(dgAddress, 0))); adds.City = txtCity.Text; adds.Province = txtProvince.Text; adds.Street = txtStreet.Text; ctx.SaveChanges(); var add = from cn in ctx.EmployeeAddresses where cn.EmployeeID == uId select new { cn.EmpAddID, cn.Street, cn.Province, cn.City }; dgAddress.ItemsSource = add.ToList(); } reset(); return; } using (var ctx = new finalContext()) { var add = ctx.TempAdresses.Find(Convert.ToInt32(getRow(dgAddress, 0))); add.City = txtCity.Text; add.Province = txtProvince.Text; add.Street = txtStreet.Text; ctx.SaveChanges(); dgAddress.ItemsSource = ctx.TempAdresses.ToList(); reset(); } } } catch (Exception ex) { System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } }