2014-03-01 80 views
-4
Private Sub frmemployedetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Try 

     Dim dr As OleDbDataReader 
     opendb() 

     cmd.Connection = con 
     cmd.CommandText = "Select DISTINCT department from employedetails" 

     'Execte reader function is used to hold more than one value from the table 
     dr = cmd.ExecuteReader() 


     ' Fill a combo box with the datareader 
     Do While dr.Read = True 
      cbodepartment = dr.Item(0) 
     Loop 

     con.Close() 

    Catch ex As Exception 
     MsgBox(ex.Message) 

    End Try 
    btnupdate.Enabled = False 
    fill_grid() 
    cbogender.Items.Add("Male") 
    cbogender.Items.Add("Female") 
End Sub 

保存在這個是我的代碼即時通訊新進入VB.net plz幫助我 ,這樣我可以添加到項目組合框前面輸入 而窗體加載它會顯示所有的不同的項目我進入vb.net組合框項目必須在Formload

+0

你得到任何錯誤。如果是的話,他們是什麼? –

+0

Bro讓我告訴你,我想從我的數據庫記錄中填充組合框中的數據字段,我該怎麼做? – user3367558

+0

你試圖直接將你的dr.Item添加到你的cbodepartment對象而不是它的items集合中。如果您在64位窗口中運行,Form Load事件中的任何錯誤都將被吞下並且不顯示。所以嘗試cboDepartment.Items.Add(dr.Item(0)) –

回答

1

如果你想將包含數據庫employeedetails表,你可以試試這個部門的列表來填充你的ComboBox

Dim Connection As OleDb.OleDbConnection 
Connection = New OleDb.OleDbConnection("YourConnectionString") 

     Dim SQL As String = "SELECT DISTINCT department FROM employedetails" 

     Try 
      Connection.Open() 

      Dim DA As New OleDb.OleDbDataAdapter(SQL, Connection) 
      Dim DS As New DataSet("DS") 
      DA.Fill(DS) 

      Dim DT As DataTable 
      DT = DS.Tables(0) 

      For Each DR As DataRow In DT.Rows  
       cbodepartment.Items.Add(DR.Item("department").ToString) 
      Next 

      Connection.Close() 

     Catch ex As Exception 

      MsgBox(ex.Message) 
      Connection.Close() 

     End Try 
+0

Im在組合框中只獲取一條記錄我希望所有帶有不同的記錄 – user3367558

+0

這沒有任何意義。 SELECT語句從表中檢索所有不同的部門。你確定你的employeedetails表中有多個部門嗎? – equisde

+0

Yah Bro,它顯示我只有一條記錄是「IT」,這在我的employedetails表中是不可用的我有超過10個條目與5個不同的部門 – user3367558