2012-08-10 188 views
0

我想在選擇無線電選項後在組合框內的數據庫中顯示項目。當我嘗試這個時候,組合框中沒有顯示任何內容。請幫忙使用數據填充組合框

private void chkDetailsButton_Click(object sender, EventArgs e) 
{ 
    if (radioButtonA.Checked) 
    { 
     OleDbConnection connect = db.dbConnect(); 

     try 
     { 
      connect.Open(); 
      MessageBox.Show("Opened"); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 

      command.CommandText = "Select * from Categories"; 
      DataTable dt = new DataTable(); 

      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       cmbDisplay.Items.Add(dt.Rows[i]["SeatNo"]); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception in Database" + ex); 
     } 
     finally 
     { 
      connect.Close(); 
     } 
    } 
} 
+2

你嘗試調試代碼? – 2012-08-10 09:51:37

+0

是的,我做了,它連接到數據庫,但不顯示SeatNo列中的項目 – 2012-08-10 09:52:52

+0

這是WinForms還是ASP.Net?如果你可以連接,那dt.Rows.Count是什麼?您似乎沒有將任何數據放入數據表中! – dash 2012-08-10 09:53:48

回答

3

有缺少填充DataTable dt與數據,這是由您的SQL命令返回。

+0

我想從訪問數據庫中獲取數據 – 2012-08-10 09:59:04

4

你try塊應如下:

try 
    { 
     connect.Open(); 
     MessageBox.Show("Opened"); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connect; 

     command.CommandText = "Select * from Categories"; 
     DataTable dt = new DataTable(); 

     //Put some data in the datatable!! 
     using(OleDbDataReader reader = command.ExecuteReader()) 
     { 
      dt.Load(reader); 
     } 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      cmbDisplay.Items.Add(dt.Rows[i]["SeatNo"]); 
     } 
    } 

您需要填寫數據的DataTable中!

你也可以考慮以下幾點:

 using(OleDbDataReader reader = command.ExecuteReader()) 
     { 
      while(reader.Read()) 
      { 
       cmbDisplay.Items.Add(reader.GetValue(reader.GetOrdinal("SeatNo")); 
      } 
     } 

這樣,你甚至不需要使用數據表;對於大量數據來說,這是一種更高效的方法。

順便說一句,你不妨考慮使用Using

using(OleDbConnection connect = db.dbConnect()) 
    { 
     try 
     { 
      connect.Open(); 
      //MessageBox.Show("Opened"); 
      using(OleDbCommand command = new OleDbCommand()) 
      { 
       command.Connection = connect; 
       command.CommandText = "SELECT * FROM Categories"; 
       using(IDataReader reader = command.ExecuteReader()) 
       { 
        while(reader.Read()) 
        { 
          cmbDisplay.Items.Add(reader.GetValue(reader.GetOrdinal("SeatNo")); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("An erorr occured:" + ex); 
     }   
    } 

這將確保您的連接,指揮和讀者對象設置。如果您打算保留連接的一個實例,這是不合適的,但是當您的代碼離開using語句時它將被關閉並處置。

+0

你是最好的男人。有效!。謝謝 – 2012-08-10 10:04:53

0

我不能相信這一點。 :D

你什麼時候準備用數據從數據庫填充DataTable? 沒有什麼在

之間

DataTable dt = new DataTable();

for (int i = 0; i < dt.Rows.Count; i++)

+0

當你在那裏時,你可能會提到SQL甚至沒有被執行! – 2012-08-10 09:57:20

1

使用此代碼嘗試 - 您的樣品不要將您的表數據綁定,創建表的新實例英寸

$ private void chkDetailsButton_Click(object sender, EventArgs e) 
{ 
if (radioButtonA.Checked) 
     { 
      OleDbConnection connect = db.dbConnect(); 

      try 
      { 
       connect.Open(); 
       MessageBox.Show("Opened"); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connect; 

       command.CommandText = "Select * from Categories"; 
       OleDbDataReader myReader = command.ExecuteReader(); 
       while (myReader.Read()) 
       { 
        cmbDisplay.Items.Add(myReader["SeatNo"]); 
       } 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Exception in Database" + ex); 
      } 
      finally 
      { 
       connect.Close(); 
      } 
     } 
    } 
0

您可以填寫與組合框數據表:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"); 
      SqlDataAdapter da = new SqlDataAdapter("select * from Books",con); 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 
      comboBox1.DisplayMember = "Name"; 
      comboBox1.ValueMember = "ID"; 
      comboBox1.DataSource = dt; 
0

在數據集連接嘗試之前,你不灌裝數據這個

 if(radio.checked) 
    { 
     try 
     { 
     connect.Open(); 
     MessageBox.Show("Opened"); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connect; 
     command.CommandText = "Select * from Categories"; 

     OleDbDataAdapter db = new OleDbDataAdapter(); 
     DataSet ds = new DataSet(); 
     db.SelectCommand = command; 
     db.Fill(ds); 
     for(int i=0;i<ds.Tables[0].Rows.Count;i++) 
     { 
      cmbDisplay.Items.Add(ds.Tables[0].Rows[i][0].ToString());  

      } 
     cmDisplay.DataBind(); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception in Database" + ex); 
     } 
     finally 
     { 
      connect.Close(); 
     } 
    }