session error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthi84
    Contributor
    • Dec 2006
    • 270

    session error

    hi,
    i have used session in my .net project. its version is 1.1, when ever i load that page i get the following error,
    Code:
     
    [b]Server Error in '/Alfi/invoice generation/WebApplication1' Application. [/b]
    
    [b][i]Object reference not set to an instance of an object.[/i] [/b]
    
    [font=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif][b]Description: [/b]An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    [b]Exception Details: [/b]System.NullReferenceException: Object reference not set to an instance of an object.
    
    [b]Source Error:[/b] 
    
    Line 41: //			tryLine 42: //			{[color=red]Line 43: 				if(Session["UnAuthorised"].ToString()!="")[/color]Line 44: 				{Line 45: 					lblPlzLgn.Visible=true;
    [b][font=Verdana]Source File: [/font][/b]\\avsystems11\alfi\invoice generation\webapplication1\scripts\login.aspx.cs[b][font=Verdana]    Line: [/font][/b]43 
    
    [b][font=Verdana]Stack Trace:[/font][/b] 
    
    [NullReferenceException: Object reference not set to an instance of an object.]   AutomaticInvoiceGeneration.Login.Page_Load(Object sender, EventArgs e) in \\avsystems11\alfi\invoice generation\webapplication1\scripts\login.aspx.cs:43   System.Web.UI.Control.OnLoad(EventArgs e) +67   System.Web.UI.Control.LoadRecursive() +35   System.Web.UI.Page.ProcessRequestMain() +731
    
    [b][font=Verdana]Version Information:[/font][/b] Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 [/font]
    my coding which caused this error is
    Code:
    [size=2][color=#0000ff]private[/color][/size][size=2] [/size][size=2][color=#0000ff]void[/color][/size][size=2] Page_Load([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, System.EventArgs e)
    
    {
    
    lblPlzLgn.Visible=[/size][size=2][color=#0000ff]false[/color][/size][size=2];
    
    lblSessionExp.Visible=[/size][size=2][color=#0000ff]false[/color][/size][size=2];
    
    Session["text"]="";
    
    Session["Alert"]="";
    
    [/size][size=2][/size][size=2][color=#0000ff]if[/color][/size][size=2](Session["UnAuthorised"].ToString()!="")
    
    {
    
    lblPlzLgn.Visible=[/size][size=2][color=#0000ff]true[/color][/size][size=2];
    
    Session["UnAuthorised"]="";
    
    }
    
    [/size][size=2][color=#0000ff]else
    
    [/color][/size][size=2]{
    some conditions();
    }
    [/size]
    what could be the cause of this error. i know that this error can be caught using exceptions, but my problem is i need to use nested try commands. as a result of this problem it is not entering into the nested region and throws exception. how to solve this problem.

    thanks,
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by karthi84
    hi,
    i have used session in my .net project. its version is 1.1, when ever i load that page i get the following error,
    ...
    what could be the cause of this error. i know that this error can be caught using exceptions, but my problem is i need to use nested try commands. as a result of this problem it is not entering into the nested region and throws exception. how to solve this problem.

    thanks,

    Hi there Karthi,

    You're trying to compare your session variable "UnAuthoris ed" to "".
    I'm assuming that the first time the user comes to the page this variable hasn't been set to anything. Then when you try to compare it to "" you're getting your Null Reference error because it hasn't yet been defined.

    Before trying to do this comparison, check to see if this variable has been defined. In other words check to see that this variable is not Null or Nothing before comparing it to "" . If it is nothing you should deal with the situation accordingly (I'm assuming the person isn't authorized).

    Hope this helps

    -Frinny

    Comment

    • karthi84
      Contributor
      • Dec 2006
      • 270

      #3
      Originally posted by Frinavale
      Hi there Karthi,

      You're trying to compare your session variable "UnAuthoris ed" to "".
      I'm assuming that the first time the user comes to the page this variable hasn't been set to anything. Then when you try to compare it to "" you're getting your Null Reference error because it hasn't yet been defined.

      Before trying to do this comparison, check to see if this variable has been defined. In other words check to see that this variable is not Null or Nothing before comparing it to "" . If it is nothing you should deal with the situation accordingly (I'm assuming the person isn't authorized).

      Hope this helps

      -Frinny
      thanks finnay for the idea. let me try and reply u

      Comment

      • karthi84
        Contributor
        • Dec 2006
        • 270

        #4
        Originally posted by Frinavale
        Hi there Karthi,

        You're trying to compare your session variable "UnAuthoris ed" to "".
        I'm assuming that the first time the user comes to the page this variable hasn't been set to anything. Then when you try to compare it to "" you're getting your Null Reference error because it hasn't yet been defined.

        Before trying to do this comparison, check to see if this variable has been defined. In other words check to see that this variable is not Null or Nothing before comparing it to "" . If it is nothing you should deal with the situation accordingly (I'm assuming the person isn't authorized).

        Hope this helps

        -Frinny
        hi frinny,
        i tried it but it was not helping me. can u get me with an example for this. my requirement is
        1. if an unauthorized person goes directly to 2nd page without logging in he is to be identified and a message is to be given. so i used one session from the login page.
        2. one more session to identify the session time out and get a message in the login page

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by karthi84
          hi frinny,
          i tried it but it was not helping me. can u get me with an example for this. my requirement is
          1. if an unauthorized person goes directly to 2nd page without logging in he is to be identified and a message is to be given. so i used one session from the login page.
          2. one more session to identify the session time out and get a message in the login page
          Try something like this...
          Code:
          private void Page_Load(object sender, System.EventArgs e)
          {
          
          lblPlzLgn.Visible=false;
          
          lblSessionExp.Visible=false;
          Session("text")="";
          Session("Alert")="";
          
          if(Session("UnAuthorised")==NULL)
          {
              lblPlzLgn.Visible=true;
              Session["UnAuthorised"]="";
          }else
          {
              some conditions();
          }
          What language are you using? C#?

          Looking at your code again I'm realizing that not only are you trying to compare NULL to "" but also you were trying to do a toString() on a variable that was null.

          Try this and let me know if it helps out

          -Frinny

          Comment

          • karthi84
            Contributor
            • Dec 2006
            • 270

            #6
            Originally posted by Frinavale
            Try something like this...
            Code:
            private void Page_Load(object sender, System.EventArgs e)
            {
             
            lblPlzLgn.Visible=false;
             
            lblSessionExp.Visible=false;
            Session("text")="";
            Session("Alert")="";
             
            if(Session("UnAuthorised")==NULL)
            {
            lblPlzLgn.Visible=true;
            Session["UnAuthorised"]="";
            }else
            {
            some conditions();
            }
            What language are you using? C#?

            Looking at your code again I'm realizing that not only are you trying to compare NULL to "" but also you were trying to do a toString() on a variable that was null.

            Try this and let me know if it helps out

            -Frinny
            hi frinny,
            i tried that code but results me in an error. the error message is
            'System.Web.UI. Page.Session' denotes a 'property' where a 'method' was expected.

            how to solve this problem

            Comment

            • karthi84
              Contributor
              • Dec 2006
              • 270

              #7
              Originally posted by Frinavale
              Try something like this...
              Code:
              private void Page_Load(object sender, System.EventArgs e)
              {
               
              lblPlzLgn.Visible=false;
               
              lblSessionExp.Visible=false;
              Session("text")="";
              Session("Alert")="";
               
              if(Session("UnAuthorised")==NULL)
              {
              lblPlzLgn.Visible=true;
              Session["UnAuthorised"]="";
              }else
              {
              some conditions();
              }
              What language are you using? C#?

              Looking at your code again I'm realizing that not only are you trying to compare NULL to "" but also you were trying to do a toString() on a variable that was null.

              Try this and let me know if it helps out

              -Frinny
              hi frinny,
              the problem is solved by replacing '(' with '['. now its working fine. thanks for the help.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by karthi84
                hi frinny,
                the problem is solved by replacing '(' with '['. now its working fine. thanks for the help.
                Glad to have helped

                Comment

                • karthi84
                  Contributor
                  • Dec 2006
                  • 270

                  #9
                  Originally posted by Frinavale
                  Glad to have helped
                  hi frinny,

                  now i get a lot of problem in my page. when i go to other pages whithout logging in using the url, it should be redirected stating please login. but for me that is not working.
                  similarly when i logoff after loging in this message is displayed. the below shown are the two different page codings. please help me in this,

                  Login Page:

                  Code:
                  [size=2][color=#0000ff]private[/color][/size][size=2] [/size][size=2][color=#0000ff]void[/color][/size][size=2] Page_Load([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, System.EventArgs e)[/size]
                  [size=2]{
                     lblPlzLgn.Visible=[/size][size=2][color=#0000ff]false[/color][/size][size=2];[/size]
                  [size=2][color=#0000ff]	 if[/color][/size][size=2](Session["UnAuthorised"]!=[/size][size=2][color=#0000ff]null[/color][/size][size=2])[/size]
                  [size=2]	 {[/size]
                  [size=2]		lblPlzLgn.Visible=[/size][size=2][color=#0000ff]true[/color][/size][size=2];[/size]
                  [size=2]	   Session["UnAuthorised"]="";[/size]
                  [size=2]	 }[/size]
                  [size=2]}[/size]
                  [size=2][size=2][color=#0000ff]private[/color][/size][size=2] [/size][size=2][color=#0000ff]void[/color][/size][size=2] Button1_Click([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, System.EventArgs e)[/size]
                  [size=2]{[/size]
                  [size=2][size=2]	 Session["text"]=TextBox1.Text;[/size][/size]
                  [size=2][size=2]	 TextBox1.Text="";[/size][/size]
                  [size=2][size=2]	 TextBox2.Text="";[/size][/size]
                  [size=2][size=2]	 Response.Redirect ("SecondPage.aspx");[/size][/size]
                  [size=2][size=2]}[/size][/size]
                  [/size]
                  Second Page:

                  Code:
                  [size=2][color=#0000ff]if[/color][/size][size=2](Session["text"]!=null)[/size]
                  [size=2]{[/size]
                  [size=2]alert(); [/size]
                  [size=2]}[/size]
                  [size=2][color=#0000ff]else
                  [/color][/size][size=2]{
                  [size=2]Session["UnAuthorised"]="UnAuthorised";[/size]
                  Response.Redirect("Login.aspx");
                  }
                  [/size]
                  can u help me in solving this problem

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by karthi84
                    hi frinny,

                    now i get a lot of problem in my page. when i go to other pages whithout logging in using the url, it should be redirected stating please login. but for me that is not working.
                    similarly when i logoff after loging in this message is displayed. the below shown are the two different page codings. please help me in this,

                    Login Page:
                    ...

                    can u help me in solving this problem

                    Hi Karthi,

                    I'm not quite sure what you are trying to do with the code you posted.

                    First of all, aren't you supposed to check the user's credentials before passing them on to the second page?

                    Secondly, is your lblPlzLgn an error label or the actual text for prompting the user?

                    You'll have to better explain what exactly is going on.
                    What happens to your site when the error occurs?
                    Does it redirect when its not supposed to?
                    Does it not redirect when it is supposed to?


                    Looking at your code I don't see any sort of user validation. I'd validate the user on the first page before allowing them to go on the second page.

                    Eg. I'd set the Session["UnAuthoris ed"] variable to true if the user's credentials weren't valid and I'd set the variable to "false" if the user's credentials were valid. Then on every page I'd check to see if the Session["UnAuthrois ed"] variable is null or if it's true...and if so I wouldn't do any processing on that page, but instead redirect the user back to the starting page.


                    Code:
                    private void Page_Load(object sender, System.EventArgs e)
                    {
                       	lblPlzLgn.Visible=false;
                    
                    	 if(Session["UnAuthorised"]!=null)
                    	 {
                    		lblPlzLgn.Visible=true;
                    	   	Session["UnAuthorised"]="";
                    	 }
                    }
                    private void Button1_Click(object sender, System.EventArgs e)
                    {
                    	 Session["text"]=TextBox1.Text;
                    	 TextBox1.Text="";
                    	 TextBox2.Text="";
                    	/* You should check to see if the user's credentials are valid 
                    here before Redirecting them to the SecondPage.aspx.  
                    Here I would do something like:
                    
                    if(userCredentialsAreValid()==true)
                    { //userCredentialsAreValid() is the function 
                     //that returns true if the user's credentials are valid and false otherwise.
                             Session["UnAuthorized"]=false;
                    	 Response.Redirect ("SecondPage.aspx");
                    }
                    else
                    {
                             Session["UnAuthorized"]=true;
                    }
                    */
                    }
                    Code:
                    if(Session["UnAuthroised"]==null)
                    {
                                Response.Redirect("Login.aspx");
                    }
                    else if (Session["UnAuthorized"]==true)
                    {
                               Response.Redirect("Login.aspx");
                    }
                    else
                    {
                              if(Session["text"]!=null)
                              {
                    	          alert(); 
                              }
                              else
                             {
                    	          Session["UnAuthorised"]=true;
                      	          Response.Redirect("Login.aspx");
                             }
                    }
                    I don't know if any of the above suggestions are going to help because I'm not exactly sure what the problem is.

                    -Frinny

                    Comment

                    Working...