Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have a WCF service that retrieves information from a database. It does so as follows:

C#
public List<string> ShowAllBooks()
        {
            List<string> booksList = new List<string>();
            try
            {

                using (bookcatalogueEntities context = new bookcatalogueEntities())
                {
                    // Fetch the product number of every product in the database
                    var books = from b in context.catalagues
                                   select b.Title;
                    booksList = books.ToList();
                }
            }
            catch
            {
                // Ignore exceptions in this implementation
            }
            // Return the list of product numbers
            return booksList;
        }
    }


When the WPF Client starts, the items (books) should be shown in a list box. I have this for the list box code behind:

C#
private void ShowAllBooks()
        {
            CatalogueManagerClient.ServiceReference.BookCatalogueServiceClient proxy = new BookCatalogueServiceClient();
            try
            {



                lbBooks.Dispatcher.Invoke(new Action(() =>
                {

                    lbBooks.ItemsSource =  proxy.ShowAllBooks();
                }), DispatcherPriority.ApplicationIdle);
                //books = App.BookCatalogue.GetBook(query);
                //lbBooks.ItemsSource = books;
                txtBookPath.Foreground = Brushes.Black;
                txtBookPath.Text = "Ready";
            }
            catch (CommunicationException ex)
            {
                MessageBox.Show(proxy.State.ToString() + " ..."+ex.Message);

            }

        }

and for the xaml part, i have:

C#
<ListBox Height="440"  KeyDown="lbBooks_KeyDown"  MouseDoubleClick="lbBooks_MouseDoubleClick"  Name="lbBooks" SelectionChanged="lbBooks_SelectionChanged"  Width="210" />


The problem is that when the client starts, it can show the books in the listbox. The problem is that when i select a book, it throws a run time error called:

InvalidOperationException

Stating:

Items collection must be empty before using ItemsSource.

I have tried googling for a solution and gone through articles and problems here but i cannot find a matching problem. Please assist on how to resolve this problem.
Posted

I'm not sure, but give it a try.
In try block, at very first line, write following code :

C#
try
{
    booksList.Clear();
    
    // rest of the code..
    .. .. ..
    .. .. ..
}
 
Share this answer
 
Comments
Wamuti 25-Dec-12 3:16am    
It just won't work. Since i am using a listbox called lbBooks, i have tried:

lbBooks.Items.Clear();

It is still giving me the same problem. :-(
What could be if operation of retrieving data from client can takes more than 1 minute? Does your UI will freeze ?? Basically YES,
so some hint:
C#
private void ShowAllBooks()
        {
System.Threading.Task.Factory.StartNew(()=>{
            CatalogueManagerClient.ServiceReference.BookCatalogueServiceClient proxy = new BookCatalogueServiceClient();
            try
            {
                lbBooks.Dispatcher.Invoke(new Action(() =>
                {
                    //this will workaround your issue
                    var serviceBooks= proxy.ShowAllBooks();
                    lbBooks.ItemsSource =  serviceBooks;
                }), DispatcherPriority.ApplicationIdle);
                //books = App.BookCatalogue.GetBook(query);
                //lbBooks.ItemsSource = books;
                txtBookPath.Foreground = Brushes.Black;
                txtBookPath.Text = "Ready";
            }
            catch (CommunicationException ex)
            {
                MessageBox.Show(proxy.State.ToString() + " ..."+ex.Message);
 
            }
});
 
        }
 
Share this answer
 
v2
Comments
Wamuti 25-Dec-12 3:18am    
My UI has no problem whatsoever. It does not freeze. Howerver, i tried what you advised me and there is no change. I suppose it is because the var keyword is just to give room not to bother with the data types but at runtime, it results to the same error. :-(
Oleksandr Kulchytskyi 25-Dec-12 3:32am    
For the first time you don't have a problem, i mean did you envisioning such term as network latency or slow internet connection? This is a kind of gold rule to get rid of redundance overheads on UI thread..
So lets go back to you problem. Would you mind to show to community declaration of your lbBooks in XAML ?
I can mistake , but if you do not provide ItemSource property with some binding , the error could be hidden there, so for example:
<ListBox x:Name="lbBooks" ItemsSource="{Binding}">....

and another way is to do next <ListBox x:Name="lbBooks" ItemsSource="{Binding ObsBooks}">

where ObsBooks is ObservableCollection<book> ObsBooks {get;set;} in your window.

Also i recommend to you change DispatcherPriority.ApplicationIdle to Background

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900