I am doing a cart for my project and I have a problem now. I have a catalogue page and a description page. Whe user select a product from the catalogue it will go to the description page which will have the addtocart button. The user will them be able to
add to cart. However the user will be able to go back to select a new product and then addtocart from the description page.
What should I use so that the info added to the cart will still be there even after the user go back to select a new product? I have an idea about using arraylist but how should i start?
On your description page you are creating a brand new array so you don't have anything but the last value as there is only one value that will be in your session.
try the following:
ArrayList idList = null;
if(Session["InstallationId"] != null)
idList = (ArrayList)Session["InstallationId"];
else
idList = new ArrayList();
This code checks to see if you already have an existing array in the session first, else it creates a new one.
You may want to try a different structure though for storing your data. A simpler one if you are doing product id, and product quantity is a NameValueCollection. It creates a name-value pair which would be useful here. That would avoid trying to figure out
which is the product id and which is the quantity. In this case, the Product Id would be a key that you can get values for, and the quantity would be the value.
// add items to the collection like so:
idList.Add(ProdID,ProQty);
To list all the items and values you can loop through the object to get both the key and the value
// example of a simple loop that lets you get a key, or a value based on an ordinal
for(int i = 0; i < idList.Count; i++)
{
// get a key value
idList.Keys[i];
// get a value
idList[i];
}
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Marked as answer by weihan1394 on Jun 22, 2012 01:37 PM
weihan1394
Member
9 Points
53 Posts
Add to Cart
Jun 22, 2012 12:11 PM|LINK
Hi,
I am doing a cart for my project and I have a problem now. I have a catalogue page and a description page. Whe user select a product from the catalogue it will go to the description page which will have the addtocart button. The user will them be able to add to cart. However the user will be able to go back to select a new product and then addtocart from the description page.
What should I use so that the info added to the cart will still be there even after the user go back to select a new product? I have an idea about using arraylist but how should i start?
weihan1394
Member
9 Points
53 Posts
Re: Add to Cart
Jun 22, 2012 12:31 PM|LINK
This is what I have now. But it will only get the latest update of the arraylist.
Description page:
ArrayList idList = new ArrayList(); idList.Add(ProdID); idList.Add(ProQty); Session["InstallationId"] = idList; Response.Write("<script>if(confirm('Added to cart!')){} else { return false; }</script>");Check out page:
ArrayList idList = (ArrayList)Session["InstallationId"]; for (int index = 0; index < idList.Count; index++) { Label1.Text = Label1.Text + idList[index].ToString(); }markfitzme
Star
14451 Points
2231 Posts
Re: Add to Cart
Jun 22, 2012 01:23 PM|LINK
On your description page you are creating a brand new array so you don't have anything but the last value as there is only one value that will be in your session.
try the following:
ArrayList idList = null;
if(Session["InstallationId"] != null)
idList = (ArrayList)Session["InstallationId"];
else
idList = new ArrayList();
This code checks to see if you already have an existing array in the session first, else it creates a new one.
You may want to try a different structure though for storing your data. A simpler one if you are doing product id, and product quantity is a NameValueCollection. It creates a name-value pair which would be useful here. That would avoid trying to figure out which is the product id and which is the quantity. In this case, the Product Id would be a key that you can get values for, and the quantity would be the value.
NameValueCollection idList = null;
if(Session["idList"] != null)
idList = (NameValueCollection)idList;
else
idList = new NameValueCollection();
// add items to the collection like so:
idList.Add(ProdID,ProQty);
To list all the items and values you can loop through the object to get both the key and the value
// example of a simple loop that lets you get a key, or a value based on an ordinal
for(int i = 0; i < idList.Count; i++)
{
// get a key value
idList.Keys[i];
// get a value
idList[i];
}
weihan1394
Member
9 Points
53 Posts
Re: Add to Cart
Jun 22, 2012 01:37 PM|LINK
Thank You!! You managed to solve my problem like my earlier stuck! :D