我有兩種方法,一種將數值存儲到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