我有两种方法,一种将数值存储到arraylist中,即Button_1Click()。另一种方法NextClick()从数组列表中检索数据并将其存储到文本框中。 我得到一个数组索引出界失误:ArgumentOutOfRangeException:索引超出范围
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
ArrayList list = new ArrayList();
int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
OleDbDataReader dReader;
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
@"data source=D:\asp\data\test.mdb;";
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM D", conn);
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
String temp = dReader[1].ToString();
list.Add(temp);
temp = dReader[2].ToString();
list.Add(temp);
}
dReader.Close();
conn.Close();
}
protected void next_Click(object sender, EventArgs e)
{
Carname.Text = list[i] as String;
i++;
}
然后,发生了什么呢?在你刚刚从** list [i] **中读取之前,你不认为你应该检查** i **的索引值?像** if(list.Count <= i)返回; ** –
这让我感到困惑,即使设法使用数据库的人也不明白为什么这样简单的东西不起作用。 –
当我使用if语句来检查我是不是list.count,我没有得到错误,但没有得到显示在文本框中。 – ares