<p>Take a .Net Winforms App.. mix in a flakey wireless network connection, stir with a few users who like to simply pull the blue plug out occasionally and for good measure, add a Systems Admin that decides to reboot the SQL server box without warning now and again just to keep everyone on their toes.</p>

<p>What are the suggestions and strategies for handling this sort of scenario in respect to :</p>

<ul>
<li><p>Error Handling - for example, do you wrap every
call to the server with a Try/Catch
or do you rely on some form of
Generic Error Handling to manage
this? If so what does it look like?</p></li>
<li><p>Application Management - for example, do you
    disable the app and not allow users
    to interact with it until a
    connection is detected again? What would you do?</p></li>
</ul>

[{'answer_id': 49432, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>In our application, we give the user the option to connect to another server, e.g., if the database connection fails, a dialog box displays saying that the server is unavailable, and they could input another IP address to try.</p>\n'}, {'answer_id': 49433, 'author': 'Simon Keep', 'author_id': 1127460, 'author_profile': 'https://Stackoverflow.com/users/1127460', 'pm_score': 1, 'selected': False, 'text': '<p>We have this in our <code>Main()</code> method which traps all unhandled exceptions...</p>\n\n<pre><code>Application.ThreadException += new \nSystem.Threading.ThreadExceptionEventHandler(UnhandledExceptionCatcher);\n\nThread.GetDomain().UnhandledException += new \nUnhandledExceptionEventHandler(Application_UnhandledException);\n</code></pre>\n\n<p>and then <code>Application_UnhandledException</code> and <code>UnhandledExceptionCatcher</code> display user friendly messages.</p>\n\n<p>In addition the application then emails data such as the stack trace to the developers which can be very useful.</p>\n\n<p>It depends on the app of course but for the kind of failures that you describe I would close the app down.</p>\n'}, {'answer_id': 49447, 'author': 'Roland Tepp', 'author_id': 1712, 'author_profile': 'https://Stackoverflow.com/users/1712', 'pm_score': 2, 'selected': False, 'text': '<p>I have not touched WinForms and .NET for years now, so I can not give you any technical details, but there is the lager picture answer:</p>\n\n<p><strong>First and foremost - do not bind your form data directly to a database.</strong></p>\n\n<p>Create a separate data/model layer that you bind your form widgets to.</p>\n\n<p>From there on, you have several options available to you depending on the level of stability and availability you need to provide.</p>\n\n<p>Probably one of the simplest solutions here would be to just enable/disable the parts of application that need to interact with a database based on the connection state.</p>\n\n<p>Next level of protection would include caching the part of the data model locally and while the database connection is down, using local cache for viewing and disabling any functions that require explicit database connection.</p>\n\n<p>Probably the trickiest thing (that may also provide the most stable experience to the end user) is to replicate the database locally and use some sort of synchronization schema to keep your copy of the database in sync with remote db.</p>\n'}, {'answer_id': 49453, 'author': 'Brad Leach', 'author_id': 708, 'author_profile': 'https://Stackoverflow.com/users/708', 'pm_score': 1, 'selected': False, 'text': '<p>This may be a little <em>too</em> much support for the offline scenario, but have you considered the "<a href="http://msdn.microsoft.com/en-us/library/cc307159.aspx" rel="nofollow noreferrer">Microsoft Sync Framework</a>"? Included in the framework is the "Sync Services for ADO.NET 2.0", which allows your application to hit a local SQL Server CE instance. This can easily be synchronized with a central SQL Server via a variety of methods.</p>\n\n<p>This framework handles the permanent offline scenario, and as I said, it may not be appropriate for your specific requirements, however it will give your application solid offline support.</p>\n'}, {'answer_id': 49460, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': True, 'text': '<p>Answer depends on type of your application. There are applications that can work offline - Microsoft Outlook for example. Such applications doesn\'t treat connectivity exceptions as critical, they can save your work locally and synchronize it later. Another applications such as online games will treat communication problem as critical exception and will quit if connection gets lost.</p>\n\n<p>As of error handling, I think that you should control exceptions on all layers rather than relying on some general exception handling piece of code. Your business layer should understand what happened on lower layer (data access layer in our case)  and respond correspondingly. Connection lost should not be treated as <em>unexpected exception</em> in my opinion. For good practices of exceptions management I recommend to take a look at <a href="http://msdn.microsoft.com/en-us/library/cc309505.aspx" rel="nofollow noreferrer">Exception Handling Application Block</a>.</p>\n\n<p>Concerning application behavior, you should answer yourself on the following question "Does my application have business value for customer in disconnected state?" In many cases it would be beneficial to end user to be able to continues their work in disconnected state. However such behavior tremendously hard to implement.</p>\n\n<p>Especially for your scenario Microsoft developed <a href="http://msdn.microsoft.com/en-us/library/cc540753.aspx" rel="nofollow noreferrer">Disconnected Service Agent Application Block</a></p>\n'}, {'answer_id': 49487, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 0, 'selected': False, 'text': '<p>Use something like <a href="http://www.sqlite.org/" rel="nofollow noreferrer">SQLite</a> to store data offline until a connection is available.</p>\n\n<p>Update: I believe SQLite is the back end for <a href="http://gears.google.com/?hl=en" rel="nofollow noreferrer">Google Gears</a>, which from my understanding does what you\'re looking for in web apps... though I don\'t know if it can be used in a non web context.</p>\n'}]
['https://Stackoverflow.com/questions/49426', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3849/']