0

I'm trying to make a program in ASP.net where a user selects products from page 1 (default.aspx), clicks a button to put into shopping cart, and it appears in the second page of product list.

List<string> skuList = new List<string>();
if (validation)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
            if (chkRow.Checked)
            {
                skuList.Add(row.Cells[2].Text);
            }
        }
    }
}
else { Response.Write("<script>alert('At least one product need to be selected!');</script>"); }
Session["sku"] = skuList;

The problem I am having is I can not update Session["sku"].

Goal I want to reach:

on Page 1, user chooses product Number 1,2,3, then the session variable get updated to 1,2,3 on Page 2, user continue to choose product Number 8, then the session variable get updated to 1,2,3,8

I tried to do something like

 Session["sku"] = Session["sku"] + skulistt;

Of course it didn't work.

2
  • What (compiler/runtime) error did you get? Commented May 28, 2014 at 17:53
  • List<string> skuList = (List<string>)Session["sku"]; Then skuList.Add("New item"); Commented May 28, 2014 at 17:53

2 Answers 2

2

It appears you have a misconception about session. The objects stored in session can be strongly-typed, but the typed returned is always object. It would look something like the following (taking session out of the equation):

object myvar = new List<string>();

myvar.Add("mystring"); // will not compile because myvar is defined as object.

To use myvar as a list is needs to be cast to the correct type:

var myvar1 = (List<string>)myvar; 
// or 
var myvar2 = myvar as List<string>; 

myvar1.Add("mystring");
myvar2.Add("mystring");

The difference between myvar and myvar1 is that myvar can hold a reference to any object type, so I could do:

object myvar = new List<string>();
myvar = new WebClient();    

but I can't do that with myvar because it's been defined as List<string> which WebClient does not derive from.

So to add a string to the session variable, it needs to be cast as the correct type:

var mylist = (List<string>)Session["sku"];
list.Add("mystring");

However it looks like you just want to replace the value:

session["sku"] = skulistt;
Sign up to request clarification or add additional context in comments.

Comments

1

You have to get List out of Session, add new element in List and then reassign to Session like this:

List<string> list= List<string>();

if(Session["sku"] != null)
     list = (List<string>)Session["sku"];

list.Add("item");

Session["sku"] = list;

UPDATED:

In your case do like this:

Session["sku"] = skulistt;

Change you code like:

List<string> list= List<string>();

if(Session["sku"] != null)
     list = (List<string>)Session["sku"];

if (validation)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
            if (chkRow.Checked)
            {
                skuList.Add(row.Cells[2].Text);
            }
        }
    }
}
else { Response.Write("<script>alert('At least one product need to be selected!');</script>"); }
Session["sku"] = skuList;

10 Comments

I got the following error on the line of List.add(skuList)-->Error 1 The best overloaded method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments
so skulisst is itself list then do this: Session["sku"] = skulistt;
this is wrong: Session["sku"] =Session["sku"] + skulistt;
Session object is different, you cannot concatenate like you do in string case
I don't think [code] Session["sku"] = skuList is right; On page 1, skuList is 1,2,3 then on the second page skuList is 8. The session variable would end up with 8, overwritten by skuList.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.