public static void DeleteBooks(int BookId) { using (Mybooks entities = new Mybooks()) { Book book = entities.Books.Where(p => p.BookID == BookId).First <Book>(); entities.Books.Remove(book); entities.SaveChanges(); } }
public void Transaction(int userid, List <Book> cartitems) { Random r = new Random(); int rand = r.Next(1000000, 9999999); Tran t = new Tran(); t.transID = rand.ToString(); t.UserID = userid; t.transDate = DateTime.Today; mb.Trans.Add(t); foreach (Book b in cartitems) { TransDetail td = new TransDetail(); td.transID = rand.ToString(); td.BookID = b.BookID; td.Discount = (decimal)GetDiscountRate(DateTime.Today.Date); td.Amount = b.Price - (b.Price * td.Discount); mb.TransDetails.Add(td); UpdateStock(b.BookID); } mb.SaveChanges(); }
protected void BtnClick(object sender, EventArgs e) { mb = new Mybooks(); Button b = (Button)sender; int bid = Convert.ToInt32(b.ID); CartBook toDel = mb.CartBooks.Where(x => (x.UserID == userid) && (x.BookID == bid)).First(); mb.CartBooks.Remove(toDel); mb.SaveChanges(); Response.Redirect("~/RegisteredUsers/ViewCart.aspx/"); }
public static void AddBook(string Title, string ISBN, string Author, int Cat, int Stock, decimal Price) { Mybooks entities = new Mybooks(); Book bk = new Book(); bk.Title = Title; bk.Author = Author; bk.ISBN = ISBN; bk.Price = Price; bk.Stock = Stock; bk.CategoryID = Cat; entities.Books.Add(bk); entities.SaveChanges(); }
public static void UpdateBooks(int BookId, string Title, int CategoryID, string Author, string ISBN, int Stock, decimal Price) { using (Mybooks entities = new Mybooks()) { Book book = entities.Books.Where(p => p.BookID == BookId).First <Book>(); book.Title = Title; book.CategoryID = CategoryID; book.Author = Author; book.ISBN = ISBN; book.Stock = Stock; book.Price = Price; entities.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { if (Session["role"] == null || (string)Session["role"] != "user") { Response.Redirect("~/UserLogin.aspx"); } else { userid = (int)Session["userid"]; } mb = new Mybooks(); if (Request.QueryString["id"] != null) { bid = Convert.ToInt32(Request.QueryString["id"]); if ((mb.Books.Where(x => x.BookID == bid).Count() > 0) && (new Work().CheckStock(bid) > 0) && (mb.CartBooks.Where(x => x.BookID == bid) .Where(x => x.UserID == userid) .Count() < 1)) { CartBook newitem = new CartBook(); newitem.UserID = userid; newitem.BookID = bid; mb.CartBooks.Add(newitem); mb.SaveChanges(); } Response.Redirect("~/RegisteredUsers/ViewCart.aspx"); } using (mb) { int count = mb.CartBooks.Where(x => x.UserID == userid).Count(); List <Book> cart = mb.CartBooks.Where(x => x.UserID == userid) .Select(x => x.Book).ToList(); if (count > 0) { Session["cartitems"] = cart; Work o = new Work(); double checkDiscountValue = o.GetDiscountRate(DateTime.Today.Date); if (checkDiscountValue > 0) { DiscountLabel.Visible = true; DiscountLabel.Text = "You have a discount of " + (checkDiscountValue * 100) + "%"; } else { DiscountLabel.Visible = false; } cartstatus.InnerText = count + " book(s) in cart."; double discount = new Work().GetDiscountRate(DateTime.Today.Date); foreach (Book b in cart) { double price = (double)b.Price; double amount = price - (price * discount); HtmlGenericControl newDiv = new HtmlGenericControl("DIV"); HtmlGenericControl details = new HtmlGenericControl("DIV"); HtmlGenericControl imagediv = new HtmlGenericControl("DIV"); HtmlGenericControl third = new HtmlGenericControl("DIV"); Image img = new Image(); Button btn = new Button(); newDiv.Attributes["class"] = "row well"; img.ImageUrl = "~/images/" + b.ISBN + ".jpg"; img.Style.Add("position", "relative"); img.Style.Add("float", "right"); img.Style.Add("margin-right", "40px"); imagediv.Attributes["class"] = "col-xs-4 col-sm-4 col-md-4"; details.InnerHtml = "<br /><br /><br />" + "<strong>" + b.Title + "</strong>" + "<br /><br />" + b.Author + "<br /><br />" + price.ToString(); details.Attributes["class"] = "col-xs-6 col-sm-6 col-md-6"; third.InnerHtml = "<br /><br />" + "<p class=\"text-center\" style=\"font-size: 200%\"><strong>" + String.Format("{0:c}", amount) + "</strong></p>"; third.InnerHtml += "<br /><br /><br />"; third.Attributes["class"] = "col-xs-2 col-sm-2 col-md-2"; btn.Text = "Remove"; btn.ID = b.BookID.ToString(); btn.Attributes.Add("class", "btn btn-danger"); btn.Click += BtnClick; cartdiv.Controls.Add(newDiv); newDiv.Controls.Add(imagediv); imagediv.Controls.Add(img); newDiv.Controls.Add(details); newDiv.Controls.Add(third); third.Controls.Add(btn); } LblTotal.Text = "Total ~ " + String.Format("{0:c}", new Work().CalculateTotal(cart)); } else { cartstatus.InnerText = "There is no book in cart."; btncheckout.Enabled = false; } } }