请帮我修复此错误(在列表框中显示) [英] please help me fix this error(displaying in listboxes)
本文介绍了请帮我修复此错误(在列表框中显示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是错误>
**************例外文字**************
System.ArgumentOutOfRangeException:InvalidArgument ='0'的值对'SelectedIndex'无效。
参数名称:SelectedIndex
我的代码>
here is the error>
************** Exception Text **************
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
Parameter name: SelectedIndex
my code>
DataSet st = new DataSet();
string strConnectionString = "Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True";
SqlConnection objconnection = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("SELECT [name] FROM [staff1]",
objconnection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(st);
}
}
var empList = st.Tables[0].AsEnumerable().Select(dataRow =>
dataRow.Field<string>("name")).ToList();
listBox1.SelectedIndex = 0;
listBox1.Update();
我不明白这个问题。它在10分钟前工作但现在却没有?我真的很困惑任何帮助都是apreciated
I dont understand the issue.It worked 10 mins ago but now it doesn't? I am really confused any help would be apreciated
推荐答案
这意味着你试图在列表中选择0项目计数。如果您至少有一个项目,则索引0将有效。只需1)使用调试器; 2)不要通过索引来解决任何事情而不检查计数,这将给出你应该检查的指数0到count - 1
的有效范围。 />
-SA
It means that you are trying to select an item in a list with 0 item count. If you had at least one item, the index 0 would be valid. Just 1) use the debugger; 2) don't address anything by index without checking the count, which would give your the valid range of indices 0 tocount − 1
, which you should check up.
—SA
这篇关于请帮我修复此错误(在列表框中显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文