2016-11-25 68 views
0

這是我用C#編寫的簡單代碼,當我更改索引時,subject_name被添加到沒有數據庫值的組合框中。如何將選定的數據庫列添加到C#組合框中?

private void gradebox_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(conString); 
    con.Open(); 

    if (con.State == System.Data.ConnectionState.Open) 
    { 
     String num = gradebox.SelectedItem.ToString(); 
     string q = "SELECT subject_name FROM subjects WHERE grade = '"+num+"'"; 
     Console.Write(gradebox.Text.ToString()); 
     // SqlDataReader dr = q.ExecuteReader(); 
     SqlCommand cmd = new SqlCommand(q, con); 

     SqlDataReader dr = cmd.ExecuteReader(); 

     while (dr.Read()) 
     { 
      subbox.Items.Add("subject_name"); 
     } 
    } 
} 
+0

你只添加一個字符串'subject_name'組合框。從填充的「博士」閱讀缺失 – MarkusEgle

回答

相關問題