views:

731

answers:

3

I've got a WPF application that uses LINQ to SQL DataContexts.

What's a "best practice" way of testing the database connection and letting the user know that the connection is either bad or the network is down, etc, so that the app doesn't just bomb.

Right now, I get the Splash Screen and then a "Program has stopped working" while "Windows is checking for a solution to the problem".

Thanks!

+1  A: 

Try to run the app in debug-mode. It will tell you which exception gets thrown at you (uncatched exceptions will give you that nasty spalsh screen youre talking about :) ). Then catch that exception, and live happily forever after ..

cwap
A: 

The problem is that my datacontexts are accessed through ObjectDataProviders in the XAML, therefore I don't get a breakpoint. Does the ObjectDataProvider have some mechanism to fail elegantly, or do I test the Connectionstring in my MainForm startup, BEFORE the InitializeComponent call?

Still, in debug-mode you should get detailed exception information about any uncatched exception (unless you've turned it off in VS)
cwap
A: 

Try (no pun intended) a try/catch statement where you think it might do some good. I'm having a similar problem with an app I'm doing and this (for what it's worth) is what I added:

    private void txtCustomerNameSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        fillCustomerListDataContext dbC = new fillCustomerListDataContext();

        var fillCustList = from c in dbC.lstCustomers
                           where c.CustomerName.StartsWith(txtCustomerNameSearch.Text)
                           orderby c.CustomerName
                           select new
                           {
                               c.CustomerName,
                               c.CustomerID
                           };

        try
        {
            lstCustomerNames.ItemsSource = fillCustList;
            lstCustomerNames.DisplayMemberPath = "CustomerName";
            lstCustomerNames.SelectedItem = "CustomerID";
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error = " + ex.Message,"Keeping Amy off balance message");
        }
    }

Hope this helps.

Jack McG