Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value does not fall within the expected range

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?

like image 792
Ebikeneser Avatar asked Apr 23 '12 17:04

Ebikeneser


2 Answers

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

like image 181
Stainedart Avatar answered Oct 04 '22 05:10

Stainedart


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

like image 45
Chaim Gluck Avatar answered Oct 04 '22 04:10

Chaim Gluck