Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 25th, 2006, 01:44 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Session & secuirty issue of shopping cart

Hi, i am having a problem. As i am new to programming i tried to come out with my own online store, as i was programming my shopping cart, although it is working fine, but my friend said that the way i programmed the part that calculates the total amount of the shopping cart is wrong. He said that he is able to make the total amount of the cart to 0 before check out. But he would not tell me the reason. Is there anyone who knows where the problem might be in the codes?? Thanks

Code:
private void Page_Load(object sender, System.EventArgs e)
        {

            if(!IsPostBack)
            {
                //get arraylist from session Create one if there is none
                ArrayList cart = (ArrayList)Session["cart"];
                if(cart==null)
                Session["cart"] = new ArrayList();

                if (Request.QueryString["id"]!=null)
                {
                    //get values from DataList
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    string name = Request.QueryString["name"];
                    decimal price = Convert.ToDecimal(Request.QueryString["price"]);
                    string stockLeft = Request.QueryString["StockLeft"];

                    //create CartItem object and add to arraylist
                    cart = (ArrayList)Session["cart"];
                    bool found=false;
                    foreach(CartItem item in cart)
                    {
                        if(id==item.ProductID)
                        {
                            item.Quantity++;
                            found = true;
                            break;
                        }
                    }

                    if(!found)
                    {
                        if(stockLeft=="No Stock")
                        {
                            lblStatus.Text = "The product is currently not in stock.";
                        }
                        else 
                        {
                            CartItem item = new CartItem(id,name,price,1,stockLeft);
                            cart.Add(item);
                        }
                    }
                }

                //Calcualte the total amount of all the items in the shopping cart.
                CalculateTotal();

                //Bind arraylist to DataGrid
                dgCart.DataSource=cart;
                dgCart.DataBind();

            }

        }



        private void dgCart_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
        {
            dgCart.EditItemIndex = -1;
            dgCart.DataSource=(ArrayList)Session["cart"];
            dgCart.DataBind();
        }

        private void dgCart_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
        {
            //get row selected
            int index = e.Item.ItemIndex;

            //get arraylist from session
            ArrayList cart = (ArrayList)Session["cart"];

            //remove item from arraylist
            cart.RemoveAt(index);

            //Calcualte the total amount of all the items in the shopping cart.
            CalculateTotal();

            //reBind arraylist to DataGrid
            dgCart.DataSource = (ArrayList)Session["cart"];
            dgCart.DataBind();

        }

        private void dgCart_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
        {

            dgCart.EditItemIndex=e.Item.ItemIndex;
            dgCart.DataSource=(ArrayList)Session["cart"];
            dgCart.DataBind();

        }

        private void dgCart_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
        {
            //get the row selected 
            int index = e.Item.ItemIndex;

            //get the qty from the txt box
            TextBox tb = (TextBox)e.Item.Cells[3].Controls[0];
            int qty = Convert.ToInt32(tb.Text);

            //get arraylist from session
            ArrayList cart = (ArrayList)Session["cart"];

            //update object qty using the index
            CartItem cItem = (CartItem)cart[index];
            cItem.Quantity = qty;

            //Calcualte the total amount of all the items in the shopping cart.
            CalculateTotal();

            //rebind the datagrid
            dgCart.EditItemIndex = -1;
            dgCart.DataSource = (ArrayList)Session["cart"];
            dgCart.DataBind();

        }



        private void btnCheckOut_Click(object sender, System.EventArgs e)
        {
            int Quantity = 1;
            // retrieves cart and checks if it is empty or not exist
            ArrayList cart = (ArrayList)Session["cart"];
            foreach (CartItem item in cart) 
            {
                Quantity = item.Quantity;
                if (Quantity == 0 ){
                    break;
                }
            }

            if (cart == null || cart.Count == 0 || Quantity == 0)
            {    
                lblTotalPrice.Text = "0";
                lblStatus.Text = "Cart is empty! Not allowed to proceed.";
            }
            else {


                Response.Redirect("/shopperH/checkout.aspx");
            }
        }

        private void btnContinueShopping_Click(object sender, System.EventArgs e)
        {
            Server.Transfer("/shopperH/ProductsListing.aspx");
        }

        //This method Calcualtes the total amount of all the items in the shopping cart.
        private void CalculateTotal()
        {
            ArrayList cart = (ArrayList)Session["cart"];
            cart = (ArrayList)Session["cart"];

            decimal totalPrice =0;
            decimal subTotal=0;

            //To Calculate the total price payable from all then items in the cart.
            foreach (CartItem item in cart) 
            {
                subTotal = item.UnitPrice * item.Quantity;
                totalPrice = totalPrice + subTotal;

                Session["OrderTotal"] = totalPrice;
                lblTotalPrice.Text = Convert.ToString(totalPrice);
            }
        }




    }
}


Similar Threads
Thread Thread Starter Forum Replies Last Post
session in shopping cart page Jane Siu Beginning PHP 1 November 22nd, 2006 01:49 AM
Problem with session on Shopping Cart comicghozt .NET Framework 1.x 6 September 21st, 2006 09:22 AM
shopping cart xipnl BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 June 10th, 2005 07:00 PM
can we manage shopping cart without using session pradeep1976 BOOK: Beginning ASP 3.0 0 April 7th, 2005 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.