public void AddToCart(string name) { ShoppingCartActions Action = new ShoppingCartActions(); Team2_BookDBEntities DB = new Team2_BookDBEntities(); // Retrieve the product from the database. string ShoppingCartId = Action.GetCatrId(); var user = from u in DB.users where u.userName == ShoppingCartId select u; WebDL.user userdata = user.FirstOrDefault(); var book = from b in DB.products where b.productName == name select b; WebDL.product bookdata = book.FirstOrDefault(); var cartItem = DB.shoppingCarts.FirstOrDefault( c => c.user.userName == ShoppingCartId && c.product.productName == name); var product = DB.products.FirstOrDefault(p => p.productName == name); if (cartItem == null) { // Create a new cart item if no cart item exists. cartItem = new shoppingCart { productID = bookdata.productID, quantity = Convert.ToInt32(tbQuantity.Text), userID = userdata.userID, shoppingCartExpiredDate = DateTime.Now.AddDays(7), user = DB.users.SingleOrDefault(u => u.userID == userdata.userID), product = DB.products.SingleOrDefault(p => p.productName == name) }; DB.shoppingCarts.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity. cartItem.quantity += Convert.ToInt32(tbQuantity.Text); } product.productQty -= Convert.ToInt32(tbQuantity.Text); DB.SaveChanges(); }
protected void btnAddToCart_Click(object sender, EventArgs e) { Button btn = (Button)sender; string productName = btn.CssClass; using (WebDL.Team2_BookDBEntities context = new WebDL.Team2_BookDBEntities()) { string userNameFromSession = Session["userName"].ToString(); var data = from p in context.products where p.productName == productName select p; var data2 = from u in context.users where u.userName == userNameFromSession select u; WebDL.user userGetFromSession = data2.First(); WebDL.product pToBeAdded = data.First(); WebDL.shoppingCart sc = new WebDL.shoppingCart(); sc.product = pToBeAdded; sc.quantity = 1; sc.userID = userGetFromSession.userID; sc.shoppingCartExpiredDate = DateTime.Now.Date; context.shoppingCarts.Add(sc); context.SaveChanges(); lbMsg.Text = "product: " + productName + " is added to shopping cart"; } }
protected void Page_Load(object sender, EventArgs e) { //Session["userID"] = "Preethi"; //Session["productName"] = "How to Deal with Anger"; Team2_BookDBEntities DB = new Team2_BookDBEntities(); ShoppingCartActions Action = new ShoppingCartActions(); string pName = Action.getProductName(); var data1 = from b in DB.products where b.productName == pName select b; WebDL.product book = data1.FirstOrDefault(); var data2 = from c in DB.categories where c.categoryID == book.categoryID select c; WebDL.category cat = data2.First(); imgCover.ImageUrl = book.productImage; lbBooktitle.Text = book.productName; tbDescription.Text = book.productDescription; lbCategory1.Text = cat.categoryName.ToString(); lbPrice1.Text = book.productPrice.ToString(); lbbookDiscount.Text = book.productDiscount.ToString(); lbBookqty.Text = book.productQty.ToString(); lbDiscountedPrice1.Text = book.productDiscountedPrice.ToString(); }