コード例 #1
0
        private void btn_Luu_Click(object sender, EventArgs e)
        {
            #region Thêm
            if (bAddFlag)
            {
                if (AddEditQuocGia)       // Nhom ngach
                {
                    if (!string.IsNullOrWhiteSpace(txt_Ten.Text))
                    {
                        if (MessageBox.Show("Bạn muốn thêm quốc gia này?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            oQuocGia.TenQuocGia = txt_Ten.Text.Trim();
                            try
                            {
                                oQuocGia.Add();
                                GetTinhTP_QuocGia();
                                UpdateTreeVDonVi();
                                FillQuocGiaCombo();
                                MessageBox.Show("Thêm quốc gia thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ResetInterface(true);
                                AddSuccess = true;
                            }
                            catch (Exception)
                            {
                                AddSuccess = false;
                                MessageBox.Show("Thêm quốc gia không thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        AddSuccess = false;
                        MessageBox.Show("Tên thành phố / quốc gia không được để trống.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else        // Ngach
                {
                    if (!string.IsNullOrWhiteSpace(txt_Ten.Text))
                    {
                        if (MessageBox.Show("Bạn muốn thêm thành phố này?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            oTinhTP.TenTinhTP = txt_Ten.Text.Trim();
                            oTinhTP.QuocGiaID = Convert.ToInt32(comB_QuocGia.SelectedValue);
                            try
                            {
                                oTinhTP.Add();
                                GetTinhTP_QuocGia();
                                UpdateTreeVDonVi();
                                FillQuocGiaCombo();
                                MessageBox.Show("Thêm thành phố thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ResetInterface(true);
                                AddSuccess = true;
                            }
                            catch (Exception)
                            {
                                AddSuccess = false;
                                MessageBox.Show("Thêm thành phố không thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        AddSuccess = false;
                        MessageBox.Show("Mã và tên thành phố không được để trống.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            #endregion

            #region Sửa
            else
            {
                if (AddEditQuocGia)       // Nhom ngach
                {
                    if (!string.IsNullOrWhiteSpace(txt_Ten.Text))
                    {
                        if (MessageBox.Show("Bạn muốn sửa quốc gia này?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            oQuocGia.TenQuocGia = txt_Ten.Text.Trim();
                            oQuocGia.ID         = Convert.ToInt32(SelectedNode.Name);
                            try
                            {
                                oQuocGia.Update();
                                GetTinhTP_QuocGia();
                                UpdateTreeVDonVi();
                                MessageBox.Show("sửa quốc gia thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ResetInterface(true);
                                AddSuccess = true;
                            }
                            catch (Exception)
                            {
                                AddSuccess = false;
                                MessageBox.Show("sửa quốc gia không thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        AddSuccess = false;
                        MessageBox.Show("Tên thành phố / quốc gia không được để trống.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else        // Ngach
                {
                    if (!string.IsNullOrWhiteSpace(txt_Ten.Text))
                    {
                        if (MessageBox.Show("Bạn muốn sửa thành phố này?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            oTinhTP.TenTinhTP = txt_Ten.Text.Trim();
                            oTinhTP.ID        = Convert.ToInt16(SelectedNode.Name);
                            oTinhTP.QuocGiaID = Convert.ToInt32(comB_QuocGia.SelectedValue);
                            try
                            {
                                oTinhTP.Update();
                                GetTinhTP_QuocGia();
                                UpdateTreeVDonVi();
                                MessageBox.Show("sửa thành phố thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ResetInterface(true);
                                AddSuccess = true;
                            }
                            catch (Exception)
                            {
                                AddSuccess = false;
                                MessageBox.Show("sửa thành phố không thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        AddSuccess = false;
                        MessageBox.Show("Mã và tên thành phố không được để trống.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            #endregion

            if (AddSuccess)
            {
                txt_Ten.Text = "";
                if (lstQuocGia.Count > 0 && !bAddFlag)
                {
                    comB_QuocGia.SelectedIndex = -1;
                }
            }
        }