-1

以下に示す 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); }
4

2 に答える 2

1

考えられるエラーは 2 つあります。2. プロパティをコンボ ボックスに設定しようとしていますが、クエリされたデータが項目として含まれていない可能性があります。

それが役に立てば幸い。

編集:たとえば、コンボボックスには「はい」と「いいえ」の2つの項目があり、「どちらでもない」を現在の項目として設定しようとしている可能性があります。

于 2015-03-13T13:27:04.193 に答える
-2

フィールドを個別のクエリに分割すると、それらは機能するだけで、クエリは変更されず、1 つのクエリにすべての行が含まれていないことがわかります。なぜそうなったのかはまだよくわかりませんが、個別のクエリを使用すると、少なくともその問題を回避できます。

于 2015-03-13T13:51:08.293 に答える