//menambahkan item kedalam shopping cart public void AddItem(string cartID, int bookID, int qty) { //mencari data yg akan ditambah apakah sudah ada di shopping cart var result = (from c in db.ShoppingCarts where c.CartID == cartID && c.BookID == bookID select c).SingleOrDefault(); //jika tidak ditemukan maka tambahkan ke cart if (result == null) { ShoppingCart newCart = new ShoppingCart() { CartID = cartID, Quantity = qty, BookID = bookID, DateCreated = DateTime.Now }; db.ShoppingCarts.Add(newCart); } else //jika ditemukan update quantitynya { result.Quantity += qty; } db.SaveChanges(); }
//menambahkan data Category public void Create(Category category) { try { db.Categories.Add(category); db.SaveChanges(); } catch (Exception ex) { throw new Exception("Error: " + ex.Message); } }
public bool SubmitOrder(string username) { try { Order newOrder = new Order() { CustomerName = username, OrderDate = DateTime.Now, ShipDate = HitungTanggalKirim() }; var addResult = db.Orders.Add(newOrder); db.SaveChanges(); orderId = addResult.OrderID; //menambahkan data order detail AddToCart objAddCart = new AddToCart(); string cartID = objAddCart.GetShoppingCartId(); var results = from s in db.ShoppingCarts.Include("Book") where s.CartID == cartID select s; foreach (var shoppingCart in results) { OrderDetail od = new OrderDetail() { OrderID = newOrder.OrderID, BookID = shoppingCart.BookID, Quantity = shoppingCart.Quantity, Price = shoppingCart.Book.Price }; db.OrderDetails.Add(od); //mendelete data pada ShoppingCart var result = (from s in db.ShoppingCarts where s.CartID == shoppingCart.CartID && s.BookID == shoppingCart.BookID select s).SingleOrDefault(); if (result != null) { db.ShoppingCarts.Remove(result); } } db.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } return(true); }
// The id parameter name should match the DataKeyNames value set on the control public void gvCart_UpdateItem(int RecordID) { ShoppingCart item = db.ShoppingCarts.Find(RecordID); if (item == null) { ModelState.AddModelError("", String.Format("Item dengan id {0} tidak ditemukan", RecordID)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); GetTotalPrice(); } }
protected void btnReview_Click(object sender, EventArgs e) { bookId = Page.RouteData.Values["id"].ToString(); var rating = (AjaxControlToolkit.Rating)lvAddReview.FindControl("myRating"); var comments = (TextBox)lvAddReview.FindControl("txtComment"); Review newReview = new Review() { BookID = Convert.ToInt32(bookId), CustomerName = Context.User.Identity.GetUserName(), CustomerEmail = Context.User.Identity.GetUserName(), Rating = rating.CurrentRating, Comments = comments.Text }; try { db.Reviews.Add(newReview); db.SaveChanges(); gvReview.DataBind(); } catch (Exception ex) { ltMessage.Text = string.Format("<p class='alert alert-danger'>{0}</p>", "Error: " + ex.Message); } }
protected void btnSubmit_Click(object sender, EventArgs e) { //menambah data try { var newBook = new Book() { Title = txtTitle.Text, CategoryID = Convert.ToInt32(ddCategory.SelectedValue), ISBN = txtIsbn.Text, Price = Convert.ToDecimal(txtPrice.Text), Publisher = txtPublisher.Text, AuthorID = Convert.ToInt32(ddAuthor.SelectedValue), PublicationDate = Convert.ToDateTime(txtDate.Text), CoverImage = fpCover.FileName, Description = txtDescription.Text }; db.Books.Add(newBook); db.SaveChanges(); string filenameCover = System.IO.Path.GetFileName(fpCover.FileName); fpCover.SaveAs(Server.MapPath("~/Catalog/Images/") + filenameCover); string filenameThumbs = System.IO.Path.GetFileName(fpThumbs.FileName); fpThumbs.SaveAs(Server.MapPath("~/Catalog/Images/Thumbs/") + filenameThumbs); ltMessage.Text = string.Format("<p class='alert alert-success'>{0}</p>", "Data Buku berhasil ditambah !"); } catch (Exception ex) { ltMessage.Text = string.Format("<p class='alert alert-danger'>{0}</p>", "Error : " + ex.Message); } }