locked
Question about Session object (C# and Asp.net) RRS feed

  • Question

  • Hi all, 

    Just trying to figure out how to pass data between web pages using session variable in ASP.net & C# (VS2010). 

    Page one sets the variable using: 

     

    Session["FirstName"] = txtFirstName.Text;
    Session["LastName"] = txtLastName.Text;

     

    Page two tries to set the values to two text boxes using: 

    txtFirstName.Text = Session["FirstName"].ToString();
    txtLastName.Text = Session["LastName"].ToString(); 

     

    However I'm getting the following message at the line that does "txtFirstName.Text = Session["FirstName"].ToString();"

    Object reference not set to an instance of an object.

     

    Both pages are using System.Web.SessionState 

     

    Do I need to instantiate a new session object on the first page? 

    Regards

    Dave 

     

    • Moved by Rudedog2 Wednesday, June 2, 2010 12:30 PM : ASP.NET is off-topic (From:Visual C# General)
    Tuesday, June 1, 2010 7:08 PM

Answers

  • I don't think you need to instantiate it first but there is a in-depth example here wiht several pages of code

    http://msdn.microsoft.com/en-us/magazine/cc163730.aspx

    There are examples of coupling and decoupling objects there.

    If this doesn't help could you please post all relevant code.

    Thanks

    • Marked as answer by york0001 Thursday, June 3, 2010 8:49 AM
    Tuesday, June 1, 2010 7:42 PM

All replies

  • Tuesday, June 1, 2010 7:17 PM

  • In order to keep the code sample short I omitted the part about checking if Session["FirstName"] != null 
    However that wasn't my question.   

    Try reading this first

    http://msdn.microsoft.com/en-us/library/03sekbw5.aspx


    Tuesday, June 1, 2010 7:26 PM
  • I don't think you need to instantiate it first but there is a in-depth example here wiht several pages of code

    http://msdn.microsoft.com/en-us/magazine/cc163730.aspx

    There are examples of coupling and decoupling objects there.

    If this doesn't help could you please post all relevant code.

    Thanks

    • Marked as answer by york0001 Thursday, June 3, 2010 8:49 AM
    Tuesday, June 1, 2010 7:42 PM
  • For questions related to ASP.NET use the ASP.NET forum .
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, June 1, 2010 7:53 PM
  • What is: txtFirstName?

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Wednesday, June 2, 2010 8:41 AM
  • Hi,

    I think you are loosing the state of your session variable somewhere. Try to debug better your code, and see if you are loosing your session variable.

    As i can see, everything is ok. You dont need to instantiate a new State object.

     

    Best Regards,

    JP.

    Wednesday, June 2, 2010 9:27 AM
  • Page one sets the variable using:

    Session["FirstName"
    ] = txtFirstName.Text;
    Session["LastName" ] = txtLastName.Text;

     Page two tries to set the values to two text boxes using: 

    txtFirstName.Text = Session["FirstName"
    ].ToString();
    txtLastName.Text = Session["LastName" ].ToString();

    Are you using window.showModalDialog for the Page Two.

    If yes please put <base target="_self" /> in the head of Page Two, it will work.

    Or put it at the window.showModalDialog page.

     

    Thanks,

    Harry.

    ---------------------------------------------------------------------

    Mark it answer if it helps.

    • Proposed as answer by Harry_Bhai Wednesday, June 2, 2010 10:57 AM
    Wednesday, June 2, 2010 10:57 AM
  • What is: txtFirstName?

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...


    Hi,

    It's a textbox.

    Thursday, June 3, 2010 8:47 AM
  • Thanks for all the responses, got it working now.

     

    Dave

    Thursday, June 3, 2010 8:50 AM