hi! kindly help meeeee :D
Populating the combo box with the list of items from the one of the column in table from database.
Based on the selection of item in the combo box, the another combobox should be populated with value corresponding to the selected item in the first combo box.

Here's my code :

 Imports System.Data.Sql
 Imports System.Data.SqlClient
 Imports System.Data.OleDb


 Public Class Form2
    Sub fillcombo()
    strsql = "select distinct YearLevels from Schedulings"
    Dim acscmd = New OleDb.OleDbCommand
    acscmd.CommandText = strsql
    acscmd.Connection = asconn
    acsdr = acscmd.ExecuteReader()
    While (acsdr.Read())
        ComboBox1.Items.Add(acsdr("YearLevels"))

    End While
    acscmd.Dispose()
    acsdr.Close()
    End Sub


Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    EnlistmentModule.connect()
    Me.fillcombo()


    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    strsql = "select * from Schedulings where YearLevels = '" & ComboBox1.Text & "'"
    Dim acscmd As New OleDb.OleDbCommand
    acscmd.CommandText = strsql
    acscmd.Connection = asconn
    acsdr = acscmd.ExecuteReader()
    If acsdr.Read = True Then
        ComboBox2.Items.Add(acsdr("Sections"))

    End If
    ComboBox2.SelectedIndex = -1
    acscmd.Dispose()
    acsdr.Close()

       End Sub


    End Class

The problem with this code only the first row that is populate in the 2nd combo box.
help me pls. this is urgent. thank you.

Use looping to read all data.

While (acsdr.Read())
ComboBox2.Items.Add(acsdr("Sections"))
End While
commented: True +2

Sure, change if loops with while.

oh thanx! it solves my problem :)

but there's another problem ..
when i first select the items on the combobox1 then the second combobox populated to its corresponding items
then when i tried it again ,the items that corresponds my first selection on combobox1 , combines on the corresponding items to the first try i made.

clear comboBox2 before loading it:
comboBox1.Items.Clear()

@Mitja it works . but it doesnt show the selected item in the combobox.

ah it works. :D

thanx. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.