Results 1 to 5 of 5

Thread: populate combobox using a loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    populate combobox using a loop

    I am trying to use a loop to populate a combobox. I have a list of counties in a table. Can any help me with my coding on how I would do that? Thanks!

    This is what I have so far:
    VB Code:
    1. Dim dsCounty As New DataSet()
    2.             Dim sqlConn1 As New SqlConnection(strConnectionString)
    3.             Dim daCounty As New SqlClient.SqlDataAdapter("SELECT *  FROM tblAlabama", sqlConn1)
    4.             dsCounty.Clear()
    5.             daCounty.Fill(dsCounty, "tblAlabama")
    6.             Dim count As Integer = dsCounty.Tables("tblAlabama").Rows.Count
    7.             Dim dr1 As DataRow
    8.             If dsCounty.Tables("tblAlabama").Rows.Count = 0 Then
    9.                 cbPropCountyDocs.Text = "None Available"
    10.                 Exit Sub
    11.             End If
    12.             dr1 = dsCounty.Tables("tblAlabama").Rows(0)
    13.             cbPropCountyDocs.Text = "Select A County"
    14.  
    15.             Dim index As Integer
    16.             For index = 0 To .Rows(count - 1)
    17.                 cbPropCountyDocs.Items.Add(dr1("County"))
    18.                 count += 1
    19.             Next

    Brenda

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    I have something like this now, but is still doesn't work:

    VB Code:
    1. Dim index As Integer
    2.             With dsCounty.Tables("tblCapRec")
    3.                 For index = 0 To count - 1
    4.                     dr1 = .Rows(count - 1)
    5.                     cbPropCountyDocs.Items.Add(dr1("County"))
    6.                     count += 1
    7.                 Next
    8.             End With

    Why?

    Brenda

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    The easiest way is with a datareader. Couple examples:

    Specific example:
    VB Code:
    1. Private Sub AddProjects()
    2.         Dim i As Integer
    3.         Dim rdr As OleDbDataReader
    4.  
    5.         'Declare and initialize data connection properties
    6.         Dim cn As New OleDbConnection(mstrCn)
    7.         Dim strSQL As String = "SELECT Projects.Title FROM(Projects) ORDER BY Projects.Title;"
    8.         Dim cmd As New OleDbCommand(strSQL, cn)
    9.  
    10.         Try
    11.             'Open connection to database and execute query
    12.             cn.Open()
    13.             rdr = cmd.ExecuteReader
    14.  
    15.             'Loop through results and fill Projects combobox
    16.             Do Until rdr.Read = False
    17.                 For i = 0 To rdr.FieldCount - 1
    18.                     cboProject.Items.Add(rdr.GetValue(i))
    19.                 Next
    20.             Loop
    21.  
    22.        Catch ex As Exception
    23.             MsgBox(ex.Message & vbCrLf & ex.StackTrace & vbCrLf & ex.Source, , "Error Reading Projects")
    24.  
    25.             'Cleanup
    26.         Finally
    27.                 If Not rdr.IsClosed Then
    28.                     rdr.Close()
    29.                 End If
    30.                 If cn.State = ConnectionState.Open Then
    31.                     cn.Close()
    32.                 End If        
    33.         End Try
    34.     End Sub
    Generic example:
    VB Code:
    1. 'Fill comboboxes.
    2.     'Parameters:
    3.     '   strSQL  : SELECT statement for combobox values
    4.     '   cbo     : specific combobox
    5.     '   strItem : value to display in combobox
    6.     Public Shared Sub FillCbo(ByVal strSQL As String, ByVal cbo As ComboBox, ByVal strItem As String)
    7.         Dim strCloseMsg As String
    8.  
    9.         Try
    10.             cmd.CommandText = strSQL
    11.  
    12.             cn.Open()
    13.             dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    14.             cbo.Items.Clear()
    15.  
    16.             Do While dr.Read
    17.                 cbo.Items.Add(dr(strItem))
    18.             Loop
    19.             dr.Close()
    20.  
    21.         Catch ex As Exception
    22.            
    23.             MessageBox.Show("FillCbo" & Environment.NewLine & ex.Message & strCloseMsg, "Error Finding Data", MessageBoxButtons.OK, MessageBoxIcon.Error)
    24.                    Finally
    25.             If cn.State = ConnectionState.Open Then
    26.                 cn.Close()
    27.             End If
    28.         End Try
    29.     End Sub

  4. #4
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Hi Brenda, what about this:
    VB Code:
    1. Dim row As DataRow
    2.  
    3. For Each row in dsCounty.Tables("tblCapRec").Rows
    4.     cbPropCountyDocs.Items.Add(CStr(row("County")))
    5. Next
    Alternatively, you can bind the dataset to the combobox, such as:
    VB Code:
    1. cbPropCountyDocs.DataSource = dsCounty.Tables("tblCapRec")
    2. cbPropCountyDocs.DisplayMember = "County"

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Thanks robymix! That works perfect!

    Brenda

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width