C# Web-App: problem with session variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arial
    New Member
    • Sep 2007
    • 109

    C# Web-App: problem with session variable

    Hi all,

    I really need some help. I have a asp.net web project using c#.

    I have put one of my datafield in session in one webform and I am calling that same datafield of the session to another web form within a same project.

    I worked before and now all of a sudden I am getting null value in my second form when i try to extract from session variable.

    As far as I know I haven't change anything in this project.

    and my syntaxt is:
    Code:
    Session["qid"] = qid; ....................webform1
    
    string getid = Session["qid"].ToString();..............webform2
    what should I do to resolve this?

    Thank You,
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    is this code

    string getid = Session["qid"].ToString();

    in the page load of webform2?

    you shud be able to debug and find out the value of that session variable before getting redirected from webform1 to webform2. Have you tried it?

    Comment

    • arial
      New Member
      • Sep 2007
      • 109

      #3
      I am calling string getid = Session ["qid"].ToString(); to one of the method that I call on page load.

      like,
      Code:
      page load()
      {
      Getpdfs();
      }
      public void pdfs()
      {
      string getid = Session ["qid"].ToString(); 
      }
      yes.. On my webform1 I can see the value getting into session during the debugging but when it comes to webform 2

      string getid = session["qid"].Tostring();

      getid come with null value.

      Comment

      • arial
        New Member
        • Sep 2007
        • 109

        #4
        Ok. here I found something strange.

        I can get session variable to my webform3 because I have several webforms withing same projetc.

        But, I can not get session variable on webform 2.

        How can this possible? And I can not find anything wrong with my webform2.

        Please help me if any one of you have any idea on this.

        Thank You,

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Your code:
          Code:
          page load()
          {
          Getpdfs();
          }
          public void pdfs()
          {
          string getid = Session ["qid"].ToString(); 
          }
          Is it because of this reason

          Modification, notice the change of the method !!!!
          Code:
          page load()
          {
          Getpdfs();
          }
          public void Getpdfs() //notice the change
          {
          string getid = Session ["qid"].ToString(); 
          }

          Comment

          • arial
            New Member
            • Sep 2007
            • 109

            #6
            I just a typo error on my previous post about the function.

            I do have the function's correct name but it not working.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Do you have any Session.Abandon s() in your pages?
              Or anything that would tell the client browser to drop the cookie?

              Comment

              • arial
                New Member
                • Sep 2007
                • 109

                #8
                I am not sure what is Session.Abandon s() .

                but, what I just recently discover is,

                I can get session variable to my second form if in my hyper link I have address like: http://localhost/form2.

                but if I replace my localhost to IP address I can't get session variable.
                like, http://10.10.10.10/form2 does not give me session variable.

                Why it would behave right with localhost and won't work with IP address?

                Thank You,

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  I am thinking it is client side.
                  There is a special header that gets passed in the request/responses that contains the session id for the user.
                  If the browser doesn't send the special header, the server assumes it's a new "user" and gives it a new session id, giving the appearance that the session values have been lost.
                  The browser might not understand it needs to send the header if you switch up the URL address.

                  Comment

                  • arial
                    New Member
                    • Sep 2007
                    • 109

                    #10
                    I think URL address is same like,

                    home page is like,

                    http://10.10.10.10/form1

                    there is a link on form1 which takes to form2

                    with a url

                    http://10.10.10.10/form2.

                    if I replace my ip address with localhost it works but if I have IP address it won't work.

                    Thank you

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      Does it work with http://127.0.0.1/ ?

                      Comment

                      • arial
                        New Member
                        • Sep 2007
                        • 109

                        #12
                        No. and I get this error message when I use my IP Address on hyperlink.

                        Code:
                        Object reference not set to an instance of an object. 
                        Description: 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. 
                        
                        Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
                        
                        Source Error: 
                        
                        
                        Line 42: 		{
                        Line 43: 			// Put user code to initialize the page here
                        Line 44: 			getqid = Session["qid"].ToString();			
                        Line 45: 			buildQuickcheck();
                        Line 46: 
                         
                        
                        Source File: d:\devsite\tap\checkoutform\qcheck.aspx.cs    Line: 44 
                        
                        Stack Trace: 
                        
                        
                        [NullReferenceException: Object reference not set to an instance of an object.]
                           CheckOutForm.QCheck.Page_Load(Object sender, EventArgs e) in d:\devsite\tap\checkoutform\qcheck.aspx.cs:44
                           System.Web.UI.Control.OnLoad(EventArgs e) +67
                           System.Web.UI.Control.LoadRecursive() +35
                           System.Web.UI.Page.ProcessRequestMain() +750

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          Yeah, you're def getting a fresh session there (Whether it thinks your a new user or for some reason flushed the existing, I don't know)
                          What is your session timeout set to?
                          Also, you know when you do a build of the website your session values are wiped out.

                          You could try packet watching to see if the session ID stays the same in all the request/responses

                          Comment

                          Working...