Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am brand newbbie interested in Vb.Net. I have three cascading comboBoxes , Country, Province and City. I need to have the state and city comboBoxes populated automatically when a selection is made on the country comboBox. The comboBoxes get the data from tables in Ms Access database (related through foreign keys). At the moment the country comboBox is the one populating and nothing on the other two.I Please help.

What I have tried:

I have tried the following code:
mports 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
Posted
Updated 10-Apr-19 3:14am

1 solution

Just put LoadCountry() function like below


VB
If Not Page.IsPostBack Then
    LoadCountry()
           
 End If


as well as please correct the parameter name of command query

C#
CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"


to

C#
CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?CountryID"
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900