-1

when I run my application, I often get an error "The underlying provider failed on Open".

failed on open

I want to ask, how can I handle this error and create a MessageBox that show "Restart" and "Cancel" Button to continue the application.

Thanks before with your help.

Irshad
  • 3,071
  • 5
  • 30
  • 51
Amirul
  • 55
  • 2
  • 8

1 Answers1

1

You've got to know where your code is throwing an exception and then handle it with a try-catch block:

try
{
    // Code that might throw an exception
}
catch (EntityException)
{
    // MessageBox.Show(...)
}

Note that you should always handle the most specific exception in your catch block.

mrahhal
  • 3,322
  • 1
  • 22
  • 41
  • I already have a `try catch` `try { } catch (Exception ex) { MessageBox.Show(ex.InnerException.ToString()); if (ex.Message.ToString() == "The underlying provider failed on Open") { MessageBox.Show("provider failed"); this.Close(); } }` – Amirul Mar 17 '16 at 05:01
  • @Amirul doing a comparison with the exception's message is a very bad idea. Just drop the comparison and catch `EntityException` like I did above. – mrahhal Mar 17 '16 at 05:04
  • i want to restart my form when i catch that erro – Amirul Mar 17 '16 at 05:04
  • If it's your main form I don't think that's possible. You can factor the logic that is connecting to the database into a method and just call that method again after an exception occurs and the user asks to restart. – mrahhal Mar 17 '16 at 05:06
  • If you can show us how (relative your your form's logic) you're connecting with the database we'll have a better picture. – mrahhal Mar 17 '16 at 05:07
  • How do I can show a messagebox to user, that ask them to restart or not – Amirul Mar 17 '16 at 05:08
  • @Amirul you're asking the same question, we can't get anywhere like this. Please show us some of your code so that we know what we're facing here. – mrahhal Mar 17 '16 at 05:09
  • `try { } catch(Exception ex) { MessageBox.Show(ex.InnerException.ToString()); if (ex.Message.ToString() == "The underlying provider failed on Open") { MessageBox.Show("provider failed"); this.Close(); } }` that's what i have, I think when i get an error like that, i want to show a custom messagebox – Amirul Mar 17 '16 at 06:29
  • 1
    @Amirul I'm sorry but you're not being too helpful. You're repeating the same things all over again. I think your problem is comparing the exception's message as a string. You should catch `EntityException` instead. I've already said that so please try it before going on. – mrahhal Mar 17 '16 at 08:57