Beispiel #1
0
        private void btnMode_Click(object sender, EventArgs e)
        {
            switch (btnMode.Text)
            {
            case "Tìm":
                #region TÌM
                if (rbtnFindbySerial.Checked)
                {
                    try
                    {
                        table          = Book.findBookBySerial(txtSerial.Text);
                        txtSerial.Text = table.Rows[0][0].ToString();
                        txtName.Text   = table.Rows[0][1].ToString();
                        txtAuthor.Text = table.Rows[0][2].ToString();
                        txtPH.Text     = table.Rows[0][3].ToString();
                        txtAmount.Text = table.Rows[0][4].ToString();
                        //img
                        if (table.Rows[0][5] == DBNull.Value)
                        {
                            ptbImg.Image = ptbImg.InitialImage;
                        }
                        else
                        {
                            byte[]       img = (byte[])table.Rows[0][5];
                            MemoryStream ms  = new MemoryStream(img);
                            ptbImg.Image = Image.FromStream(ms);
                        }
                        //
                        txtTag.Text                  = table.Rows[0][6].ToString();
                        tsbtnDelMode.Enabled         = tsbtnUpdateMode.Enabled = true;
                        xóaToolStripMenuItem.Enabled = sửaToolStripMenuItem.Enabled = true;
                    }
                    catch (Exception ex)
                    {
                        clear();
                        MessageBox.Show("Không tìm thấy sách", "Thất bại!");
                    }
                }
                else
                {
                    try
                    {
                        table          = Book.findBookByName(txtName.Text);
                        txtSerial.Text = table.Rows[0][0].ToString();
                        txtName.Text   = table.Rows[0][1].ToString();
                        txtAuthor.Text = table.Rows[0][2].ToString();
                        txtPH.Text     = table.Rows[0][3].ToString();
                        txtAmount.Text = table.Rows[0][4].ToString();
                        //img
                        try
                        {
                            if (table.Rows[0][5] == DBNull.Value)
                            {
                                ptbImg.Image = ptbImg.InitialImage;
                            }
                            else
                            {
                                byte[]       img = (byte[])table.Rows[0][5];
                                MemoryStream ms  = new MemoryStream(img);
                                ptbImg.Image = Image.FromStream(ms);
                            }
                            //
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        txtTag.Text                  = table.Rows[0][6].ToString();
                        tsbtnDelMode.Enabled         = tsbtnUpdateMode.Enabled = true;
                        xóaToolStripMenuItem.Enabled = sửaToolStripMenuItem.Enabled = true;
                    }
                    catch (Exception ex)
                    {
                        clear();
                        MessageBox.Show("Không tìm thấy sách", "Thất bại!");
                    }
                }
                setAutoComplete();
                #endregion TÌM
                break;

            case "Thêm":
                #region THÊM
                if ((txtAuthor.TextLength * txtName.TextLength * txtPH.TextLength * txtSerial.TextLength * txtTag.TextLength * imgLogcation.Length) == 0)
                {
                    MessageBox.Show("Bạn cần điền đầy đủ thông tin cho sách", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                if (Book.insertBook(txtSerial.Text, txtName.Text, txtAuthor.Text, txtPH.Text, (int)txtAmount.Value, imgLogcation, txtTag.Text))
                {
                    MessageBox.Show("Thêm sách thành công!", "Thành công");
                }
                else
                {
                    MessageBox.Show("Thêm sách thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                #endregion THÊM
                clear();
                setAutoComplete();
                break;

            case "Xóa":
                if (MessageBox.Show("Bạn có chắc muốn xóa sách?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    if (Book.deleteBook(txtSerial.Text))
                    {
                        MessageBox.Show("Xóa sách thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Không xóa được sách", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                setAutoComplete();
                break;

            case "Sửa":
                #region SỬA
                if (MessageBox.Show("Bạn có chắc muốn sửa thông tin của sách?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    if ((txtAuthor.TextLength * txtName.TextLength * txtPH.TextLength * txtSerial.TextLength * txtTag.TextLength) == 0 || (imgLogcation.Length == 0 && table.Rows[0][5] == DBNull.Value))
                    {
                        MessageBox.Show("Bạn cần điền đầy đủ thông tin cho sách", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (!Book.updateBook(txtSerial.Text, txtName.Text, txtAuthor.Text, txtPH.Text, (int)txtAmount.Value, imgLogcation, txtTag.Text))
                        {
                            if (!Book.updateBook(txtSerial.Text, txtName.Text, txtAuthor.Text, txtPH.Text, (int)txtAmount.Value, (byte[])table.Rows[0][5], txtTag.Text))
                            {
                                MessageBox.Show("Sửa sách thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                break;
                            }
                        }
                        MessageBox.Show("Sửa sách thành công!", "Thành công");
                    }
                }
                setAutoComplete();
                break;

                #endregion SỬA
            default:
                break;
            }
        }