예제 #1
0
 /// <summary>
 /// Thêm mới hoặc Cập nhật thông tin Khu vực
 /// </summary>
 /// <param name="areaName"></param>
 private Area InsertOrUpdateArea(string areaName)
 {
     if (!string.IsNullOrEmpty(areaName))
     {
         Area area;
         if (!_areaService.CheckAreaNameExits(areaName))
         {
             area = _areaService.GetAreaByName(areaName);
         }
         else
         {
             area = new Area()
             {
                 AreaID = NextAreaId(),
                 AreaName = areaName,
                 CreatedBy = _userName,
                 CreatedDate = DateTime.Now,
                 Description = areaName,
             };
             try
             {
                 _areaService.Add(area);
             }
             catch (Exception ex)
             {
                 XtraMessageBox.Show(string.Format("Lỗi thêm Khu vực \n{0}", ex.Message));
             }
         }
         return area;
     }
     return null;
 }
예제 #2
0
        /// <summary>
        /// Lưu thông tin từ file exel đã chọn
        /// </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 <Data.Area>(x => x.AreaName, "AreaName");
                excelFile.AddMapping <Data.Area>(x => x.Description, "Description");

                excelFile.TrimSpaces = TrimSpacesType.Both;
                excelFile.ReadOnly   = true;

                IQueryable <Data.Area> areas = (from a in excelFile.Worksheet <Data.Area>(sheetName) select a);

                try
                {
                    foreach (Data.Area area in areas)
                    {
                        // Nếu tên tồn tại
                        if (!_areaService.CheckAreaNameExits(area.AreaName))
                        {
                            // Bỏ qua nếu đã tồn tại
                            if (radioButtonIgnoreIfDepartmentExits.Checked)
                            {
                                countExits++;
                            }
                            // Cập nhật lại thông tin Khu vực nếu đã tồn tại
                            if (radioButtonUpdateIfDepartmentExits.Checked)
                            {
                                Data.Area areaUpdate = _areaService.GetAreaByName(area.AreaName);
                                areaUpdate.UpdateBy    = userName;
                                areaUpdate.ModifyDate  = DateTime.Now;
                                areaUpdate.AreaID      = area.AreaName;
                                areaUpdate.Description = area.Description;
                                try
                                {
                                    _areaService.Update(areaUpdate);
                                    countUpdate++;
                                    strUpdate += string.Format("{0}, ", area.AreaName);
                                }
                                catch (Exception ex)
                                {
                                    XtraMessageBox.Show(string.Format("Lỗi cập nhật \n{0}", ex.Message));
                                }
                            }
                        }
                        // Nếu tên khu vực chưa tồn tại thì thực hiện thêm mới khu vực
                        else
                        {
                            area.AreaID      = NextId();
                            area.CreatedDate = DateTime.Now;
                            area.CreatedBy   = userName;
                            try
                            {
                                _areaService.Add(area);
                                countInsert++;
                                strInsert += string.Format("{0}, ", area.AreaName);
                            }
                            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} - vì đã 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 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();
            }
        }