Page 1 of 1

Connection not closed error

Posted: Tue 16 Apr 2013 22:09
by russelle
This is in a Windows Store app:
I have a TextBox that accepts a search string. I am searching for Households by name of household member, address (house number) or phone number. When a Search button is clicked, the following code is executed:

Code: Select all

private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            SaveChanges();
            HouseholdCountTextBlock.Text = "";
            SearchProgressRing.IsActive = true;
            PerformSearch(HouseholdSearchCriteria.Text);
        }

async void PerformSearch(string criteria)
        {
            IQueryable<Data.HouseholdSummary> query = null;
            if (!String.IsNullOrEmpty(criteria))
            {
                if (criteria.Length < 3)
                {
                    var messageDialog = new MessageDialog("Search criteria must be at least 3 characters.");
                    messageDialog.Commands.Add(new UICommand("OK"));
                    await messageDialog.ShowAsync();
                }
                else
                {
                    // Search criteria is not null or empty
                    if (RubinEubanksCommon.IsNumeric(criteria, System.Globalization.NumberStyles.Integer))
                    {
                        // Search criteria is an integer
                        if (criteria.Trim().Length >= 7)
                        {
                            // Search criteria is assumed to be a phone number
                            if ((bool)DisplayInactiveCheckBox.IsChecked)
                            {
                                query = from h in App.NPDDatabase.HouseholdSummaries
                                        where h.Phone1.Contains(criteria)
                                        || h.Phone2.Contains(criteria)
                                        orderby h.LastName, h.FirstName, h.MiddleName
                                        select h;
                            }
                            else
                            {
                                query = from h in App.NPDDatabase.HouseholdSummaries
                                        where (h.Phone1.Contains(criteria)
                                           || h.Phone2.Contains(criteria))
                                           && h.IsActive == true && h.HouseholdMemberIsActive == true
                                        orderby h.LastName, h.FirstName, h.MiddleName
                                        select h;
                            }
                        }
                        else
                        {
                            // Search criteria is assumed to be an address
                            if ((bool)DisplayInactiveCheckBox.IsChecked)
                            {
                                query = from h in App.NPDDatabase.HouseholdSummaries
                                        where h.AddressLine1.Contains(criteria)
                                        orderby h.LastName, h.FirstName, h.MiddleName
                                        select h;
                            }
                            else
                            {
                                query = from h in App.NPDDatabase.HouseholdSummaries
                                        where h.AddressLine1.Contains(criteria)
                                           && h.IsActive == true && h.HouseholdMemberIsActive == true
                                        orderby h.LastName, h.FirstName, h.MiddleName
                                        select h;
                            }
                        }
                    }
                    else
                    {
                        // Search criteria is assumed to be a name part
                        if ((bool)DisplayInactiveCheckBox.IsChecked)
                        {
                            query = from h in App.NPDDatabase.HouseholdSummaries
                                    where h.LastName.Contains(criteria)
                                       || h.FirstName.Contains(criteria)
                                       || h.MiddleName.Contains(criteria)
                                    orderby h.LastName, h.FirstName, h.MiddleName
                                    select h;
                        }
                        else
                        {
                            query = from h in App.NPDDatabase.HouseholdSummaries
                                    where (h.LastName.Contains(criteria)
                                       || h.FirstName.Contains(criteria)
                                       || h.MiddleName.Contains(criteria))
                                       && h.IsActive == true && h.HouseholdMemberIsActive == true
                                    orderby h.LastName, h.FirstName, h.MiddleName
                                    select h;
                        }
                    }
                    if (query != null)
                    {
                        householdCount = query.AsEnumerable().Count();
                    }
                    else
                    {
                        householdCount = 0;
                    }
                }
                if (householdCount == 0)
                {
                    var messageDialog = new MessageDialog("No households found with specified search criteria.");
                    messageDialog.Commands.Add(new UICommand("OK"));
                    await messageDialog.ShowAsync();
                }
                else
                {
                    HouseholdSelectionListView.ItemsSource = await query.ToListAsync();
                    if (householdCount == 1)
                    {
                        HouseholdCountTextBlock.Text = "1 Household Member";
                        HouseholdSelectionListView.SelectedIndex = 0;
                    }
                    else
                    {
                        HouseholdCountTextBlock.Text = householdCount + " " + "Household Members";
                    }
                }
            }
            SearchProgressRing.IsActive = false;
        }

private async void SaveChanges()
        {
            if (App.NPDDatabase.GetChangeSet().Updates.Count() >= 1)
            {
                await App.NPDDatabase.SubmitChangesAsync();
                var messageDialog = new MessageDialog("Database changes made.");
                messageDialog.Commands.Add(new UICommand("OK"));
                await messageDialog.ShowAsync();
            }
        }
I wanted to invoke the search when the Enter key is pressed while in the search criteria TextBox. Since there is no Default property for WinRT controls, I implemented a Page_Keydown event handler:

Code: Select all

private void Page_Keydown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                this.SearchButton.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                SearchButton_Click(SearchButton, e);
            }
        }
The Search button works fine, but when I try to invoke the Page_Keydown event using the Enter key, the statement

householdCount = query.AsEnumerable().Count();

in the PerformSearch method generates an "Error on opening DbConnection; The connection was not closed" message. When I step through the code slowly, it sometimes works. This leads me to believe it may be some kind of async timing problem.

Anyone have any ideas why this implementation of the Keydown event won't work?

Re: Connection not closed error

Posted: Thu 18 Apr 2013 10:31
by MariiaI
Could you please send us a sample project with which this behaviour could be reproduced.