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);
}
}
}
}