I am building a website .When a user login i am passing two sessions(Session["uid"],Session["uname"]) across pages and i am fetching these values in different pages and depending on these values further processing is made.In the page load event
of all the pages where i am fetching this session to a variable, i have wrote
but at times when i use the back arrow of the browser an error is coming "Object reference not set to an instance of an object".ie , the session value is empty but it is not redirecting to the index page.Why this is happening and how can i overcome
this.what can i do to keep theSession value across pages if if the user uses the browser back and forward arrow.This is very important because the session cannot be empty because it affects the further processing
.ToString() method does not handle the NULL that why it will throw the error so check the null only otherwise if you want to check the Empty string also then use to
Convert.ToString() method.
Hope it would be helpfull.
Sovit Mittal | My Blog Mark the post as Answer if it helped you.
You can alternatively use Global.asax Application_BeginRequest event to check for null session rather than writting it in each and every page. Check for null, if it is redirect to the page you want.
Member
11 Points
63 Posts
Managing sessions
Jan 26, 2011 02:06 AM|aadidev|LINK
Hi,
I am building a website .When a user login i am passing two sessions(Session["uid"],Session["uname"]) across pages and i am fetching these values in different pages and depending on these values further processing is made.In the page load event of all the pages where i am fetching this session to a variable, i have wrote
if ((Session["uid"].ToString() == "") || (Session["uid"].ToString() == null)||(Session["uname"].ToString() == "") || (Session["uname"].ToString() == null))
{
Response.Redirect("index.aspx");
}
but at times when i use the back arrow of the browser an error is coming "Object reference not set to an instance of an object".ie , the session value is empty but it is not redirecting to the index page.Why this is happening and how can i overcome this.what can i do to keep the Session value across pages if if the user uses the browser back and forward arrow.This is very important because the session cannot be empty because it affects the further processing
pls help and thanks in advance
Managing sessions
Member
270 Points
86 Posts
Re: Managing sessions
Jan 26, 2011 03:15 AM|NikxGupta|LINK
u should not use Session["keyname"].ToString() as when Session["keyname"] is null it will throw an exception
check directly for null if( Session["keyname"] == null)
Also once u created the session it cannot be automatically empty ur using right approach
check your session timeout settings and increase it if neccessary in web.config
<sessionState timeout="">
Star
10444 Points
2463 Posts
Re: Managing sessions
Jan 26, 2011 06:25 AM|sirdneo|LINK
You should check null in seperate if and blank string in seperate if like this:-
Reason is that if session["uid"] is null then .ToString() will give exception, so i first checked that these values are not null.
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
Member
220 Points
76 Posts
Re: Managing sessions
Jan 27, 2011 12:55 AM|sovitmittal|LINK
Hi,
.ToString() method does not handle the NULL that why it will throw the error so check the null only otherwise if you want to check the Empty string also then use to Convert.ToString() method.
Hope it would be helpfull.
Mark the post as Answer if it helped you.
Star
11146 Points
3957 Posts
Re: Managing sessions
Jan 27, 2011 12:58 AM|nilsan|LINK
You can alternatively use Global.asax Application_BeginRequest event to check for null session rather than writting it in each and every page. Check for null, if it is redirect to the page you want.
I hope it makes sense!! :)
Blog | Get your forum question answered | Microsoft Community Contributor 2011
Member
221 Points
112 Posts
Re: Managing sessions
Jan 27, 2011 01:36 AM|vaibhav_shah1988|LINK
Just remove .ToString() from session varibale of your code ...then it will work propetly.
Click Here For More Answers