StudentRik
StudentRik

Reputation: 1049

Hold more than one value in Session?

The project at Login checks the users details and the service area they belong too. It was just one area but now they can belong to more than one.

The code is how it was set up to work using Session.

var user = (from u in db.tbl_UserServiceAreaDetails
            where u.tbl_User.UserName.Equals(txt_LoginName.Text)
            && u.tbl_User.Password.Equals(txt_Password.Text)
            select u).FirstOrDefault();

if (user != null)
{
Session["Username"] = user.tbl_User.UserName;

Session["ServiceArea"] = user.tbl_ServiceArea.ServiceArea;

Session["ServiceAreaID"] = user.serviceAreaID;

The ServiceArea can now be many. Could I try a foreach over the serviceAreaID which is in a link table between User and Servicearea called serviceAreaDetails and the serviceAreaID is mapped to the serviceAreaId in tbl_ServiceArea and UserAreaID mapped to userId in tbl_User.

Would foreach (ListItem item in user.ServiceAreaID) be able to work?

If i get the userId when the user logs in and pass that into my Foreach could I some how set that to the Session. As the Index page displays the servicearea the user belongs too which can now be more than one.

Upvotes: 1

Views: 1372

Answers (2)

J.Starkl
J.Starkl

Reputation: 2343

You can store also complex objects in the session, so create a class for your users, where all the areas with ids are stored to the user and save this class in the session

class UserClass
{
    public string userName;
    public Dictionary<int, string> serviceArea = new Dictionary<int,string>();
}

UserClass user = new UserClass();

Session["User"] = user;

user = (UserClass)Session["User"];

Reply to comment:
Yes, feel free to use a Getter&Setter
Yes, thats how a Dictionary works, the first parameter is the key, the second the value, it's also flexible to be used with any objects

UserClass user = new UserClass();

user.userName = "Rik";

foreach(...serviceArea...) // Or whatever loop you like
{
    user.serviceArea.Add(serviceAreaID, serviceArea);
}

Upvotes: 1

jason
jason

Reputation: 3615

What I find is useful is creating a list of Objects and adding that to a session. You can then cast session variables to that object type and iterate over it. For example:

Public Class someClass
    Public Property userName as String
    Public Property FirstName as String     
End Class

You can then Create a list of this type and add it to a session:

dim myList as new list(of someClass)
dim newSomeClass as new someClass

newSomeClass.userName="username1"
newSomeClass.FirstName="Jason"
myList.add(newSomeClass)

newSomeClass.userName="username2"
newSomeClass.FirstName="Frank"
myList.add(newSomeClass)

Session.add("mySessionId",myList) 

You can then cast that session to myList and iterate over it somewhere else:

Dim myList as new list(of someClass)
if(Session("mySessionId") isNot nothing) then
    myList = Session("mySessionId")

    for each n as someClass in myList
      'iterate
    next
end if

You just need to be careful that you destroy these session when finished and that you aren't consuming to much resources between postbacks. If you have any questions, let me know.

Upvotes: 0

Related Questions