15

I am using the following code to update a listbox, this recieving a list from a Web service:

client.userKeywordsCompleted += new EventHandler<userKeywordsCompletedEventArgs>(client_userKeywordsCompleted);
client.userKeywordsAsync();

Using:

void client_userKeywordsCompleted(object sender, userKeywordsCompletedEventArgs e)
{

    string result = System.Convert.ToString(e.Result);

    for (int i = 0; i < e.Result.Count; i++)
    {

        ListBoxItem lbitem = new ListBoxItem();

        lbitem.Name = "lb_" + i;
        lbitem.Content = e.Result[i];

        lbitem.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(ListBoxItem_DoubleClickEvent), true);
            
        listBox1.Items.Add(lbitem);
            
    }
       

This works fine, as I use it when the Child window loads, so the ListBox gets the list from the database, however, when a user selects one of the items in the ListBox, they have the option to edit the selected item. So once the edit is in place, there is an edit button, which updates the column in the table in the database. So then on the button click, I am again calling the aforementioned code to update the ListBox with the new credentials. However, this brings back the error:

"Value does not fall within the expected range."

Why can I not call the Web method on the button click, as all it is doing is refreshing the ListBox?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

3 Answers3

13

This might be due to the fact that you are trying to add a ListBoxItem with the same name to the page.

If you want to refresh the content of the listbox with the newly retrieved values you will have to first manually remove the content of the listbox other wise your loop will try to create lb_1 again and add it to the same list.

Look here for a similar problem that occurred Silverlight: Value does not fall within the expected range exception

TylerH
  • 20,799
  • 66
  • 75
  • 101
Stainedart
  • 1,929
  • 4
  • 32
  • 53
  • So what would you suggest I did? I did have a look at that answer, I think what you said is the problem. – Ebikeneser Apr 23 '12 at 17:21
  • I actually just add a clear function to the listbox items on the button click, this works the first tiem with the options being refreshed, however if I do it again the list box does not refresh, do you know why this is? – Ebikeneser Apr 23 '12 at 17:35
  • After you clear can just for the sake of debugging ensure that the list of items is empty. Maybe you should consider putting the clear within the callback. – Stainedart Apr 23 '12 at 17:43
  • Sorted, put the clear in the callbacks, if you set that as your answer I will mark as correct. Thanks bud. – Ebikeneser Apr 23 '12 at 17:53
2

I had from a totaly different reason the same notice "Value does not fall within the expected range" from the Visual studio 2008 while trying to use the: Tools -> Windows Embedded Silverlight Tools -> Update Silverlight For Windows Embedded Project.

After spending many ohurs I found out that the problem was that there wasn't a resource file and the update tool looks for the .RC file

Therefor the solution is to add to the resource folder a .RC file and than it works perfectly. I hope it will help someone out there

0

In case of WSS 3.0 recently I experienced same issue. It was because of column that was accessed from code was not present in the wss list.

CPW
  • 83
  • 10