1

When i store an item, i can store it and read it. But when i try to store another item, the first one disappear.

I'm using a masterpage for my default site dont know if it affects it.

my code is very simple and looks like this.

 List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
    List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
    if (kurv == null)
    {
        kurv = new List<kurvliste>();
        Session["kurv"] = kurv; // Store the new list in the session object!
    }

}

protected void Unnamed_ServerClick(object sender, EventArgs e)
{
    kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
    Session["kurv"] = kurv;
}

In my masterpage it looks like this

List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{


    List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];

    if (kurv == null)
    {
        kurv = new List<kurvliste>();
        Session["kurv"] = kurv; // Store the new list in the session object!
    }

    Repeater1.DataSource = kurv;
    Repeater1.DataBind();
}
Driton Cazimi
  • 15
  • 1
  • 6

1 Answers1

0

Because you have a bug - you declare the same name outside and inside the Page_load, so on the click the outside is used - so you actually use two different lists.

I write your code adding 1 & 2 to understand the bug.

 List<kurvliste> kurv_1 = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
    List<kurvliste> kurv_2 = (List<kurvliste>)Session["kurv"];
    if (kurv_2 == null)
    {
        kurv_2 = new List<kurvliste>();
        Session["kurv"] = kurv_2; // Store the new list in the session object!
    }    
}

protected void Unnamed_ServerClick(object sender, EventArgs e)
{
    kurv_1.Add(new kurvliste(1,1,1, "Produktnavn"));
    Session["kurv"] = kurv_1;
}

How to solve it

But if i remove the outside, i get a null reference just by leaving it to be List kurv.

No you need to remove the inside of Page_Load declaration that hides the outside one

protected void Page_Load(object sender, EventArgs e)
{
    // on this line, just remove the declaration
    // List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
    // do it like
    kurv = Session["kurv"] as List<kurvliste>;
    if (kurv == null)
    {
        kurv = new List<kurvliste>();
        Session["kurv"] = kurv; // Store the new list in the session object!
    }
}

Alternative

I suggest to move the list on the session to a property like that

// using this const you avoid bugs in mispelling the correct key.
const string ckurvlisteNameConst = "kurvliste_cnst";

public List<kurvliste> kurv
{
    get
    {
        // If not on the Session then add it
        if (Session[ckurvlisteNameConst] == null)                
            Session[ckurvlisteNameConst] = new List<kurvliste>();

        // this code is not exist on release, but I check to be sure that I did not 
        //  overwrite this Session with a different object.
        Debug.Assert(Session[ckurvlisteNameConst] is List<kurvliste>);

        return (List<kurvliste>)Session[ckurvlisteNameConst];
    }
}



protected void Page_Load(object sender, EventArgs e)
{

}

protected void Unnamed_ServerClick(object sender, EventArgs e)
{
    kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
}

a similar answer : How to store list of object into ViewState

Aristos
  • 66,005
  • 16
  • 114
  • 150