1

I have website where user need to do some simple step 1 - step 4, so i decided to save all the data into Session Object, on step 2 - step 4 user can go back to previous step to check the form (data will be fill with session)

my question is. can we store new value/object into session while maintain the previous value??

Session["regis"] = student.firstname

// i want object_a value still remain in Session["regis"]
Session["regis"] = student.lastname

or should i make more than 1 Session??

Session["Step1"] = student.firstname

Session["Step2"] = student.lastname
1
  • As you're coding in MVC it might be better to pass the object to and from the View or better still implement the steps1-4 bit as a bit in some client side script in JQuery or some such. You could then POST the completed object in one go which may even help with issue alike storing incomplete records in a back end. Commented Jan 26, 2015 at 8:12

3 Answers 3

1

Yes. You can store multiple values in the session under different keys as in your second example (note that this is the same session, just a different key). However, this won't do what you want it to:

Session["regis"] = student.firstname

// This will overwrite the first value
Session["regis"] = student.lastname

You might also consider storing the entire view model under one key.

Do be careful when storing things in the session, though. It is very easy to elicit undesired behaviour. For example - the application pool might expire, losing session data; if you are running in a load balanced environment your users may lose their sessions when they hit a different application server; if a user is running multiple tabs, session variables assigned in requests from one tab may interfere with the behaviour of the application in another, etc.

Consider using cookies or hidden fields instead. If you must use session variables, it is a good idea to at least generate dynamic keys and pass those from page to page in the query string to ensure that, for example, using multiple tabs doesn't cause key conflicts.

Sign up to request clarification or add additional context in comments.

Comments

0

You will lose the previous value when new values is assigned to session object with the same key in your case i.e. regis So you need to use the second option.

You can use stage to create/access the session object like

To set value in session

Session["Step" + stepIndex] = student.firstname;

To get value from session

string str = Session["Step" + stepIndex].ToString();

2 Comments

so in my case, the only way is to make 1 or more session keys?
You need more the one keys or you can store some collection / array in session object but that will require casting. If you only have four stages and only four keys are required then you can avoid collections and simply use four keys.
0

Another option is storing object in session. (Assuming student is instance of Student class)

private Student StudentSession
{
    get{ return (Student)Session["regis"]; }
    set{ Session["regis"] = value; }
}

Usage,

StudentSession = new Student();

StudentSession.Name = "Scott";
StudentSession.Surname = "Foo";

You could assign aditional variables later on another page.

StudentSession.Age = 12;

Comments

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.