Results 1 to 3 of 3

Thread: Loading data into a cboBox from DataReader

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106

    Loading data into a cboBox from DataReader

    I am trying to load data into a cboBox but it fails on the do while loop. does anyone have a clue?



    Code:
    Private Function FillComboBoxFromDataReader()
    
            'Purpose: Populate the combo box with countries list, from a DataReader
    
            Dim sqlConn As SqlConnection  'Form-level connection object
            Dim drCountries As SqlDataReader 'Form-level DataReader object
    
            'Create a Connection object
            sqlConn = New SqlConnection(SQL_CONNECTION)
    
            'Load list of countries into the combo box, from the DataReader
            Call ListCountriesInDataReader(sqlConn, drCountries)
    
            'Populate the combo box cboCountries, with the DataReader output
            Do While drCountries.Read()
                'Do something with the row
                cboCountries.Items.Add(drCountries.Item("Country"))
            Loop
    
            'Close DataReader
            drCountries.Close()
    
            'Close the Connection
            sqlConn.Close()
    
        End Function

  2. #2
    egiggey
    Guest
    I havn't used the datareader but something i see that dosn't look right is this

    Do While drCountries.Read()
    'Do something with the row
    cboCountries.Items.Add(drCountries.Item("Country"))
    Loop

    question isn't the drcountries a collection that needs to be referenced by an index and if so you would need to do a loop like this


    dim maxrecords as integer

    do until maxrecords = drcountries.items.count -1
    cbocountries.items.add(drcountries.items(maxrecords)
    maxrecords = maxrecords + 1
    loop

    if i am way off I apologize as like I said I have never used the datareader
    if i'm close let me know i'm on the right track
    Good luck

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    I figured it out. My code was correct. for some reason the cbobox control was not referenced in the form.


    Thanks.

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