以下に示す SQL クエリを実行しようとすると、IndexOutOfRangeException が発生します。理由がわかりません。他の SO ページでは、存在しないフィールドからデータを取得しようとしたことが原因である可能性があると書かれていますが、存在することは確かであり、要求されたフィールドの両方を「 ADRES" と "TAAL" から "LEV" へのリクエストは、上の 2 つだけが拒否されますが、"LEV" の一番上のリクエストは機能していました。「ADRES」は 8 桁の varchar フィールドで、「TAAL」は 1 桁の varchar フィールドです。
try
{
//BESTEL,[PLAN],LEV,ADRES,TAAL
SqlCommand getlist = new SqlCommand("select * from BESW where BEST=@best", Connectie.connMEVO);
getlist.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist.ExecuteReader();
while (DRorder.Read())
{
dateTimePicker1.Value = Convert.ToDateTime(DRorder["BESTEL"]);
dateTimePicker2.Value = Convert.ToDateTime(DRorder["PLAN"]);
comboBox1.Text = DRorder["LEV"].ToString();
comboBox2.Text = DRorder["ADRES"].ToString();
textBox8.Text = DRorder["TAAL"].ToString();
}
}
catch (Exception er) { MessageBox.Show("" + er); }
編集:以下に示すようにクエリを分割すると機能するようですが、その理由が本当にわかりません。
try
{
//BESTEL,[PLAN],LEV,ADRES,TAAL
SqlCommand getlist = new SqlCommand("select BESTEL,[PLAN],ADRES from BESW where BEST=@best", Connectie.connMEVO);
getlist.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist.ExecuteReader();
while (DRorder.Read())
{
dateTimePicker1.Value = Convert.ToDateTime(DRorder["BESTEL"]);
dateTimePicker2.Value = Convert.ToDateTime(DRorder["PLAN"]);
comboBox2.Text = DRorder["ADRES"].ToString();
}
SqlCommand getlist2 = new SqlCommand("select LEV from BESW where BEST=@best", Connectie.connMEVO);
getlist2.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist2.ExecuteReader();
while (DRorder.Read())
{
comboBox1.Text = DRorder["LEV"].ToString();
}
SqlCommand getlist3 = new SqlCommand("select TAAL from BESW where BEST=@best", Connectie.connMEVO);
getlist3.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist3.ExecuteReader();
while (DRorder.Read())
{
textBox8.Text = DRorder["TAAL"].ToString();
}
}
catch (Exception er) { MessageBox.Show("" + er); }