Hey guys, still working on creating some sort of shopping basket and have decided to try using session cookies. I can populate the cookie with at least 1 entry, but either can't seem to convert string <> arraylist correctly or am working with cookies incorrectly.

I'm using asp.net/c# and am wanting to store a series of item ID numbers. I can easly store just the last one in the cookie, but either it's replacing it every time or I am using the wrong methods.

Cookie creation and checking...

string itemID = Request.QueryString["itemID"];
...
/* No cookie named items */
ArrayList itemList = new ArrayList();
itemList.Add(itemID);
string result0 = String.Join(",", itemList.ToArray());
Session["items"] = result0;
...
/* Cookie already present, retrieve it*/
ArrayList itemList = new ArrayList();
string itemListTemp = Session["items"].ToString();
string[] itemListTemp2 = itemListTemp.Split(',');
...
/* update list, create as string and reset cookie */
itemList.Add(itemID);
string result = String.Join(",", itemList.ToArray());
Session["items"] = result;

Displaying the items in a gridview (code behind for it)...

public ArrayList MyArrayOut = new ArrayList();
public string thisAry = "";
...
/* Get session cookie and create string list */
string itemList = Session["items"].ToString();
string[] itemListTemp = itemList.Split(',');
foreach (string item in itemListTemp)
{
    MyArrayOut.Add(item);
}
...
tempAry = String.Join("' OR itemID='", itemList.ToArray());
/*I then databind this to a SQL server select statement after some cleaning for safety*/

That's the basics of the main section... if you want to see anything else, let me know. As always, any ideas are very much appreciated!

EDIT: tempAry I created to echo in the html, it says "System.Collections.ArrayList"

i think things get a bit messy at line 6. there's no need to create a string out of the arraylist (especially doing 'itemList.ToArray()', as it's already an array!); just store itemList directly in the session var. i'll leave it at that for now. work with the session var and the arrayList[n] for a bit and see what you come up with. i think it could be easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.