0
我需要幫助顯示listbox
輸出到標籤的數據。 在listbox
一切都加載(我需要看到Firstname
和LastName
),但它不會在標籤上寫任何東西 - 總是一個錯誤。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);
}
}
listBox3_SelectedINdexChanged
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);
}
}
謝謝。 enter image description here
'總是error.'總是告訴我們的錯誤。 – LarsTech
你必須使用參數來避免sql注入和格式錯誤,這可能是你的問題。一個標籤只能顯示一個值,所以while循環沒有意義。使用'if(reader.Read())'代替。 – LarsTech