예제 #1
0
        public List <shoppingCart> UpdateCartItems()
        {
            using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
            {
                String cartId = usersShoppingCart.GetCatrId();

                ShoppingCartActions.ShoppingCartUpdates[] cartUpdates = new ShoppingCartActions.ShoppingCartUpdates[CartList.Rows.Count];
                for (int i = 0; i < CartList.Rows.Count; i++)
                {
                    IOrderedDictionary rowValues = new OrderedDictionary();
                    rowValues = GetValues(CartList.Rows[i]);
                    cartUpdates[i].ProductId = Convert.ToInt32(rowValues["productID"]);

                    CheckBox cbRemove = new CheckBox();
                    cbRemove = (CheckBox)CartList.Rows[i].FindControl("Remove");
                    cartUpdates[i].RemoveItem = cbRemove.Checked;

                    TextBox quantityTextBox = new TextBox();
                    quantityTextBox = (TextBox)CartList.Rows[i].FindControl("PurchaseQuantity");
                    cartUpdates[i].PurchaseQuantity = Convert.ToInt16(quantityTextBox.Text.ToString());
                }
                usersShoppingCart.UpdateShoppingCartDatabase(cartId, cartUpdates);
                CartList.DataBind();
                lblTotal1.Text = String.Format("{0:c}", usersShoppingCart.GetTotal());

                return(usersShoppingCart.CartItems());
            }
        }
예제 #2
0
        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();
        }