0

I have a problem and I have consulted some other people with this but they too didn't know what the problem was. I have a very simple asp.net page right now with some table controls on there. The page consists of a form in which objects can be added (which is not implemented jet). And There are buttons beneath the tables which clears the objects from these tables. All objects are shared in a single list and have an enum which seperates them from tables.

//fields List animals;

protected void Page_Load(object sender, EventArgs e)
{            
    if (!IsPostBack)
    {
        animals = new List<Animal>();

        animals.Add(new Animal("kees", false, species.dog) { Age = 13, RegNr = "0123456789" });
        animals.Add(new Animal("Henk", true, species.dog));
        animals.Add(new Animal("Cat", false, species.cat));

        ViewState["animalsSession"] = animals;
        fillTables();   
    }
}

private void fillTables()
{
    foreach (Animal a in (List<Animal>)ViewState["animalsSession"])
    {
        //make a row
        TableRow newRow = new TableRow();

        //making cells
        TableCell cellName = new TableCell();
        cellName.Text = a.Name;
        newRow.Cells.Add(cellName);

        TableCell cellAge = new TableCell();
        cellAge.Text = (a.Age == -1 ? String.Empty : a.Age.ToString());                 
        newRow.Cells.Add(cellAge);

        TableCell cellRegNr = new TableCell();
        cellRegNr.Text = (a.RegNr == null ? String.Empty : a.RegNr);
        newRow.Cells.Add(cellRegNr);

        TableCell cellReserved = new TableCell();
        cellReserved.Text = (a.IsReserved ? "x" : String.Empty);
        newRow.Cells.Add(cellReserved);


        if (a.Species == species.dog)
        {
            dogTable.Rows.Add(newRow);
        }
        else if (a.Species == species.cat)
        {
            catTable.Rows.Add(newRow);
        }
    }
}

protected void dogDeleteButton_Click(object sender, EventArgs e)
{
    List<Animal> cats = new List<Animal>();

    foreach (Animal a in (List<Animal>)ViewState["animalsSession"])
    {
        if (a.Species == species.cat)
        {
            cats.Add(a);
        }
    }
    ViewState["animalsSession"] = cats;

    fillTables();
}

Now this all works as expected, but notice that the aniimalSession is not a session but a viewstate. When I change all viewstates on this page with sessions it's not working at all. I don't get why this is since how I learned it they do kinda the same. Only the session saves the information on the server while the viewstate saves it on the client, but that shouldn't prevent the session from not working in this case right?

Does somebody know what I did wrong here, or are there other differences in sessions and viewstate.

BTW I know I could better use something like a gridview, but I'm learning about sessions right now so I try to get it working using these tables.

Politiepet
  • 93
  • 1
  • 2
  • 11
  • not working at all as no output, blank page, error page what? and as far as I see, your code doesn't even need a viewstate, as animal is present at class level. – Codeek Dec 20 '14 at 10:55
  • When using a viewstate the delete button gets the animal list from the viewstate and makes a new list with only dogs in it (by looking at the enum). This works properly as it fills the tables with only cats. When I use a session it throws a null reference exception for the viewstate in the foreach loop for the delete button. Also in this case using sessions is useless because the objects themselves are made in the page load method, but this is more of a test. – Politiepet Dec 20 '14 at 11:04
  • So you mean you are using session to store the List and using viewstate in foreach to retrieve that list?. show the code where you are filling your session and the one where you are using the value stored in session along with the place where you are getting your error, – Codeek Dec 20 '14 at 11:11
  • No, what I mean that all viewstates in the code above are working as in the way it is. I wan't to use sessions here though, so when I replace all viewstates above with session (so using sessions to get and set the list) it's not working, whilst the rest of the program remains the same. So for saving 'Session["animalsSession"] = animals;' and for getting: 'foreach (Animal a in (List)Session["animalsSession"]){...}' – Politiepet Dec 20 '14 at 11:13
  • It's difficult for someone with as little experience as mine to determine why is it causing you such problem, if replacing viewstate with session is only what you are doing. Probable cause would be session timeout, but even in it's default state, it shouldn't time-out in a span as quick as a page's construction. Will need more precise information and code – Codeek Dec 20 '14 at 11:23

1 Answers1

1

Can you please make sure whether session is enabled or not. Please refer to the link below to check whether session state is enabled or not at the ASP Alliance.

Lokesh
  • 129
  • 3
  • Not working unfortunately. These fields where not entered (so default value true). I manually added them and set them to true but not working still. – Politiepet Dec 20 '14 at 11:38
  • 1
    Please follow this link just to make sure that session is not disabled. http://stackoverflow.com/questions/14334147/session-state-can-only-be-used-when-enablesessionstate-is-set-to-true-either-in – Lokesh Dec 20 '14 at 11:50
  • 1
    The problem seems to be that the asp.net state service was not running on my computer. I turned it on now and it's working. Thanks for giving the answer. – Politiepet Dec 20 '14 at 11:55