Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6158951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:02:53+00:00 2026-05-23T21:02:53+00:00

We have an ASP.NET application that uses SQL Server to store its session state.

  • 0

We have an ASP.NET application that uses SQL Server to store its session state. This works great except when it doesn’t 😉

Our database servers are hosted by a 3rd party and they periodically perform maintenance on the SQL server. When this happens we want our code to gracefully detect db connection errors and display a friendly message that we are under maintenance. We wrote an HttpModule that runs with each request and instantiates an instance of our Entity Framework container. If the instantiation fails for any reason we know there are connection issues and we redirect the user to a maintenance page.

This works fine until we turn our session state server setting on. Session state accesses its connection string before this module is run. We then get an ugly connection error for session state.

How can we catch this error? Is there a place in our application where we can catch/detect session state connection errors and switch over to “in-process” session state?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:02:54+00:00Added an answer on May 23, 2026 at 9:02 pm

    You should just be able to catch the error in the Application_Error method in Global.asax. Something like this will work, although you may want to make the check for if the exception is a Session related exception more robust. Unfortunately, ASP.NET doesn’t throw a SessionException, just an HttpException that wraps a SqlException.

    protected void Application_Error()
    {
        Response.Clear();
        var ex = Server.GetLastError();
        if (ex is HttpException && 
            ex.Message.Contains("Unable to connect to SQL Server session database"))
        {
            Server.Transfer("/Maintenance.aspx");
        }
        else
        {
            Response.Write("Other error occurred.");
            Response.Write(ex.ToString());
            Response.End();
        }
    }
    

    In Maintenance.aspx make sure you set the EnableSessionState page directive to false. You can use a Server.Transfer to prevent the Session module from running again for this request and causing another exception to occur.

    EDIT

    Did a little testing, and it is probably better to use the following check to test if session is not working:

    if (ex is HttpException && Context.Session == null)
    

    Or possibly even just:

    if (Context.Session == null)
    

    Context.Session should always be null if the Session module fails (unfortunately I can’t find any documentation to support that assertion, but in my testing that was the case).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.Net Application that uses a SQL Server database. I am also
I hope this makes sense. I have a ASP.NET web application that uses Entity
I have an application built in ASP.NET MVC 3 that uses SQL CE for
I have a ASP.NET application that connects to a SQL server backend on another
I have a very unusual problem. I have an ASP.NET application that uses LINQ-to-SQL
I have an asp.net application (relying party) that uses a passive STS for authentication
We have an ASP.NET application running at a customer site that uses ActiveDirectory for
I have an ASP .NET application that connects to an Oracle or a SQL
I have just re-implemented an ASP.NET web application that uses NHibernate to use a
I have just started a new web application with asp.net mvc and sql server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.