private void LoadData() { if (!string.IsNullOrEmpty(_departmentId)) { _department = _departmentService.GetDepartmentById(_departmentId); txtDepartmentID.Text = _departmentId; txtDepartmentName.Text = _department.DepartmentName; txtDescription.Text = _department.Description; checkActive.Checked = _department.Active; } else { txtDepartmentID.Text = _departmentService.NextId(); } }
private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtDepartmentID.Text)) { Ultils.TextControlNotNull(txtDepartmentID, "Mã Bộ Phận"); } else if (string.IsNullOrEmpty(txtDepartmentName.Text)) { Ultils.TextControlNotNull(txtDepartmentName, "Tên Bộ Phận"); } else { if (string.IsNullOrEmpty(_departmentId)) { _department = new Department() { DepartmentID = txtDepartmentID.Text.Trim(), DepartmentName = txtDepartmentName.Text, Active = checkActive.Checked, CreatedDate = DateTime.Now, CreatedBy = Program.CurrentUser.Username, }; try { _departmentService.Add(_department); _logService.InsertLog(Program.CurrentUser.Username, "Thêm", this.Text); MessageBoxHelper.ShowMessageBoxSuccess("Thêm thành công!"); ResetControls(); } catch (Exception ex) { MessageBoxHelper.ShowMessageBoxError(ex.Message); } } else { _department = _departmentService.GetDepartmentById(_departmentId); if (_departmentId != null) { _department.DepartmentName = txtDepartmentName.Text; _department.Description = txtDescription.Text; _department.Active = checkActive.Checked; _department.ModifyDate = DateTime.Now; _department.ModifyBy = Program.CurrentUser.Username; try { _departmentService.Update(_department); _logService.InsertLog(Program.CurrentUser.Username, "Sửa", this.Text); MessageBoxHelper.ShowMessageBoxSuccess("Sửa thành công!"); ResetControls(); } catch (Exception ex) { MessageBoxHelper.ShowMessageBoxError(ex.Message); } } } } }
/// <summary> /// Thêm mới /// </summary> /// <param name="department"></param> /// <returns></returns> public void Add(Department department) { _context.Departments.Add(department); SaveChanges(); }
/// <summary> /// Cập nhật thông tin /// </summary> /// <param name="department"></param> public void Update(Department department) { _context.Departments.Attach(department); _context.Entry(department).State = EntityState.Modified; SaveChanges(); }
/// <summary> /// Thêm mới hoặc Cập nhật thông tin Bộ Phận /// </summary> /// <param name="departmentName"></param> private Department InsertOrUpdateDepartment(string departmentName) { if (!string.IsNullOrEmpty(departmentName)) { Department department; if (!_departmentService.CheckDepartmentNameExits(departmentName)) { department = _departmentService.GetDepartmentName(departmentName); } else { department = new Department() { DepartmentID = NextDepartmentId(), DepartmentName = departmentName, CreatedBy = _userName, CreatedDate = DateTime.Now, Description = departmentName, }; try { _departmentService.Add(department); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi thêm Bộ Phận \n{0}", ex.Message)); } } return department; } return null; }