Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0
Hi.
 
I have a WCF service that retrieves information from a database. It does so as follows:
 
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:
 
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:
 
<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 24-Dec-12 19:05pm
Wamuti1.5K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I'm not sure, but give it a try.
In try block, at very first line, write following code :
 
try
{
    booksList.Clear();
    
    // rest of the code..
    .. .. ..
    .. .. ..
}
  Permalink  
Comments
Wamuti at 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. :-(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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);
 
            }
});
 
        }
  Permalink  
v2
Comments
Wamuti at 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 at 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 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 878
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100