protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["User"] == null) { Panel3.Visible = true; Panel4.Visible = false; } else { Panel3.Visible = false; Panel4.Visible = true; } if (Page.RouteData.Values["ID"] != null) { string id = Page.RouteData.Values["ID"].ToString().Trim(); List <Sach> li_sach = new List <Sach>(); sach = tbl_sach.GetByID(id); if (sach != null) { ten = sach.TenFile; li_sach.Add(sach); if (sach.TenFile.Trim() == "") { btnTai.Visible = false; } else { btnTai.Visible = true; } } else { li_sach = tbl_sach.SearchSachObj(Indext.key); } if (li_sach.Count > 1) { Panel1.Visible = false; Panel2.Visible = true; DataList1.DataSource = li_sach; DataList1.DataBind(); } else if (li_sach.Count == 0) { Panel1.Visible = false; Panel2.Visible = false; WebMsgBox.Show("Không tìm thấy sách"); } else if (li_sach.Count == 1) { Panel1.Visible = true; Panel2.Visible = false; FormView1.DataSource = li_sach; FormView1.DataBind(); } } }
//xoa du lieu public void Delete(Sach obj) { db.Saches.Attach(obj); db.Saches.Remove(obj); db.SaveChanges(); }
//Them du lieu public Sach Insert(Sach obj) { db.Saches.Add(obj); db.SaveChanges(); return(obj); }
//Cap nhap du lieu public void Update(Sach obj) { db.Saches.Attach(obj); db.Entry(obj).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); }
//Thêm File Excel protected void btnThem_Click(object sender, EventArgs e) { if (FileUpload2.HasFile) { if (Path.GetExtension(FileUpload2.FileName) == ".xlsx") { try { using (var excel = new ExcelPackage(FileUpload2.PostedFile.InputStream)) { //gọi bảng chứa dữ liệu excel ExcelPackage.LicenseContext = LicenseContext.Commercial; var ws = excel.Workbook.Worksheets.First(); int rowCount = ws.Dimension.End.Row; //get row count for (int row = 5; row <= rowCount; row++) { if (ws.Cells[row, 3].Text.Trim() == "") { break; } Sach sach = tbl_sach.GetByName(ws.Cells[row, 3].Text.Trim()); if (sach == null) { ThemNhomSach(ws.Cells[row, 5].Text.Trim()); ThemNhaXuatBan(ws.Cells[row, 8].Text.Trim()); sach = new Sach(); sach.MaSach = CreateKey(); sach.TenSach = ws.Cells[row, 3].Text.Trim(); sach.TacGia = ws.Cells[row, 4].Text.Trim(); sach.NhomSach = ws.Cells[row, 5].Text.Trim(); sach.GhiChu = ws.Cells[row, 6].Text.Trim(); sach.SoLuong = Int32.Parse(ws.Cells[row, 7].Text.Trim()); sach.NhaXuatBan = ws.Cells[row, 8].Text.Trim(); sach.NoiXuatBan = ws.Cells[row, 9].Text.Trim(); sach.NamXuatBan = ws.Cells[row, 10].Text.Trim(); if (ws.Cells[row, 11].Text.Trim() != "") { sach.NgayNhap = ws.Cells[row, 11].Text.Trim(); } else { sach.NgayNhap = DateTime.Now.ToString("dd/MM/yyyy"); } sach.TenFile = ""; tbl_sach.Insert(sach); } else { ThemNhomSach(sach.NhomSach); ThemNhaXuatBan(sach.NhaXuatBan); sach.TenSach = ws.Cells[row, 3].Text.Trim(); sach.TacGia = ws.Cells[row, 4].Text.Trim(); sach.NhomSach = ws.Cells[row, 5].Text.Trim(); sach.GhiChu = ws.Cells[row, 6].Text.Trim(); sach.SoLuong = Int32.Parse(ws.Cells[row, 7].Text.Trim()); sach.NhaXuatBan = ws.Cells[row, 8].Text.Trim(); sach.NoiXuatBan = ws.Cells[row, 9].Text.Trim(); sach.NamXuatBan = ws.Cells[row, 10].Text.Trim(); sach.NgayNhap = ws.Cells[row, 11].Text.Trim(); tbl_sach.Update(sach); } } } WebMsgBox.Show("Thêm Vào Thành Công"); } catch (Exception ex) { WebMsgBox.Show("Lỗi nhập file, hãy kiểm tra lại file của bạn"); } } else { WebMsgBox.Show("Lỗi Hệ thống: chỉ nhận file Excel"); } } else { WebMsgBox.Show("Hãy chọn tệp"); } ShowData(); }