2015-09-17 110 views
1

組合框不顯示數據,我填充我的組合框從我的數據庫中的數據是這樣的:組合框不顯示數據

private void PartDefective(string id) 
{ 
    cmd = new SQLiteCommand("Select * FROM Part_defective where testers = '" + id + "'", DBcon); 
    if (DBcon.State == ConnectionState.Closed) 
     DBcon.Open(); 
    myDA = new SQLiteDataAdapter(cmd); 
    myDataSet = new DataSet(); 
    myDA.Fill(myDataSet, "comboBox6"); 
    this.comboBox6.DataSource = myDataSet.Tables["comboBox6"].DefaultView; 
    this.comboBox6.ValueMember = "Part"; 
    this.comboBox6.DisplayMember = "Part"; 
    this.comboBox6.SelectedItem = "ID"; 
    this.comboBox6.SelectedIndex = -1; 

    DBcon.Close(); 
} 

,並顯示出從我所用的數據庫中的數據:

private void comboBox6_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (this.comboBox6.SelectedValue == null) 
    { 
     testerid = "1"; 
    } 
    else 
    { 
     part = this.comboBox6.SelectedText.ToString(); 
    } 
} 
+0

在這一行放置一個斷點:this.comboBox6.DataSource = myDataSet.Tables [「comboBox6」]。DefaultView; – Derek

+0

您已開放** Sql注入**使用參數化查詢 –

回答

0

放在這一行斷點:

this.comboBox6.DataSource = myDataSet.Tables["comboBox6"].DefaultView; 

再看看手錶或quickw myDataSet.Tables [ 「comboBox6」]的價值atch窗口。有沒有行?

而且更改代碼:

string sql = "Select * FROM Part_defective where testers = '" + id + "'"; 
cmd = new SQLiteCommand(sql, DBcon); 

把一個斷點,看看SQL的值。

對數據庫手動運行sql並查看是否有任何結果。

1

絕對沒有理由去定義SelectedItemSelectedIndexSelectedIndexChanged Event對於顯示組合框也是完全多餘的。

刪除這些行,看看它是否解決了你的問題。

this.comboBox6.SelectedItem = "ID"; 
this.comboBox6.SelectedIndex = -1; 

如果ComboBox仍然是空的,問題的根源必須與您的數據讀取和填充數據源。