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 8926691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:02:04+00:00 2026-06-15T08:02:04+00:00

How do I gracefully handle errors when trying to connect to a database that

  • 0

How do I gracefully handle errors when trying to connect to a database that may be down, or given an incorrect connection string in MVC 4?

I have my string in Web.config and the way it accesses the database is just instantiating my class derived from DBContext in each controller, like so:

private DBEntities db = new DBEntities("database");

This works fine if the database is up and everything is correct, but if it’s not, the pages on my web site display this error:

Exception Details: System.ComponentModel.Win32Exception: The network path was not found

Obviously I don’t want this to appear, what I’d like to do is have a way to try catch the error and display a custom error page (similar to 404 Page Not Found).

Thank you.

  • 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-06-15T08:02:06+00:00Added an answer on June 15, 2026 at 8:02 am

    Do the initialisation in the function

    private DBEntities db;
    
    // then in your function attempt to initialise
    try{
    
    db = new DBEntities("database");
    
    db.Connection.Open();
    
    }
    catch(Exception ex){
    
    if(db.Connection != ConnectionState.Closed){
    
    db.Connection.Close();
    db.Dispose();
    
    //go to error page
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page that uses the JSONP Plugin to gracefully handle errors
By closed connection I mean that the other end closed it gracefully. I would
I'm trying to figure out how to handle an error that gets thrown at
I am trying to implement hibernate on a legacy database (that still has a
I wanted to handle all internal errors gracefully, without program termination. As discussed here
I have to implement a Task subclass that gracefully fails if the broker is
I have a client/server question that i am trying to figure out the best
Im trying to send a Docx file via this form that I made, the
I have an app that is deployed to Tomcat 7 and is currently clustered
I have an error panel that is subbed in to a page if an

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.