コード例 #1
0
        private void pbSave_Click(object sender, EventArgs e)
        {
            // Mở kết nối
            // Thêm dữ liệu
            if (Them)
            {
                if (this.txtServiceID.Text == "" || this.txtServiceName.Text == "")
                {
                    if (this.txtServiceID.Text == "")
                    {
                        MessageBox.Show("Please fill in Service ID !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Service Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                for (int i = 0; i < dgvService.Rows.Count; i++)
                {
                    string t = txtServiceID.Text.Trim();
                    if (t == dgvService.Rows[i].Cells["ID"].Value.ToString().Trim())
                    {
                        MessageBox.Show("Existed '" + t + "', please type another one !");
                        txtServiceID.ResetText();
                        txtPrice.ResetText();
                        txtServiceName.ResetText();
                        txtUnit.ResetText();
                        txtServiceID.Focus();
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                try
                {
                    // Thực hiện lệnh
                    BLService blService = new BLService();
                    if (this.txtServiceID.Text != "")
                    {
                        float Price = 0;
                        if (this.txtPrice.Text != "")
                        {
                            Price = float.Parse(this.txtPrice.Text);
                        }
                        blService.AddService(this.txtServiceID.Text, this.txtServiceName.Text, Price, this.txtUnit.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("Added failed!");
                }
            }
            else
            {
                if (this.txtServiceID.Text == "" || this.txtServiceName.Text == "")
                {
                    if (this.txtServiceID.Text == "")
                    {
                        MessageBox.Show("Please fill in Sẻvice ID !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Please fill in Service Name !");
                        pbCancel_Click(sender, e);
                        return;
                    }
                }
                // Thực hiện lệnh
                BLService blService = new BLService();
                blService.UpdateService(this.txtServiceID.Text, this.txtServiceName.Text, float.Parse(this.txtPrice.Text), this.txtUnit.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
        }