/// <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); }
/// <summary> /// Lưu thông tin /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveDataFormExel_Click(object sender, EventArgs e) { string userName = Program.CurrentUser.UserName; string strUpdate = null; string strInsert = null; int countUpdate = 0; int countInsert = 0; int countExits = 0; if (!string.IsNullOrEmpty(textEditPathFileExel.Text)) { const string sheetName = "Sheet1"; string pathToExcelFile = textEditPathFileExel.Text.Trim(); var excelFile = new ExcelQueryFactory(pathToExcelFile); excelFile.AddMapping <Department>(x => x.DepartmentName, "DepartmentName"); excelFile.AddMapping <Department>(x => x.Description, "Description"); excelFile.TrimSpaces = TrimSpacesType.Both; excelFile.ReadOnly = true; IQueryable <Department> departments = (from a in excelFile.Worksheet <Department>(sheetName) select a); try { foreach (Department department in departments) { if (!_departmentService.CheckDepartmentNameExits(department.DepartmentName)) { // Bỏ qua nếu đã tồn tại rồi if (radioButtonIgnoreIfDepartmentExits.Checked) { countExits++; } // Cập nhật nếu tên Bộ Phận đã tồn tại rồi if (radioButtonUpdateIfDepartmentExits.Checked) { Department updateDepartment = _departmentService.GetDepartmentName(department.DepartmentName); updateDepartment.UpdateBy = userName; updateDepartment.ModifyDate = DateTime.Now; updateDepartment.DepartmentName = department.DepartmentName; updateDepartment.Description = department.Description; try { _departmentService.Update(updateDepartment); countUpdate++; strUpdate += string.Format("{0}, ", department.DepartmentName); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi cập nhật \n{0}", ex.Message)); } } } // Nếu tên chưa tồn tại thì thực hiện thêm mới else { department.DepartmentID = NextId(); department.CreatedDate = DateTime.Now; department.CreatedBy = userName; department.IsActive = true; try { _departmentService.Add(department); countInsert++; strInsert += string.Format("{0}, ", department.DepartmentName); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi thêm mới \n{0}", ex.Message)); } } } if (XtraMessageBox.Show( string.Format("Thực hiện thành công.\n" + "=> Bỏ qua: {3} - Bộ phận đã tồn tại \n" + "=> Thêm mới: {0} - {2} \n" + "=> Cập nhật: {1} - {4}" + "\nBạn có muốn thêm mới Bộ Phận nữa không?", countInsert, countUpdate, strInsert, countExits, strUpdate), "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { gridControl1.DataSource = null; textEditPathFileExel.Text = string.Empty; } else { DialogResult = DialogResult.No; } } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); foreach (var eve in ex.EntityValidationErrors) { sb.AppendLine(String.Format("Entity of type '{0}' in state '{1}' has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { sb.AppendLine(String.Format("- Property: '{0}', Error: '{1}'", ve.PropertyName, ve.ErrorMessage)); } } throw new Exception(sb.ToString(), ex); } } else { XtraMessageBox.Show("Vui lòng chọn tập tin để nhập", "Thông Báo Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); textEditPathFileExel.Focus(); } }