private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtProductGroupID.Text))
            {
                Ultils.TextControlNotNull(txtProductGroupID, "Mã Nhóm Hàng");
            }
            else if (string.IsNullOrEmpty(txtProductGroupName.Text))
            {
                Ultils.TextControlNotNull(txtProductGroupName, "Tên Nhóm Hàng");
            }
            else
            {
                if (string.IsNullOrEmpty(_productGroupId))
                {
                    _productGroup = new ProductGroup()
                    {
                        ProductGroupID   = txtProductGroupID.Text.Trim(),
                        ProductGroupName = txtProductGroupName.Text,
                        Active           = checkActive.Checked,
                        CreatedDate      = DateTime.Now,
                        CreatedBy        = Program.CurrentUser.Username,
                    };
                    try
                    {
                        _productGroupService.Add(_productGroup);
                        _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
                {
                    _productGroup = _productGroupService.GetProductGrouprById(_productGroupId);
                    if (_productGroupId != null)
                    {
                        _productGroup.ProductGroupName = txtProductGroupName.Text;
                        _productGroup.Description      = txtDescription.Text;
                        _productGroup.Active           = checkActive.Checked;
                        _productGroup.ModifyDate       = DateTime.Now;
                        _productGroup.ModifyBy         = Program.CurrentUser.Username;

                        try
                        {
                            _productGroupService.Update(_productGroup);
                            _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);
                        }
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Lưu
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(txtProductGroupID.Text))
     {
         XtraMessageBox.Show("Mã Nhóm hàng không được để trống !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
         txtProductGroupID.Focus();
     }
     else if (string.IsNullOrEmpty(txtProductGroupName.Text))
     {
         XtraMessageBox.Show("Tên Nhóm Hàng không được để trống !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
         txtProductGroupName.Focus();
     }
     else
     {
         ProductGroup productGroup = _productGroupService.GetProductGrouprById(_productGroupId);
         if (productGroup != null)
         {
             productGroup.ProductGroupName = txtProductGroupName.Text;
             productGroup.Description      = txtDescription.Text;
             productGroup.IsActive         = checkActive.Checked;
             productGroup.ModifyDate       = DateTime.Now;
             productGroup.UpdateBy         = null;
         }
         try
         {
             _productGroupService.Update(productGroup);
             InsertSysLog(txtProductGroupName.Text);
             XtraMessageBox.Show("Sửa thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
             btnClose_Click(sender, e);
         }
         catch (Exception ex)
         {
             XtraMessageBox.Show(string.Format("Lỗi {0}", ex.Message), "THÔNG BÁO");
         }
     }
 }
Пример #3
0
        /// <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 <ProductGroup>(x => x.ProductGroupName, "ProductGroupName");
                excelFile.AddMapping <ProductGroup>(x => x.Description, "Description");

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

                IQueryable <ProductGroup> productGroups = (from a in excelFile.Worksheet <ProductGroup>(sheetName) select a);

                try
                {
                    foreach (ProductGroup productGroup in productGroups)
                    {
                        if (!_productGroupService.CheckProductGroupNameExit(productGroup.ProductGroupName))
                        {
                            // 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)
                            {
                                ProductGroup updateProductGroup = _productGroupService.GetProductGrouprByName(productGroup.ProductGroupName);
                                updateProductGroup.UpdateBy   = userName;
                                updateProductGroup.ModifyDate = DateTime.Now;
                                try
                                {
                                    _productGroupService.Update(updateProductGroup);
                                    countUpdate++;
                                    strUpdate += string.Format("{0}, ", productGroup.ProductGroupName);
                                }
                                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
                        {
                            productGroup.ProductGroupID = NextId();
                            productGroup.CreatedDate    = DateTime.Now;
                            productGroup.CreatedBy      = userName;
                            productGroup.IsActive       = true;
                            try
                            {
                                _productGroupService.Add(productGroup);
                                countInsert++;
                                strInsert += string.Format("{0}, ", productGroup.ProductGroupName);
                            }
                            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} - Nhóm Hàng đã tồn tại \n" +
                                          "=> Thêm mới: {0} - Nhóm Hàng \n" +
                                          "=> Cập nhật: {1} - Nhóm Hàng \n" +
                                          "Bạ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();
            }
        }