Value does not fall within the expected range

2019-02-12 15:13发布

问题:

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 list box 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 one 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 list box with the new credentials. However this brings bcak 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???

回答1:

This might be due to the fact that you are trying to add a ListBoxItem with a 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 at here for a similar problem that occured Silverlight: Value does not fall within the expected range exception

Cheers,



回答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



回答3:

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.