session variable getting lost

Discussion in 'ASP .Net' started by SAL, Nov 6, 2007.

  1. SAL

    SAL Guest

    Okay, don't bash me to hard for my design on this app, it's my first web app
    and it's in production.
    My basic design is using Datatables created via the designer with a business
    logic class in between the datatable and ObjectDataSources.
    In one page I had a Gridview with select enabled. When an row in the grid is
    selected, I retrieve the SelectedValue, store the value in a Session
    variable and redirect the response to another web page, which, has an
    ObjectDataSource on it and a DetailsView. The ObjectDataSource on this page
    retrieves the value of the Session variable set from the first page. The
    problem is that the Session variable is randomly losing its value.
    So, the ObjectDataSource's Business object's SelectMethod accepts a value to
    select records by. How do I know that the value is getting lost?
    Because no rows are returned as:
    dr = dtAdapter.Rows(0)

    throws an exception IndexOutOfRange.

    My question is, is there some reason that my session variable evaporates
    randomly?

    Thanks in advance

    SAL
     
    SAL, Nov 6, 2007
    #1
    1. Advertisements

  2. No. Session variables will be lost when the Session times out, or when they
    are overwritten / removed - does either of these situations apply here...?
     
    Mark Rae [MVP], Nov 6, 2007
    #2
    1. Advertisements

  3. SAL

    SAL Guest

    Mark, thanks for the reply.
    I do not see either of these things applying here. This happened twice this
    morning for no apparent reason and then it didn't happen. I have not changed
    the time out so it should still be 20 minutes and I am not overwriting them
    as far as I can see. I can not reproduce it on my machine.

    S
     
    SAL, Nov 6, 2007
    #3
  4. SAL

    Guest Guest

    Sal,
    The only time a Session item would be lost under "normal circumstances"
    (e.g. it hasn't timed out by itself due to no page requests) is that IIS is
    recycling the application. Recycling can be because of IIS settings, because
    something touched one or more of the files in the web folder "tree", or
    because you've got buggy code that's causing unhandled exceptions.
    In any of these cases, you can bet that your Application, Session and Cache
    items will all go down the potty.
    Peter
    --
    http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    BlogMetaFinder: http://www.blogmetafinder.com



     
    Guest, Nov 7, 2007
    #4
  5. Hi SAL,

    Based on my understanding, you're redirecting to the second page right
    after setting the session variable, right? If this is the case, there's no
    reason the session variable will be lost due to session timeout. Could you
    please double check if the session variable is really lost or it's because
    the query that uses the variable to retrieve records from database returns
    empty result?

    By the way, there's another way to pass variables between pages using
    Context.Items and Server.Transfer:

    #Famil Jones : Pass Variables Between ASP.NET Pages
    http://www.dotnetjunkies.com/WebLog/familjones/archive/2004/04/08/11020.aspx


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 7, 2007
    #5
  6. SAL

    SAL Guest

    Hmmm, thank you for the replies all.
    Would it be possible for Walter or someone else to comment on the load that
    using the Server.Transfer pattern would place on the server? So, is there an
    increased load on the server using this model as compared to using Session
    variables?

    Thanks
    SAL

     
    SAL, Nov 7, 2007
    #6
  7. SAL

    SAL Guest

    Okay, so let's say that all of the things you've listed below are not
    happening. But, that a Response.Redirect may be. The documentation states
    that Redirect calls End which raises a ThreadAbortException. I do not see
    this exception however. I am wondering if this could be at the root of my
    random problem here...

    Thanks for your responses
    S

     
    SAL, Nov 7, 2007
    #7
  8. Here are some guesses why session might be lost.

    1. Host must be the same. Example http://www.mysite.com and
    http://mysite.com might be 2 different sites. Thus browser does not pass
    cookies from one to another. Thus session might be lost. If for example you
    logged in into http://mysite.com/login.aspx and then got redirected to
    http://www.mysite.com

    2. Basically comes from #1. http://www.mysite.com and http://132.234.123.123
    are 2 different sites even if www.mysite.com resolves to 132.234.123.123

    3. Application is recycled.
    To verify #3 add folowing code to Global.asax You need to replace last
    line clsGlobal.SendEmail with your emailing code.
    This will send an email to you with a reason everytime application was
    shutdown. Thus you will know.....Unless of course someone pulls a power plug
    from the server. Then no email will be sent :)

    protected void Application_End(Object sender, EventArgs e)

    {

    HttpRuntime runtime =
    (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",

    BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null,
    null, null);

    if (runtime == null)

    return;

    string shutDownMessage =
    (string)runtime.GetType().InvokeMember("_shutDownMessage",

    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
    null, runtime, null);

    string shutDownStack =
    (string)runtime.GetType().InvokeMember("_shutDownStack",

    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
    null, runtime, null);

    //send email to me

    clsGlobal.SendEmail(String.Format("\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}",

    shutDownMessage,shutDownStack), "Application Shutdown",
    , null, null);

    }



    George
     
    George Ter-Saakov, Nov 7, 2007
    #8
  9. Hi SAL,

    Regarding difference between Response.Redirect and Server.Transfer, I think
    following page is useful for your reference. Each approach has its own pros
    and cons.

    #Response.Redirect vs Server.Transfer
    http://haacked.com/archive/2004/10/06/responseredirectverseservertransfer.as
    px

    Back to the original issue about losing session after Redirect, I think
    George's suggestion is quite helpful.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 8, 2007
    #9
  10. SAL

    SAL Guest

    Thank you for your reply and the article link.
    I am currently investigating George's suggestion as well

    S
     
    SAL, Nov 8, 2007
    #10
  11. SAL

    SAL Guest

    George, thanks for you suggestion. I have implemented the Application_End
    routine in Global.aspx and the application is getting shut down with the
    following information. If you can help it would be great:

    HostingEnvironment caused shutdown

    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
    at System.Environment.get_StackTrace()
    at System.Web.HttpRuntime.ShutdownAppDomain()
    at System.Web.Hosting.HostingEnvironment.ShutdownThisAppDomainOnce()
    at
    System.Web.Hosting.HostingEnvironment.InitiateShutdownWorkItemCallback(Object
    state)
    at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object
    state)
    at System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state)
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object
    state)


    S
     
    SAL, Nov 8, 2007
    #11
  12. SAL

    kpg* Guest

    Please post back here if (and when) you find the culprit.

    I have stopped using sesion variables altogether becuase of
    similar issues on a client's server, but am very interested
    in what you figure out.

    Now my client may well be changing the web.config file
    which will cause the application to restart, all the more
    reason to not use session varialbes (in my case).

    Instead I pass info encrypted in the query string, which
    works just fine, the only session variable I use is to
    relay error messages to tan error page - if that gets
    lost, oh well.

    kpg
     
    kpg*, Nov 8, 2007
    #12
  13. Hi SAL,

    This looks like a normal shutdown, try to use following method to log the
    cause and the time it occurs:

    #Logging ASP.NET Application Shutdown Events - ScottGu's Blog
    http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 9, 2007
    #13
  14. Hi SAL,

    How's the issue now?

    Not directly related, but I think following information might be useful:

    #Thomas Marquardt's Blog : ASP.NET File Change Notifications, exactly which
    files and directories are monitored?
    http://blogs.msdn.com/tmarq/archive/2007/11/02/asp-net-file-change-notificat
    ions-exactly-which-files-and-directories-are-monitored.aspx


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 14, 2007
    #14
  15. SAL

    SAL Guest

    Okay, I did that. Now I'm getting this in the event log:

    The description for Event ID ( 0 ) in Source ( .NET Runtime ) cannot be
    found. The local computer may not have the necessary registry information or
    message DLL files to display messages from a remote computer. You may be
    able to use the /AUXSOURCE= flag to retrieve this description; see Help and
    Support for details. The following information is part of the event:

    _shutDownMessage=HostingEnvironment caused shutdown

    _shutDownStack= at System.Environment.GetStackTrace(Exception e, Boolean
    needFileInfo)
    at System.Environment.get_StackTrace()
    at System.Web.HttpRuntime.ShutdownAppDomain()
    at System.Web.Hosting.HostingEnvironment.ShutdownThisAppDomainOnce()
    at
    System.Web.Hosting.HostingEnvironment.InitiateShutdownWorkItemCallback(Object
    state)
    at System.Threading.

    Which I really don't understand. Help?

    S
     
    SAL, Nov 14, 2007
    #15
  16. SAL

    SAL Guest

    Hi Walter. It's just as bad as ever and what I don't get is that, I'm not
    changing anything when this stuff occurs that I'm aware of. All I'm doing is
    redirecting to another page, changing DetailsView states to edit, cancelling
    the edit, clicking another menu item and doing the same thing all over again
    and it will restart which causes the ID of the item the viewer is using to
    be lost in the session variable. And, since the ObjectDataSource is getting
    records based on this ID, the app fails with a web page that isn't showing
    any data.

    S
     
    SAL, Nov 14, 2007
    #16
  17. SAL

    SAL Guest

    kpg,
    what I figured out was the app was getting recycled due to a limit I had set
    on virtual memory. There are times when the virtual memory usage jumps up
    dramatically and then it pops right back down. The app does not appear to be
    leaking memory at this time. So, I unchecked that option in the App pool's
    configuration.
    I used information garnered from this link to aid me in my quest, although I
    haven't completely implemented Peter's Exception Engine.
    http://www.eggheadcafe.com/articles/20030816.asp

    I placed this code in my Application_Error routine in my Global.asax file:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
    ' check to see if the ASPNETApplication log exists
    Dim b As Boolean
    Try
    b =
    Convert.ToBoolean(ConfigurationManager.AppSettings("logAppErrors"))
    Catch ex As Exception

    End Try
    If Not b Then Exit Sub

    If Not System.Diagnostics.EventLog. _
    SourceExists("AnnexationTrackerApplication") Then
    System.Diagnostics.EventLog. _
    CreateEventSource("AnnexationTrackerApplication",
    "Application")
    End If
    System.Diagnostics.EventLog. _
    WriteEntry("AnnexationTrackerApplication",
    Server.GetLastError().GetBaseException().Message)

    End Sub

    The message appeared in the system event log not the application event log.

    HTH
    S
     
    SAL, Nov 15, 2007
    #17
  18. There is a problem with losing values in Session Variable. I am
    currently troubleshooting as to why. I am working with tabs and all
    code is on the same page and my session variables are dropping.
    Microsoft acknowledge this is a problem:
    http://support.microsoft.com/kb/316148. I am still trying to figure out
    why I am losing my session variables. None of the issues mentioned here
    or on microsoft have been found to be the culprit.

    If anyone knows why, I would like to know.
     
    Carlos Vasquez, Mar 28, 2008
    #18
  19. That KB is over 2 years old.

    Antivirus scans are but one of the possible reasons for applications to restart,
    which in turn causes the loss of session variables if session state is kept in-process.

    Modifying the source code of your Web application will
    cause ASP.NET to recompile source files into assemblies.

    When you modify the top-level items in your application, all other assemblies
    in the application that reference the top-level assemblies are recompiled as well.

    In addition, modifying, adding, or deleting certain types of files
    within the application's known folders will cause the application to restart.

    The following actions will cause an application restart:

    1. Adding, modifying, or deleting assemblies from the application's Bin folder.

    2. Adding, modifying, or deleting localization resources
    from the App_GlobalResources or App_LocalResources folders.

    3. Adding, modifying, or deleting the application's Global.asax file.

    4. Adding, modifying, or deleting source code files in the App_Code directory.

    5. Adding, modifying, or deleting Profile configuration.

    6. Adding, modifying, or deleting Web service references in the App_WebReferences directory.

    7. Adding, modifying, or deleting the application's Web.config file.
     
    Juan T. Llibre, Mar 28, 2008
    #19
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.