Session object lost when page is changed

Discussion in 'ASP.NET 2.0' started by MaverickUK, Feb 19, 2007.

  1. For some reason, when I store something to a session, the session is then lost if I change the page I'm looking at when it's hosted here at asp.net.

    I've set-up the following page to better demostrate the problem. If you goto http://www.crap-co.com/test.aspxin the trace you'll see the line "No session". This because the code tries to view the contents of a session, which doesn't yet exist. When it finds the session doesn't exist, it then creates it.

    So if you then refresh the page, that line will change to "Session OK" as now the code can see the session it just created.

    However if you now change your URL to http://www.crap-co.comand then back to http://www.crap-co.com/test.aspx it'll once again say "No session".

    A session shouldn't disapear when the page is changed. However that is what's happening here.

    Does anyone know what is going on?

    Regards
    Pete
     
  2. In the case of test.aspx the code is as follows


     
  3. There doesn't appear to be anything wrong there. Is there anything in your default.aspx page that could be clearing the session?


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  4. The default.aspx page (index.aspx) doesn't contain any reference to any sessions.

    If there was a Session.Remove("xxx") about it might make some sense :)

    I really don't understand what's going on here, but it's a big problem, as I can't publically launch this website until this session issue is resolved!
     
  5. I've removed the session settings from the Web.Config file, but still no dice!

    Something fishy is going on here... [​IMG]
     
  6. I actually added that into the Web.Config to try and fix this problem originally :)

    I'll remove it later this morning and see if it makes any difference.
     
  7. I guess it wasnt happening while database access ,but since the database file was in there,it must be creating an issue during compilation.
    You should use App_Data instead to keep all your Database files.

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  8. I've worked out what was going on! :)

    Thanks for your time on this Joel and also Wisemx.

    The problem was that I'd put my database in the /App_code directory. Which meant that everytime the database was accessed, it would cause an application restart and all in memory sessions would be lost.

    I was the first time I'd used the /App_code direction and I thought I'd heard it was a good place to put certain files, like databases.... which it isn't!

    Ah well, I've learnt something new, the hard way! :)

    Pete
     
  9. You shouldtestTrace properly before using it each time.

    Even though youenabled itin the page directive trytesting the state ofTrace in the Page_Load.


    private void Page_Load(object sender, System.EventArgs e)</o:p>
    {</o:p>
    if (Trace.IsEnabled)</o:p>
    {</o:p>
    Trace.Write("MyBeautifulTraceIsEnabled");</o:p>
    }</o:p>
    }
     
  10. Good catch guys.


    F.Y.I., yesterday Al Pascual blogged about something interesting concerning Cache and System State:
    http://alpascual.com/blog/al/archive/2007/02/21/I-was-wrong-again-about-Cache_2E00_.aspx



    ?Cache is stored in memory on each server within each asp.net application.
    There is no cache synchronization, hence the need for dependencies which you can use to ensure each cache on each server.
    Cache does not share or use in any way the asp.net session state server.
    When you add something to cache you're committing it to memory; if the application is shut down that cached data is lost and has to be re-created.?
     
  11. AAAAHHHHHH!! haha

    Yes, any file additions or modifications done inside the App_Code directory will cause your application to recompile.Recompile will clear your sessions. So if your DB is in there, it'll be recompiling a lot. Good catch, never would have guessed that.

    Your database will be much happier inside it's new home App_Data.


    Also, if you use sql sessions, recompiles will NOT cause your sessions to become destroyed.


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     

Share This Page