示例#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 Staff's CMND !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Staff's Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                for (int i = 0; i < dgvStaff.Rows.Count; i++)
                {
                    string t = txtID.Text.Trim();
                    if (t == dgvStaff.Rows[i].Cells["ID"].Value.ToString())
                    {
                        MessageBox.Show("Existed '" + t + "', please type another one !");
                        txtID.ResetText();
                        txtChucVu.ResetText();
                        txtName.ResetText();
                        txtID.Focus();
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                try
                {
                    // Thực hiện lệnh
                    BLStaff blStaff = new BLStaff();
                    if (this.txtID.Text != "")
                    {
                        blStaff.AddStaff(this.txtID.Text, this.txtName.Text, this.txtChucVu.Text, this.dtpBirthDate.Text, this.cbFemale.Checked.ToString(), 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 Staff's CMND !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Staff's Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                // Thực hiện lệnh
                BLStaff blStaff = new BLStaff();
                blStaff.UpdateStaff(this.txtID.Text, this.txtName.Text, this.txtChucVu.Text, this.dtpBirthDate.Text, this.cbFemale.Checked.ToString(), 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
        }