Storing class into session
-
I changed a way of storing user information from many session to one session object with storing a class into that session. I have a class like:
public class UserInfo { // Inside this class there are properties to store users data (all properties are public) string _username; int _id; bool _somePermission; ... and so on }
When user logs in and if user have all necessary permissions, I create a new instance of a UserInfo class
UserInfo user = new UserInfo();
And then read user permissions, from database, for every page on a site and store it in each property that correspond to
user.Username = dataReader["username"]; user.ID = dataReader["id"]; user.SomePermission = dataReader["some_permission"];
And finally, store that class into session like
Session.Add("UserInfo", user);
On every page, where I need information from session, I do
UserInfo user = (UserInfo)Session["UserInfo"];
And then use
user.Username, user.ID or user.SomePermission
But, theres something wrong about it. When I try to compile it, I get an error (marking Session)
An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session.get'
I am missing something, thats for sure, but I don't know what. Anyone would help me with this? Thanks in advanced.
-
I changed a way of storing user information from many session to one session object with storing a class into that session. I have a class like:
public class UserInfo { // Inside this class there are properties to store users data (all properties are public) string _username; int _id; bool _somePermission; ... and so on }
When user logs in and if user have all necessary permissions, I create a new instance of a UserInfo class
UserInfo user = new UserInfo();
And then read user permissions, from database, for every page on a site and store it in each property that correspond to
user.Username = dataReader["username"]; user.ID = dataReader["id"]; user.SomePermission = dataReader["some_permission"];
And finally, store that class into session like
Session.Add("UserInfo", user);
On every page, where I need information from session, I do
UserInfo user = (UserInfo)Session["UserInfo"];
And then use
user.Username, user.ID or user.SomePermission
But, theres something wrong about it. When I try to compile it, I get an error (marking Session)
An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session.get'
I am missing something, thats for sure, but I don't know what. Anyone would help me with this? Thanks in advanced.
Kasic Slobodan wrote:
On every page, where I need information from session, I do UserInfo user = (UserInfo)Session["UserInfo"];
Are you doing this outside the events, I meant on the declarative part of the Page Class? Session object get initialized after the Page Init event. May be you are try to assign the Session value to your class before the Page Load like,
UserInfo user = (UserInfo)Session["UserInfo"]; protected void Page_Load (object sender, EventArgs e){ // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS }
[Venkatesh Mookkan] My: Website | Yahoo Group | Blog Spot
-
I changed a way of storing user information from many session to one session object with storing a class into that session. I have a class like:
public class UserInfo { // Inside this class there are properties to store users data (all properties are public) string _username; int _id; bool _somePermission; ... and so on }
When user logs in and if user have all necessary permissions, I create a new instance of a UserInfo class
UserInfo user = new UserInfo();
And then read user permissions, from database, for every page on a site and store it in each property that correspond to
user.Username = dataReader["username"]; user.ID = dataReader["id"]; user.SomePermission = dataReader["some_permission"];
And finally, store that class into session like
Session.Add("UserInfo", user);
On every page, where I need information from session, I do
UserInfo user = (UserInfo)Session["UserInfo"];
And then use
user.Username, user.ID or user.SomePermission
But, theres something wrong about it. When I try to compile it, I get an error (marking Session)
An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session.get'
I am missing something, thats for sure, but I don't know what. Anyone would help me with this? Thanks in advanced.
Venkatesh is correct. Also when you do type casting you can use
as
keyword which is much faster than this type of casts.Kasic Slobodan wrote:
(UserInfo)Session["UserInfo"];
So this can be written like
UserInfo user = Session["UserInfo"] as UserInfo;
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Kasic Slobodan wrote:
On every page, where I need information from session, I do UserInfo user = (UserInfo)Session["UserInfo"];
Are you doing this outside the events, I meant on the declarative part of the Page Class? Session object get initialized after the Page Init event. May be you are try to assign the Session value to your class before the Page Load like,
UserInfo user = (UserInfo)Session["UserInfo"]; protected void Page_Load (object sender, EventArgs e){ // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS // YOUR STATEMENTS }
[Venkatesh Mookkan] My: Website | Yahoo Group | Blog Spot
Yes. I was doing outside the events. Thank you for your reply and your time. If I asked later, I would save some time figuring out what went wrong :). And thank you Navaneeth for a tip about casting. Best regards and thanks both of you.