Exemple #1
0
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNCC.Text != "" && txtTenNCC.Text != "" && txtDiaChi.Text != "" && txtSDT.Text != "")
                {
                    nhaCungCapDTO.MaNCC  = Convert.ToInt32(txtMaNCC.Text);
                    nhaCungCapDTO.HoTen  = txtTenNCC.Text;
                    nhaCungCapDTO.DiaChi = txtDiaChi.Text;
                    nhaCungCapDTO.SDT    = txtSDT.Text;

                    if (!(nccBLL.CheckID(nhaCungCapDTO.MaNCC)))
                    {
                        int check = nccBLL.InsertNCC(nhaCungCapDTO);

                        if (check == 1)
                        {
                            dgvNCC.DataSource = nccBLL.getAllNCC();
                            dgvNCC.Show();
                        }
                        else
                        {
                            MessageBox.Show("Thêm không thành công");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Mã nhân viên " + nhaCungCapDTO.MaNCC + " đã tồn tại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Các trường không được để trống !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thêm bị lỗi ! " + ex.Message.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //
        //NHÀ CUNG CẤP---------------------------------------------
        private void Excute_NCC(string query)
        {
            if (query == "insert")
            {
                try
                {
                    NhaCungCap ncc = new NhaCungCap();
                    //mã tự động tăng
                    int count = 0;
                    count = grvNhaCungCap.Rows.Count; //đếm tất cả các dòng của data grv rồi gán cho count
                    string str  = "";
                    int    str2 = 0;
                    str  = Convert.ToString(grvNhaCungCap.Rows[count - 2].Cells[0].Value);
                    str2 = Convert.ToInt32((str.Remove(0, 1)));
                    if (str2 + 1 < 10)
                    {
                        ncc.IDNCC = txtIDNCC.Text = "N0" + (str2 + 1).ToString();
                    }
                    else if (str2 + 1 < 100)
                    {
                        ncc.IDNCC = txtIDNCC.Text = "N" + (str2 + 1).ToString();
                    }
                    //
                    //
                    ncc.TenNCC = txtTenNCC.Text.Trim();
                    ncc.DiaChi = txtDiaChiNCC.Text.Trim();
                    ncc.SDT    = txtSDTNCC.Text.Trim();
                    NhaCungCapBLL capBLL = new NhaCungCapBLL();
                    //
                    //check xem co trung hay khong
                    if (!(capBLL.CheckID(txtIDNCC.Text.Trim())))
                    {
                        capBLL.Insert(ncc);
                        Load_Data_NCC();
                        MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK);
                    }
                    else
                    {
                        MessageBox.Show("Mã " + txtIDNCC.Text.Trim() + " đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    Load_Data_NCC();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Thêm bị lỗi: " + ex.Message.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            if (query == "update")
            {
                try
                {
                    NhaCungCap ncc = new NhaCungCap();
                    ncc.IDNCC  = txtIDNCC.Text.Trim();
                    ncc.TenNCC = txtTenNCC.Text.Trim();
                    ncc.DiaChi = txtDiaChiNCC.Text.Trim();
                    ncc.SDT    = txtSDTNCC.Text.Trim();
                    //
                    //

                    NhaCungCapBLL capBLL = new NhaCungCapBLL();
                    capBLL.Update(ncc);
                    Load_Data_NCC();
                    MessageBox.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Sửa bị lỗi: " + ex.Message.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            if (query == "delete")
            {
                try
                {
                    string        id     = txtIDNCC.Text.Trim();
                    NhaCungCapBLL capBLL = new NhaCungCapBLL();
                    capBLL.Delete(id);
                    Load_Data_NCC();
                    MessageBox.Show("Đã xóa thành công:");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Không thể xóa: " + ex.Message.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }