Esempio n. 1
0
    protected void btnAddtoCart_Click(object sender, EventArgs e)
    {
        int quantitySelected = 0;

        try{
            quantitySelected = Convert.ToInt16(txtQuantity.Text);

            if (Page.IsValid)
            {
                Cake         selectedProduct = this.GetSelectedProduct();
                CartItemList cart            = CartItemList.GetCart();
                CartItem     cartItem        = cart[selectedProduct.ProductId];


                int index         = 0;
                int alreadyInCart = 0;
                if (cart.Count > 0)
                {
                    while (index != -1)
                    {
                        if (cart.GetProdID(index) == selectedProduct.ProductId)
                        {
                            alreadyInCart = cart.GetQuantity(index);
                        }
                        index = cart.IndexAdvance(index);
                    }
                }
                // checking first to see if we have the amount selected currently on hand
                if (quantitySelected + alreadyInCart <= selectedProduct.OnHand)
                {
                    if (quantitySelected > 0)
                    {
                        // if we do, then we can add to cart and redirect to cart
                        //if item isn't in cart, add it; otherwise, increase its quantity
                        if (cartItem == null)
                        {
                            cart.AddItem(selectedProduct, Convert.ToInt32(txtQuantity.Text));
                        }
                        else
                        {
                            cartItem.AddQuantity(Convert.ToInt32(txtQuantity.Text));
                        }
                        Response.Redirect("~/Cart/Cart.aspx");
                    }
                    else
                    {
                        lblQuan.Text = "Please enter a quantity greater than zero.";
                    }
                }
                else
                {
                    //ask them to order fewer
                    lblQuan.Text = "Sorry, we do not have that many on hand.";
                }
            }
        }
        catch (Exception ex)
        {
            lblQuan.Text = "That's not a valid number.";
        }
    }