0

I have a array list formed to each genre of music, for example I will show country music:

public class CountryItemList
{
    ArrayList CountryItems = new ArrayList();

    protected void CountryBuy1_Click(object sender, ImageClickEventArgs e)
    {
        CountryItems.Add("Johnny Cash - I Walk The Line - $9");
    }
    protected void CountryBuy2_Click(object sender, ImageClickEventArgs e)
    {
        CountryItems.Add("Carrie Underwood - Blown Away - $9");
    }
    protected void CountryBuy3_Click(object sender, ImageClickEventArgs e)
    {
        CountryItems.Add("Keith Urban - The Story So Far - $9");
    }

    protected void CountryBuy5_Click(object sender, ImageClickEventArgs e)
    {
        CountryItems.Add("Taylor Swift - Red - $11");
    }
    protected void CountryBuy6_Click(object sender, ImageClickEventArgs e)
    {
        CountryItems.Add("Willie Nelson - Legend - $9");
    }

}

}

for each buy button that is clicked, the selected album is added to the array list, from then on i wish to take the array list into a session and carry it over to a list box on the "shopping cart page". I am having trouble taking the array list and making it into a session to carry over to the next page

user2246454
  • 3
  • 1
  • 5

4 Answers4

1

You're just going to need to use Session[] to carry it over.

I'd use a List rather than an ArrayList.

Try something like:

private List<string> _countryItems;
        public List<string> CountryItems {
            get {
                if (_countryItems == null) {
                    _countryItems = (List<string>)Session["CountryItems"];
                    if (_countryItems == null) {
                        _countryListItems = new List<string>();
                        Session["CountryItems"] = _countryItems;
                    }
                }
                return _countryItems;
            }
            set { _countryItems = value; }
        }

        protected void CountryBuy6_Click(object sender, ImageClickEventArgs e) {
            CountryItems.Add("Willie Nelson - Legend - $9");
        }

This way you reference CountryListItems directly from the Session. If it exists in the session, you will get the value from session. If it doesn't, you will get an empty List that you can add values to. This should then commit any updates to the session as you add the elements.

Also, look at using CommandEventArgs, which will allow you to create a single click event called from multiple places, then you will be able to send the individual comment event args in on e and get the values right from the markup.

Alex S.
  • 585
  • 3
  • 10
1

I've always been less than enthusiastic for using Session for things like this. I find it's not as reliable as using a database table, especially for something like a shopping cart.

If you ever needed to restart your application pool, any current users would lose their selections if you store it in Session. You also shouldn't assume that putting something into Session is readily available right away.

You should think about storing this stuff inside a database table. You could sssign each user a GUID when they add something to the cart for the first time and use that in addition to a product ID for each song. Throw that GUID into a cookie so if they come back at a later date, their cart will still be full. You could also use their SessionID if you don't want to use cookies or care if their cart is empty if they close the window.

If you want to continue to use Session, something like this would work

    protected List<string> SelectedSongs
    {
        get
        {
            List<string> li = new List<string>();

            if (Session["SelectedSongs"] != null)
            {
                try
                {
                    return (List<string>)Session["SelectedSongs"];
                }
                catch (Exception e)
                {
                    return li; // Something went wrong?  Return the empty list.
                }
            }
            else
            {
                return li;
            }
        }
        set
        {
            Session["SelectedSongs"] = value;
        }
    }

For adding them into the List, you could really clean up your code a bit by using just one Event Handler. Something like this

protected void AddToCart(object sender, CommandEventArgs e)
{
     List<string> li = SelectedSongs;
     li.Add(e.CommandArgument )
     SelectedSongs = li;    
}

<asp:Button id="btnAddToCart"  Text="Add Song" CommandArgument="Johnny Cash - I Walk The Line - $9" CommandName="AddToCart" runat="server"/>

<asp:Button id="btnAddToCart"  Text="Add Song" CommandArgument="Carrie Underwood - Blown Away - $9" CommandName="AddToCart" runat="server"/>

And so forth for the other songs.

Lucky Pierre
  • 570
  • 4
  • 18
  • What do you mean by: _You also shouldn't assume that putting something into Session is readily available right away._? – Shai Cohen Apr 04 '13 at 20:57
  • I get an error at li.add(e.CommandArgument) and also an error when I add command Argument and Command name – user2246454 Apr 06 '13 at 18:41
0

I don't think this will get a best shopping card award, but it'll do the job.

It basically stored the selected items into Session, so that you can retrieved it back in next page.

Note: ArrayList is deprecated, so please use Generic List.

enter image description here

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CountryItemList.aspx.cs"
    Inherits="WebApplication2010.CountryItemList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="Button1" OnCommand="Button_Command" 
        CommandName="Johnny Cash - I Walk The Line - $9"
            Text="Johnny Cash - I Walk The Line - $9" /><br/>
        <asp:Button runat="server" ID="Button2" OnCommand="Button_Command" 
        CommandName="Carrie Underwood - Blown Away - $9"
            Text="Carrie Underwood - Blown Away - $9" /><br/>
        <asp:Button runat="server" ID="Button3" OnCommand="Button_Command" 
        CommandName="Keith Urban - The Story So Far - $9"
            Text="Keith Urban - The Story So Far - $9" /><br/>
        <asp:Button runat="server" ID="Button4" OnCommand="Button_Command" 
        CommandName="Taylor Swift - Red - $11"
            Text="Taylor Swift - Red - $11" /><br/>
        <asp:Button runat="server" ID="Button5" OnCommand="Button_Command" 
        CommandName="Willie Nelson - Legend - $9"
            Text="Willie Nelson - Legend - $9" />
    </div>
    </form>
</body>
</html>

public partial class CountryItemList : System.Web.UI.Page
{
    public List<string> CountryItems
    {
        get { return Session["CountryItems"] as List<string> ?? new List<string>(); }
        set { Session["CountryItems"] = value; }
    }

    protected void Button_Command(object sender, CommandEventArgs e)
    {
        var items = CountryItems;
        items.Add(e.CommandName);
        CountryItems = items;
    }
}
Community
  • 1
  • 1
Win
  • 61,100
  • 13
  • 102
  • 181
0

Try this

Page 1:

protected void Page_Load(object sender, EventArgs e)
{
    //Creating Session
    ArrayList idList = new ArrayList();
    idList.Add("1");
    idList.Add("2");
    idList.Add("3");
    Session["idArrayList"] = idList;
}

Page 2:

protected void Page_Load(object sender, EventArgs e)
{
    //Retrieving
    ArrayList idList = (ArrayList)Session["idArrayList"];
    string id1 = idList[0].ToString() ;
    string id2 = idList[1].ToString();
    string id3 = idList[2].ToString(); 
}

Source: http://forums.asp.net/t/1425975.aspx?C+How+to+place+an+arraylist+inside+a+Session+Variable+and+Iterate+through+it

Syed Ali Taqi
  • 4,898
  • 3
  • 34
  • 44