Answered by:
Storing Session Values

Question
-
User801447716 posted
Hi,
What is the most effieceint method to store session values, I have an application that I store (name, company, ID, email, securityID etc) at first I stored these as single session values but wondered if I should store these as an array in a single session e.g. userDetails = (name, company, ID, email, securityID) then reference these on the pages.
Any experience/opions is appreicated.
Thanks
Tuesday, September 4, 2012 4:16 PM
Answers
-
User-1360095595 posted
You might create an object with properties and store that in session instead. Having said that, I don't know if it's more "efficient". What do you mean by that?- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 4, 2012 4:38 PM
All replies
-
User-1360095595 posted
You might create an object with properties and store that in session instead. Having said that, I don't know if it's more "efficient". What do you mean by that?- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 4, 2012 4:38 PM -
User2117486576 posted
When the properties are related, I typically create an object and store that in session because at the end of the day it requires less typing. That's my definition of efficient.
Tuesday, September 4, 2012 4:56 PM -
User-1362334014 posted
yes, I too agree on creation of a object and its storage in Session.
See, casting will be done in each case, whether you store and retrieve string or a class object.
There might be a very slight performance drop using class object in session (though not sure), but practically it will be much easier and maintainable using the class object. Single string will be looking some undisciplined and unmaintainable code
Wednesday, September 5, 2012 10:41 AM -
User801447716 posted
Thanks for your replies, I like the thought of less typing, would it be possible for someone please provide a simple example of a typical object that I can adopt.
Thanks in advance.
Wednesday, September 5, 2012 11:50 AM -
User-1656361569 posted
public class class2
{ public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{ //Code to Add Data in Session object
class2 objcls = new class2();
objcls.Address = "India";
objcls.Name = "Test";
objcls.Age = 15;
Session.Add("StoreData", objcls);
//Retrive Data from Session
class2 objcls1 = new class2();
objcls1 = (class2)Session["StoreData"];
}
Wednesday, September 12, 2012 2:43 AM