Exemplo n.º 1
0
        private void pbSave_Click(object sender, EventArgs e)
        {
            // Mở kết nối
            // Thêm dữ liệu
            if (Them)
            {
                if (this.txtID.Text == "" || this.txtName.Text == "")
                {
                    if (this.txtID.Text == "")
                    {
                        MessageBox.Show("Please fill in CMND !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                for (int i = 0; i < dgvClient.Rows.Count; i++)
                {
                    string t = txtID.Text.Trim();
                    if (t == dgvClient.Rows[i].Cells["CMND"].Value.ToString())
                    {
                        MessageBox.Show("Existed '" + t + "', please type another one !");
                        txtID.ResetText();
                        txtAddress.ResetText();
                        txtName.ResetText();
                        txtPhoneNumber.ResetText();
                        cbFemale.ResetText();
                        txtID.Focus();
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                try
                {
                    // Thực hiện lệnh
                    BLClient blClient = new BLClient();
                    if (this.txtID.Text != "")
                    {
                        blClient.AddClient(this.txtID.Text, this.txtName.Text, this.txtAddress.Text, this.txtPhoneNumber.Text, this.cbFemale.Checked.ToString(), this.dtpBirthDate.Text, ref err);

                        // Load lại dữ liệu trên DataGridView
                        LoadData();

                        // Thông báo
                        MessageBox.Show("Added successfully!");
                    }
                }
                catch (SqlException)
                {
                    this.gbInfor.Text = "Information";
                    MessageBox.Show("Cannot add data !");
                }
            }
            else
            {
                if (this.txtID.Text == "" || this.txtName.Text == "")
                {
                    if (this.txtID.Text == "")
                    {
                        MessageBox.Show("Please fill in CMND !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                // Thực hiện lệnh
                BLClient blClient = new BLClient();
                blClient.UpdateClient(this.txtID.Text, this.txtName.Text, this.txtAddress.Text, this.txtPhoneNumber.Text, this.cbFemale.Checked.ToString(), this.dtpBirthDate.Text, ref err);
                // Thông báo
                MessageBox.Show("Edited successfully!");
                // Load lại dữ liệu trên DataGridView
                LoadData();
            }
            // Đóng kết nối
        }
        public void Save(int id)
        {
            BLClient bl = new BLClient();

            if (id != 0)
            {
                List <ClientModule> listUpdate = bl.GetClientList(id, "", "", "", "", "");
                ClientModule        client     = listUpdate[0];
                string clientName    = txtClientName.Text;
                string clientSurname = txtClientSurname.Text;
                string clientCode    = txtClientCode.Text;
                string email         = txtEmail.Text;
                string phoneNo       = txtPhoneNo.Text;

                if (!(clientName == client.ClientName & clientSurname == client.ClientSurname & clientCode == client.ClientCode & email == client.Email & phoneNo == client.PhoneNo))
                {
                    foreach (char c in txtPhoneNo.Text.ToCharArray())
                    {
                        if (!(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'))
                        {
                            MessageBox.Show("Tnumarul de telefon trebuie sa contina doar cife.", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    if (!(new EmailAddressAttribute().IsValid(txtEmail.Text)) & !String.IsNullOrWhiteSpace(txtEmail.Text))
                    {
                        MessageBox.Show("Emailul trebuie sa fie valid!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }


                    //txtClientCode.TextChanged
                    bool status = false;
                    if (clientCode == client.ClientCode)
                    {
                        bl.UpdateClient(id, clientName, clientSurname, "", phoneNo, email, out status);
                    }
                    else
                    {
                        bl.UpdateClient(id, clientName, clientSurname, clientCode, phoneNo, email, out status);
                    }
                    //BLClient bl = new BLClient();

                    if (status)
                    {
                        MessageBox.Show("Modificare reusita.");
                    }
                    else
                    {
                        MessageBox.Show("Codul de client exista deja!");
                        return;
                    }
                }



                BLAddress bLAddress = new BLAddress();
                //List<AddressModel> list= new List<AddressModel>();
                //list = bLAddress.Address(id);
                for (int i = 0; i < dataGridClientAddress.Rows.Count; i++)
                {
                    AddressModel addressModel = new AddressModel();
                    addressModel = (AddressModel)dataGridClientAddress.Rows[i].DataBoundItem;
                    if (addressModel.status == 1)
                    {
                        bLAddress.AddAddress(id, addressModel.CityName, addressModel.CountyName, addressModel.StreetName, addressModel.StreetNo);
                    }
                    //INSERT
                    if (addressModel.status == 2)
                    {
                        bLAddress.UpdateAddress(id, addressModel.ClientAddressId, addressModel.CityName, addressModel.CountyName, addressModel.StreetName, addressModel.StreetNo);
                    }



                    //UPDATE
                    if (addressModel.status == 3)
                    {
                        bLAddress.DeleteAddress(addressModel.ClientId, addressModel.ClientAddressId);
                    }
                }
            }
            else
            {
                if (String.IsNullOrWhiteSpace(txtClientName.Text))
                {
                    MessageBox.Show("Numele clientului este obligatoriu", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (String.IsNullOrWhiteSpace(txtClientSurname.Text))
                {
                    MessageBox.Show("Prenumele utilizatorului eate obligatoriu", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (String.IsNullOrWhiteSpace(txtClientCode.Text))
                {
                    MessageBox.Show("codul de client este obligatoriu", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                foreach (char c in txtPhoneNo.Text.ToCharArray())
                {
                    if (!(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'))
                    {
                        MessageBox.Show("Numarul de telefon trebuie sa contina doar cife.", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                if (!(new EmailAddressAttribute().IsValid(txtEmail.Text)) & !String.IsNullOrWhiteSpace(txtEmail.Text))
                {
                    MessageBox.Show("Emailul trebuie sa fie valid!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string clientName    = txtClientName.Text;
                string clientSurname = txtClientSurname.Text;
                string clientCode    = txtClientCode.Text;
                string phoneNo       = String.IsNullOrWhiteSpace(txtPhoneNo.Text) ? null : txtPhoneNo.Text;
                string email         = String.IsNullOrWhiteSpace(txtEmail.Text) ? null : txtEmail.Text;;
                bool   status        = true;

                bl.AddClient(clientName, clientSurname, clientCode, phoneNo, email, out status);
                if (status)
                {
                    MessageBox.Show("Clientul " + clientName + " " + clientSurname + " a fost adaugat cu succes");
                    List <ClientModule> list = bl.GetClientList(-1, "", "", clientCode, "", "");
                    Save(list[0].ClientId);//RECURSIVITATE
                }
                //MessageBox.Show("Clientul " + clientName + " " + clientSurname + " a fost adaugat cu succes");
                else
                {
                    MessageBox.Show("Codul de client exista deja!");
                }
            }
        }