locked
Managing sessions RRS feed

  • Question

  • User233403405 posted

    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

    Wednesday, January 26, 2011 2:06 AM

Answers

  • User1009619486 posted

    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="">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 3:15 AM
  • User-481631678 posted

    You should check null in seperate if and blank string in seperate if like this:-


     bool moveToIndexPage= false;
                if (Session["UID"] == null || Session["uname"] == null)
                {
                    moveToIndexPage = true;
                }
                else if (Session["UID"].ToString() == "" || Session["uname"].ToString() == null)
                {
                    moveToIndexPage = true;
                }
                if (moveToIndexPage)
                {
                    Response.Redirect("index.aspx");
                }

    Reason is that if session["uid"] is null then .ToString() will give exception, so i first checked that these values are not null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 6:25 AM

All replies

  • User1009619486 posted

    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="">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 3:15 AM
  • User-481631678 posted

    You should check null in seperate if and blank string in seperate if like this:-


     bool moveToIndexPage= false;
                if (Session["UID"] == null || Session["uname"] == null)
                {
                    moveToIndexPage = true;
                }
                else if (Session["UID"].ToString() == "" || Session["uname"].ToString() == null)
                {
                    moveToIndexPage = true;
                }
                if (moveToIndexPage)
                {
                    Response.Redirect("index.aspx");
                }

    Reason is that if session["uid"] is null then .ToString() will give exception, so i first checked that these values are not null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 6:25 AM
  • User510401362 posted

    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.




    Thursday, January 27, 2011 12:55 AM
  • User-2139489267 posted

    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!! :)

    Thursday, January 27, 2011 12:58 AM
  • User590655761 posted

    Just remove .ToString() from session varibale of your code ...then it will work propetly.


    Thursday, January 27, 2011 1:36 AM