Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

How to handle SQL Server Exceptions in Episerver



I am handling episerver application exceptions in global.asax file. Whenever any exception occur it redirects to 404 error page and exception get log in text file.

But when the SQL server is down it shows an sql server exception on page and does not redirect it to error page.

So how can I handle such types of exceptions in episerver.

Example of SQL server exception 

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Edited, Aug 21, 2017 14:12

<customErrors> ?

Aug 21, 2017 14:27

As Valdis are saying the best way to handle this is in web.config with customerrors and httperrors.

Aug 21, 2017 15:31


Is there any different option other than custom error.

Aug 22, 2017 9:47

can try to subscribe to unhandled exceptions in global.asax

Aug 22, 2017 9:52

Why do you not want to use the built in and proved effective solution with httperrors?

Aug 22, 2017 9:58


As such there is no issue in using built in solution.

Actullay I am handling exception in global.asax file like

protected void Application_Error(object sender, EventArgs e)
//Get error page url
var baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
var contentReference = new ContentReference(2664);
var url = ServiceLocator.Current.GetInstance<IContentRepository>().Get<CustomErrorPage>(contentReference).LinkURL;
var completeUrl = baseUrl + url;

//Global Exception handling
Exception exception = Server.GetLastError();

I am to handle all exception but If server is down then this approach gets fail.

Any ways I can go with customErrors approach also.

Aug 22, 2017 11:12

"server is down"? you mean no network access for it? there is great magnitude of edge cases what could possibly go wrong and how to hande.

Aug 22, 2017 13:26

Your biggest problem is that you are trying to show a Episerver page when you get an error and for examle if the SQL is down, this line will throw it's own error since it can not talk to the database.

var url = ServiceLocator.Current.GetInstance<IContentRepository>().Get<CustomErrorPage>(contentReference).LinkURL;

A page for 500-error should never be a dynamic page, it should always be static.

Aug 22, 2017 13:58

@Valdis Iljuconoks and @Henrik Fransas.

Okay,  I understood, thank you.

Aug 22, 2017 14:07
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.