tags:

views:

81

answers:

3

I have an application which makes calls to an online database. If I lose the connection to the internet or the database is down my application will crash.

What is the approach to handle this?

My own approach would be to have a object which keeps calling the database all the time and if an exception is thrown, then an event is fired which all the other components register to and which then would disable some functions.

Is this the right way or is there a better solution?

+3  A: 

I guess it depends on how important the functionality is. What you are trying to do is similar to what what the Smart Client Application Block tries to achieve. So it is probably worth looking into how it handles situations like this.

From memory I think most applications I have heard of actually attempt to cache data. So the application works off a local copy of the database, and syncs with the on-line one whenever it can connect.

SQL Server Express, for example, allows you to sync the database with another SQL Server database over a network.

So basically it depends on your data, if you can have a local cache that the user works from and that you can sync up later then you can go with that. Otherwise the approach you have described sounds like it should work.

Glenn Condron
+1  A: 

To check whether you're on the network or not, you can use .NET's NetworkInterface from System.Net.NetworkInformation namespace:

bool connected = NetworkInterface.GetIsNetworkAvailable();

As for the database being offline - you'll have to handle possible exceptions when calling that database in such a way that your app doesn't crash, but returns an informational message to the user - or does logging, or whatever makes sense in your scenario.

Marc

marc_s
+2  A: 

Difficult to say what would be best for you with knowing the bigger picture, but I can think of 3 ways you could potentially hand these situations:

1) handles the connectivity issues well at the point in time they happen and just report them nicely
2) like you say, periodically poll for connectivity and disable functionality if the "ping" fails
3) work with a local copy of the db, then synch updates up to the main db when connectivity is there

IMO, I'd go for 1) or 3). Option 3 gives the best user experience. Option 1 potentially involves the least dev effort.

With option 2, you have the issue that no matter how often you poll, you could hit a connectivity issue at any time.

AdaTheDev
First of all thx for all the answers! I think I will go with a solution of 2 and 3 combined. I need to be able to show if the application is online or not.
bobjink