//Master and Detail INSERT public static int SaveDAO(BookRent bookObj, DataTable dt) { BookPOSEntities3 db = new BookPOSEntities3(); using (var transaction = db.Database.BeginTransaction()) { try { var entity = db.Set <BookRent>().Create(); //mapping the values of your view models to data models entity.MemberId = bookObj.MemberId; entity.StartDate = bookObj.StartDate; entity.IssueDate = bookObj.IssueDate; entity.NumberOfDay = bookObj.NumberOfDay; //INSERT MASTER TABLE db.BookRents.Add(entity); int l_return = db.SaveChanges(); long RentID = entity.RentID; //GET MASTER ID if (l_return > 0) { //INSERT DETAIL TABLE for (int i = 0; i < dt.Rows.Count; i++) { var RentDetail = db.Set <RentBookDetail>().Create(); //can not assign null value RentDetail.RentID = RentID; RentDetail.BookID = Convert.ToInt64(dt.Rows[i]["BookID"].ToString()); RentDetail.Status = 1; db.RentBookDetails.Add(RentDetail); l_return = db.SaveChanges(); RentDetail = null; //can not use null vlue } } transaction.Commit(); transaction.Dispose(); db.Dispose(); return(l_return); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }
//Insert New Member public static int SaveNewMember(DataTable dt) { BookPOSEntities3 db = new BookPOSEntities3(); int l_return = 0; using (var transaction = db.Database.BeginTransaction()) { try { //INSERT DETAIL TABLE for (int i = 0; i < dt.Rows.Count; i++) { var Obj = db.Set <Member>().Create(); Obj.MemberCode = dt.Rows[i]["MemberCode"].ToString(); Obj.MemberName = dt.Rows[i]["MemberName"].ToString(); Obj.email = dt.Rows[i]["Email"].ToString(); Obj.City = dt.Rows[i]["City"].ToString(); Obj.Phone = dt.Rows[i]["Phone"].ToString(); Obj.Active = 1; Obj.Address = dt.Rows[i]["Address"].ToString(); db.Members.Add(Obj); l_return = db.SaveChanges(); } transaction.Commit(); transaction.Dispose(); db.Dispose(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } return(l_return); }
//Insert New Book public static int SaveNewBook(DataTable dt) { BookPOSEntities3 db = new BookPOSEntities3(); int l_return = 0; using (var transaction = db.Database.BeginTransaction()) { try { //INSERT DETAIL TABLE for (int i = 0; i < dt.Rows.Count; i++) { var bookObj = db.Set <Book>().Create(); bookObj.BookCode = dt.Rows[i]["BookCode"].ToString(); bookObj.BookName = dt.Rows[i]["BookName"].ToString(); bookObj.CategoryId = Convert.ToInt64(dt.Rows[i]["Category"]); bookObj.Author = dt.Rows[i]["Author"].ToString(); bookObj.ISBN = dt.Rows[i]["ISBN"].ToString(); db.Books.Add(bookObj); l_return = db.SaveChanges(); } transaction.Commit(); transaction.Dispose(); db.Dispose(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } return(l_return); }