private void button_Them_Click_1(object sender, EventArgs e) { if (ValidateChildren(ValidationConstraints.Enabled)) { var y = new DanhSachSach(); y.MaSach = TaoMa(); y.TheLoai = Kho_TheLoai.Text; y.TenSach = Kho_TenSach.Text; y.TacGia = Kho_TacGia.Text; y.NamXuatBan = int.Parse(Kho_NamXuatBan.Value.ToString()); y.NhaXuatBan = Kho_NhaXuatBan.Text; y.TriGia = int.Parse((double.Parse(Kho_Gia.Value.ToString()) * 1000).ToString()); y.NgayNhap = DateTime.Today; y.MaNgNhan = Mathukho; y.TinhTrang = "Còn"; y.DanhGia = 0; y.LuotDanhGia = ""; y.MoTa = text_MoTA.Text; if (pictureBox1.Image != null) { Image anh = pictureBox1.Image; var byte_anh = ImageToByteArray(anh); y.Anh = byte_anh; } /* Image Y = pictureBox1.BackgroundImage; * var Z = ImageToByteArray(Y); * SaveFile A = new SaveFile(); * A.Name = "Yes"; * A.Files = Z; * x.SaveFiles.Add(A); * x.SaveChanges();*/ qltv.DanhSachSaches.Add(y); qltv.SaveChanges(); Reset_ThuocTinh(); MessageBox.Show("Thêm thành công ", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } else { MessageBox.Show("Thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } HienThi(); LoadForm(); // MessageBox.Show(i.ToString()); ThongKe(); }
private void button_Sua_Click_1(object sender, EventArgs e) { if (ValidateChildren(ValidationConstraints.Enabled)) { DanhSachSach x = qltv.DanhSachSaches.SingleOrDefault(p => p.MaSach == Kho_MaSach.Text); x.TenSach = Kho_TenSach.Text; x.TheLoai = Kho_TheLoai.Text; x.TacGia = Kho_TacGia.Text; x.NamXuatBan = int.Parse(Kho_NamXuatBan.Value.ToString()); x.NhaXuatBan = Kho_NhaXuatBan.Text; x.TriGia = int.Parse((double.Parse(Kho_Gia.Value.ToString()) * 1000).ToString()); x.MoTa = text_MoTA.Text; /// x.Anh = ImageToByteArray(pictureBox1.Image); if (pictureBox1.Image != null) { x.Anh = ImageToByteArray(pictureBox1.Image); } /*string sql_ed = $"update DanhSachSach set TenSach=" + * $"N'{Kho_TenSach.Text}',TacGia=N'{Kho_TacGia.Text}'," + * $"TheLoai=N'{Kho_TheLoai.Text}',NamXuatBan={Kho_NamXuatBan.Value}," + * $"NhaXuatBan=N'{Kho_NhaXuatBan.Text}',TriGia={Kho_Gia.Value * 1000}" + * $"where MaSach='{Kho_MaSach.Text}'"; * SqlCommand cmd = new SqlCommand(sql_ed, con); * cmd.ExecuteNonQuery();*/ try { qltv.DanhSachSaches.AddOrUpdate(x); qltv.SaveChanges(); MessageBox.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); HienThi(); } catch { MessageBox.Show("Sửa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Sửa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } }