// Chuyển giá trị trong Textbox thành tblSach public tblSach Textbox_To_tblSach() { try { tblSach result = new tblSach(); result.id = (int)this.numbid.Value; result.tenSach = (this.txttenSach.Text == null ? null : (string)(this.txttenSach.Text)); result.giaBan = (int)this.numbgiaBan.Value; result.soLuong = (int)this.numbsoLuong.Value; return(result); } catch (Exception ex) { ShowMessage(ex.Message, false); } // Không load được hoặc xảy ra lỗi return(null); }
// Chuyển giá trị trong DataGridView thành tblSach public tblSach DataGridView_To_tblSach(int index) { try { tblSach result = new tblSach(); var id = this.dgvData.Rows[index].Cells["id"].Value; if (id != null) { result.id = int.Parse(id.ToString()); } var tenSach = this.dgvData.Rows[index].Cells["tenSach"].Value; if (tenSach != null) { result.tenSach = (tenSach == null ? null : (string)(tenSach)); } var giaBan = this.dgvData.Rows[index].Cells["giaBan"].Value; if (giaBan != null) { result.giaBan = (giaBan == null ? null : (int?)int.Parse(giaBan.ToString())); } var soLuong = this.dgvData.Rows[index].Cells["soLuong"].Value; if (soLuong != null) { result.soLuong = (soLuong == null ? null : (int?)int.Parse(soLuong.ToString())); } return(result); } catch (Exception ex) { ShowMessage(ex.Message, false); } // Không load được hoặc xảy ra lỗi return(null); }
public ActionResult Create(tblSach model, HttpPostedFile filePost, FormCollection collection) { setControl(); string fileLocation = ""; if (Request.Files["filePost"].ContentLength <= 0) { model.IMAGE = ""; } ModelState["filePost"].Errors.Clear(); //if (ModelState.IsValid == true) //{ if (Request.Files["filePost"].ContentLength > 0) { string fileExtension = System.IO.Path.GetExtension(Request.Files["filePost"].FileName); fileLocation = Server.MapPath("~/Content/") + Request.Files["filePost"].FileName; if (System.IO.File.Exists(fileLocation)) { System.IO.File.Delete(fileLocation); } Request.Files["filePost"].SaveAs(fileLocation); } model.MA_NXB = collection["cboNXB"].ToString(); model.MA_TACGIA = collection["cboTacGia"].ToString(); model.MA_THELOAI = collection["cboTLoai"].ToString(); model.MO_TA = "1"; model.TRANG_THAI = 1; int iContent = fileLocation.IndexOf("Content"); if (iContent > 0) { model.IMAGE = @"\" + fileLocation.Substring(iContent, fileLocation.Length - iContent); } db = new TRUONGHOCDbContext(); var item = db.tblSaches.Where(x => x.TRANG_THAI == 1).ToList(); var result = false; foreach (var itemSach in item) { if (model.MA_SACH == itemSach.MA_SACH) { result = false; break; } else { result = true; } } if (result == false) { ModelState.AddModelError("", "Mã sách đã tồn tại"); setControl(); return(View()); } else if (collection["cboNXB"].ToString() == "-1" || collection["cboTacGia"].ToString() == "-1" || collection["cboTLoai"].ToString() == "-1" || model.MA_SACH == null) { ModelState.AddModelError("", "Lỗi kiểm tra dữ liệu"); setControl(); return(View()); } else { db.tblSaches.Add(model); db.SaveChanges(); return(RedirectToAction("Index", "Book")); } }