C# 值不在预期范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10284992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value does not fall within the expected range
提问by Ebikeneser
I am using the following code to update a listbox, this recieving a list from a Web service:
我使用以下代码更新列表框,这从 Web 服务接收列表:
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 ListBoxgets 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 ListBoxwith the new credentials. However, this brings back the error -
这工作正常,因为我在加载子窗口时使用它,因此ListBox从数据库中获取列表,但是,当用户选择 中的项目之一时ListBox,他们可以选择编辑所选项目。所以一旦编辑到位,就会有一个编辑按钮,它会更新数据库表中的列。然后单击按钮,我再次调用上述代码以ListBox使用新凭据更新。然而,这又带来了错误——
"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???
为什么我不能在单击按钮时调用 Web 方法,因为它所做的只是刷新ListBox???
采纳答案by Stainedart
This might be due to the fact that you are trying to add a ListBoxItem with a same name to the page.
这可能是因为您尝试向页面添加同名的 ListBoxItem。
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.
如果您想用新检索的值刷新列表框的内容,您必须首先手动删除列表框的内容,否则您的循环将尝试再次创建 lb_1 并将其添加到同一列表中。
Look at here for a similar problem that occured Silverlight: Value does not fall within the expected range exception
在这里查看Silverlight发生的类似问题:值不在预期范围内异常
Cheers,
干杯,
回答by Chaim Gluck
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.
在尝试使用以下工具时,我从 Visual Studio 2008 收到了完全不同的相同通知“值不在预期范围内”:工具 -> Windows Embedded Silverlight 工具 -> 为 Windows Embedded 项目更新 Silverlight。
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
花了很多时间后,我发现问题是没有资源文件,更新工具会查找 .RC 文件
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
因此,解决方案是将 .RC 文件添加到资源文件夹中,然后它可以完美运行。我希望它会帮助那里的人
回答by CPW
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.
对于 WSS 3.0 最近我遇到了同样的问题。这是因为从代码访问的列不存在于 wss 列表中。

