2017-11-11 6 views
0

listbox出力のデータをラベルに表示する手助けが必要です。 listboxにはすべてがロードされていますが(FirstnameLastName参照)、ラベルには何も書き込まれません。常にエラーです。C#とローカルデータベース(アクセス)

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      connection.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connection; 
      string query = "SELECT * FROM Tab1 WHERE groupA='" + listBox1.Text + "' ORDER BY FirstName"; 
      command.CommandText = query; 

      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       listBox3.Items.Add(reader["FirstName"].ToString() + " " + reader["LastName"].ToString()); 
      } 

      connection.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("ERROR" + ex); 
     } 
    } 

 
private void listBox3_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      try 
      { 
       connection.Open(); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connection; 
       string query = "select * from Tab1 where FirstName=" + listBox2.Text + ""; 
       command.CommandText = query;

OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { label6.Text = reader["FirstName"].ToString(); label8.Text = reader["LastName"].ToString(); label10.Text = reader["GroupPolice"].ToString(); } connection.Close(); } catch (Exception ex) { MessageBox.Show("ERROR" + ex); } }

ありがとうlistBox3_SelectedINdexChanged。 enter image description here

+0

'常にerror.'常に私たちにエラーを伝えます。 – LarsTech

+0

SQLインジェクションと書式設定のエラーを避けるためには、パラメータを使用する必要があります。これはおそらく問題を引き起こしています。ラベルには1つの値しか表示されないので、whileループは意味をなさない。代わりに 'if(reader.Read())'を使用してください。 – LarsTech

答えて

0

変更する2番目のクエリ:

string query = "select * from [Tab1] where FirstName='" + listBox2.Text + "'"; 

そしてwhile (reader.Read())へ:

if (reader.Read()) 
関連する問題