我正在開發一個應用程序,其中有四個組合框I-e(組合框1爲組合框,組合框2爲組合框,組合框3爲部分和組合框4爲月)和列表框。我已經從數據庫中提取數據到「OnLoad Function」中的所有這些組合框。現在,我想根據組合框在列表框中顯示學生名稱。當我從類(comboBox1)中選擇「class 7th」時。它應該顯示那些處於選定班級(第7班),選定組(在組合框2中選擇)和選定部分(在組合框3中選擇)的學生。這是我的代碼,但它給了我SQL語法異常和indexOutOfRange異常。附帶的數據庫在mysql中。 「dbOperation」是類「myDatabse」的對象,其中我連接了數據庫並實現了所有查詢。根據組合框選擇項填充ListBox的SQL語法錯誤項目
public partial class FeeVoucherPrint : Form
{
myDatabase dbOperation = new myDatabase();
DataTable table = new DataTable();
public FeeVoucherPrint()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
}
private void FeeVoucherPrint_Load(object sender, EventArgs e)
{
table = dbOperation.select("* from class");
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
table = dbOperation.select("* from `group`");
comboBox2.DataSource = table;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "id";
table = dbOperation.select("* from section");
comboBox3.DataSource = table;
comboBox3.DisplayMember = "name";
comboBox3.ValueMember = "id";
table = dbOperation.select("* from months");
comboBox4.DataSource = table;
comboBox4.DisplayMember = "month";
comboBox4.ValueMember = "id";
}
private void fill()
{
try
{
table = dbOperation.select("studentid from studentinclass where classid = " + comboBox1.SelectedValue + " and groupid = " + comboBox2.SelectedValue);
table = dbOperation.select("* from student where studid = " + table.Rows[0]["studentid"]);
listBox1.DataSource = table;
listBox1.DisplayMember = "name";
listBox1.ValueMember = "studid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
fill()
}
}
假設dbOperation返回一個數據表或一個可枚舉的,你應該直接將它賦值給數據源。 – Stig