user1264228
user1264228

Reputation: 41

Need to copy items from one listbox to another listbox using a session variable

Page 1: (sending listbox items into a list and saving the list as a session variable)

List<string> list = new List<string>();
foreach(ListItem item in ListBox1.Items)
{
    list.Add(item.ToString());
}
Session["temp"] = list;

Page 2: (copying the session variable into a list and assigning the list to a listbox in this page)

List<string> list = new List<string>();
list = (List<string>)Session["JobRole"];
foreach (ListItem item in list)
{
    ListBox2.Items.Add(item.ToString());
}

On doing so, this is the error i m getting : Cannot convert type 'string' to 'System.Web.UI.WebControls.ListItem'

can anyone pls help me with this ??

Thanks

Upvotes: 0

Views: 1791

Answers (2)

Maess
Maess

Reputation: 4156

That's because you have defined list as List. If you simply want to add the string value into the listbox you can do:

List<string> list = new List<string>();
        list = (List<string>)Session["JobRole"];
        foreach (string item in list)
        {
            ListBox2.Items.Add(item);
        }

Also you should check that Session["JobRole"] is not null.

Upvotes: 0

trutheality
trutheality

Reputation: 23465

Don't you mean

    foreach (string item in list)
    {
        ListBox2.Items.Add(item);
    }

... list is a list of strings after all.

Upvotes: 2

Related Questions