|
| 1 | +using GiaoDien.DAO; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Data; |
| 6 | +using System.Drawing; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | + |
| 12 | +namespace GiaoDien |
| 13 | +{ |
| 14 | + public partial class AdjustData : Form |
| 15 | + { |
| 16 | + public AdjustData() |
| 17 | + { |
| 18 | + InitializeComponent(); |
| 19 | + //this.TopMost = true; |
| 20 | + //this.FormBorderStyle = FormBorderStyle.Fixed3D; |
| 21 | + //this.WindowState = FormWindowState.Maximized; |
| 22 | + ShowBookData(); |
| 23 | + } |
| 24 | + void ShowBookData() |
| 25 | + { |
| 26 | + string query = "SELECT MASA 'Mã sách', TENSA 'Tên sách', LOAISA 'Loại sách', NXB 'Nhà xuất bản', TACGIA 'Tác giả', TAIBAN 'Tái bản', SOLUONG 'Số lượng' FROM Book"; |
| 27 | + data.DataSource = DataProvider.Instance.ExecuteQuery(query); |
| 28 | + } |
| 29 | + private void updBtn_Click(object sender, EventArgs e) |
| 30 | + { |
| 31 | + string bookId = bookIDText.Text; |
| 32 | + string bookTitle = bookTitleText.Text; |
| 33 | + string bookType = bookTypeText.Text; |
| 34 | + string publisher = publishText.Text; |
| 35 | + string author = authorText.Text; |
| 36 | + string edition = editText.Text; |
| 37 | + string quantity = quantityText.Text; |
| 38 | + if (IsNullData(bookId, bookTitle, bookType, publisher, author, edition, quantity)) |
| 39 | + MessageBox.Show("Nhập không đủ dữ liệu. Vui lòng kiểm tra lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 40 | + else |
| 41 | + { |
| 42 | + if (!IsValidNumber(edition)) |
| 43 | + MessageBox.Show("Thông tin 'Lần tái bản' không hợp lệ. Vui lòng kiểm tra lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 44 | + else if (!IsValidNumber(quantity)) |
| 45 | + MessageBox.Show("Thông tin 'Số lượng' không hợp lệ. Vui lòng kiểm tra lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 46 | + else |
| 47 | + { |
| 48 | + if (UpdateBook(bookId, bookTitle, bookType, publisher, author, edition, quantity) == 0) |
| 49 | + MessageBox.Show("Mã sách không hợp lệ. Vui lòng kiểm tra lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 50 | + else |
| 51 | + { |
| 52 | + MessageBox.Show("Cập nhật dữ liệu thành công !!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 53 | + ShowBookData(); |
| 54 | + bookTitleText.Clear(); |
| 55 | + bookTypeText.Clear(); |
| 56 | + publishText.Clear(); |
| 57 | + authorText.Clear(); |
| 58 | + editText.Clear(); |
| 59 | + quantityText.Clear(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + bool IsNullData(string bookID, string bookTitle, string bookType, string publisher, string author, string edition, string quantity) |
| 65 | + { |
| 66 | + if (String.IsNullOrEmpty(bookID) || String.IsNullOrEmpty(bookTitle) || String.IsNullOrEmpty(bookType) || String.IsNullOrEmpty(publisher) || String.IsNullOrEmpty(author) || String.IsNullOrEmpty(edition) || String.IsNullOrEmpty(quantity)) |
| 67 | + return true; |
| 68 | + return false; |
| 69 | + } |
| 70 | + bool IsValidNumber(string str) |
| 71 | + { |
| 72 | + if (str.Trim() == "") |
| 73 | + return false; |
| 74 | + for (int i = 0; i < str.Length; i++) |
| 75 | + { |
| 76 | + if (!char.IsNumber(str[i])) |
| 77 | + return false; |
| 78 | + } |
| 79 | + if (Int64.Parse(str) < 0) |
| 80 | + return false; |
| 81 | + return true; |
| 82 | + } |
| 83 | + int UpdateBook(string bookID, string bookTitle, string bookType, string publisher, string author, string edition, string quantity) |
| 84 | + { |
| 85 | + string query = "UPDATE Book SET TENSA = N'" + bookTitle + "', LOAISA = N'" + bookType + "', NXB = N'" + publisher + "', TACGIA = N'" + author + "', TAIBAN = " + edition + ", SOLUONG = " + quantity + " WHERE MASA = '" + bookID + "'"; |
| 86 | + return DataProvider.Instance.ExecuteNonQuery(query); |
| 87 | + } |
| 88 | + private void data_CellClick(object sender, DataGridViewCellEventArgs e) |
| 89 | + { |
| 90 | + |
| 91 | + int index = e.RowIndex; |
| 92 | + if (index >= 0) |
| 93 | + { |
| 94 | + bookIDText.Text = data.Rows[index].Cells["Mã sách"].Value.ToString(); |
| 95 | + bookTitleText.Text = data.Rows[index].Cells["Tên sách"].Value.ToString(); |
| 96 | + bookTypeText.Text = data.Rows[index].Cells["Loại sách"].Value.ToString(); |
| 97 | + publishText.Text = data.Rows[index].Cells["Nhà xuất bản"].Value.ToString(); |
| 98 | + authorText.Text = data.Rows[index].Cells["Tác giả"].Value.ToString(); |
| 99 | + editText.Text = data.Rows[index].Cells["Tái bản"].Value.ToString(); |
| 100 | + quantityText.Text = data.Rows[index].Cells["Số lượng"].Value.ToString(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments