Cascading combo box not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bittsman
    New Member
    • Apr 2019
    • 1

    Cascading combo box not working

    i am a newbbie am trying to have 3 cascading comboboxes for Country, State and City all contained in a 3 tables in Ms Access Database and related via foreign keys. When the country combobox is has a selected the other two will show the relevant state and cities for that country selected. in the meantime the first combobox is the one populating the countries and nothing for the State and cities. please help
    the code is as follows:

    Code:
    Imports System.Data
    Imports System.Configuration
    Imports System.Data.OleDb
    
    Public Class Form1
        Dim strConn As String = "Provider= Microsoft.Ace.Oledb.12.0; Data source =" & Environment.CurrentDirectory & "\Practice_Country_combo1.accdb"
        Dim cn As New OleDbConnection(strConn)
        Dim Cmd As OleDbCommand
        Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            LoadCountry()
        End Sub
        Private Sub LoadCountry()
            Using cn As New OleDbConnection(strConn)
                Using cmd As New OleDbCommand()
                    Try
                        With cmd
                            .Connection = cn
                            .CommandType = CommandType.Text
                            .CommandText = "SELECT CountryID, Country FROM Country"
                        End With
                        Dim ds As New DataSet()
                        Dim da As New OleDbDataAdapter()
                        da.SelectCommand = cmd
                        cn.Open()
                        da.Fill(ds)
                        cn.Close()
                        ComboBox1.ValueMember = "CountryID"
                        ComboBox1.DisplayMember = "Country"
                        ComboBox1.DataSource = ds.Tables(0)
                    Catch ex As Exception
    
                    End Try
                End Using
            End Using
        End Sub
    
        Private Sub LoadProvince(ByVal countryID As Integer)
            Using cn As New OleDbConnection(strConn)
                Using Cmd As New OleDbCommand()
                    Try
                        With Cmd
                            .Connection = cn
                            .CommandType = CommandType.Text
                            .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"
                        End With
                        Cmd.Parameters.AddWithValue("?CountryID", OleDbType.Integer)
                        Dim ds As New DataSet()
                        Dim da As New OleDbDataAdapter()
                        da.SelectCommand = Cmd
                        cn.Open()
                        da.Fill(ds)
                        cn.Close()
                        If ds.Tables(0).Rows.Count > 0 Then
                            ComboBox2.ValueMember = "ProvinceID"
                            ComboBox2.DisplayMember = "Province"
                            ComboBox2.DataSource = ds.Tables(0)
                        End If
                    Catch ex As Exception
    
                    End Try
                End Using
            End Using
        End Sub
    
        Private Sub LoadCity(ByVal ProvinceID As Integer)
            Using cn As New OleDbConnection(strConn)
                Using cmd As New OleDbCommand()
                    Try
                        With cmd
                            .Connection = cn
                            .CommandType = CommandType.Text
                            .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =?"
                        End With
                        cmd.Parameters.AddWithValue("?ProvinceID", OleDbType.Integer)
                        Dim ds As New DataSet()
                        Dim da As New OleDbDataAdapter()
                        da.SelectCommand = cmd
                        cn.Open()
                        da.Fill(ds)
                        cn.Close()
                        If ds.Tables(0).Rows.Count > 0 Then
                            ComboBox3.DataSource = ds.Tables(0)
                            ComboBox3.DisplayMember = "City"
                            ComboBox3.ValueMember = "CityID"
                        End If
                    Catch ex As Exception
    
                    End Try
                End Using
            End Using
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            If ComboBox1.SelectedValue.ToString() <> "" Then
                Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
                LoadProvince(CountryID)
                ComboBox3.SelectedIndex = 0
            End If
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
            LoadCity(ProvinceID)
        End Sub
    
    
    
    End Class
    Last edited by gits; Apr 11 '19, 02:03 PM. Reason: added code tags
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    In Subroutine LoadProvince()

    Code:
    With Cmd
      .Connection = cn
      .CommandType = CommandType.Text
      .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =" & comboBox1.SelectedValue.ToString
    End With
    And
    In SUbroutine LoadCity()

    Code:
    With cmd
      .Connection = cn
      .CommandType = CommandType.Text
      .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =" & comboBox2.SelectedValue.ToString
    End With
    Last edited by gits; Dec 16 '19, 06:58 AM. Reason: added code tags

    Comment

    Working...