By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Cascading combo box not working

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Configuration
  3. Imports System.Data.OleDb
  4.  
  5. Public Class Form1
  6.     Dim strConn As String = "Provider= Microsoft.Ace.Oledb.12.0; Data source =" & Environment.CurrentDirectory & "\Practice_Country_combo1.accdb"
  7.     Dim cn As New OleDbConnection(strConn)
  8.     Dim Cmd As OleDbCommand
  9.     Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.         LoadCountry()
  11.     End Sub
  12.     Private Sub LoadCountry()
  13.         Using cn As New OleDbConnection(strConn)
  14.             Using cmd As New OleDbCommand()
  15.                 Try
  16.                     With cmd
  17.                         .Connection = cn
  18.                         .CommandType = CommandType.Text
  19.                         .CommandText = "SELECT CountryID, Country FROM Country"
  20.                     End With
  21.                     Dim ds As New DataSet()
  22.                     Dim da As New OleDbDataAdapter()
  23.                     da.SelectCommand = cmd
  24.                     cn.Open()
  25.                     da.Fill(ds)
  26.                     cn.Close()
  27.                     ComboBox1.ValueMember = "CountryID"
  28.                     ComboBox1.DisplayMember = "Country"
  29.                     ComboBox1.DataSource = ds.Tables(0)
  30.                 Catch ex As Exception
  31.  
  32.                 End Try
  33.             End Using
  34.         End Using
  35.     End Sub
  36.  
  37.     Private Sub LoadProvince(ByVal countryID As Integer)
  38.         Using cn As New OleDbConnection(strConn)
  39.             Using Cmd As New OleDbCommand()
  40.                 Try
  41.                     With Cmd
  42.                         .Connection = cn
  43.                         .CommandType = CommandType.Text
  44.                         .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"
  45.                     End With
  46.                     Cmd.Parameters.AddWithValue("?CountryID", OleDbType.Integer)
  47.                     Dim ds As New DataSet()
  48.                     Dim da As New OleDbDataAdapter()
  49.                     da.SelectCommand = Cmd
  50.                     cn.Open()
  51.                     da.Fill(ds)
  52.                     cn.Close()
  53.                     If ds.Tables(0).Rows.Count > 0 Then
  54.                         ComboBox2.ValueMember = "ProvinceID"
  55.                         ComboBox2.DisplayMember = "Province"
  56.                         ComboBox2.DataSource = ds.Tables(0)
  57.                     End If
  58.                 Catch ex As Exception
  59.  
  60.                 End Try
  61.             End Using
  62.         End Using
  63.     End Sub
  64.  
  65.     Private Sub LoadCity(ByVal ProvinceID As Integer)
  66.         Using cn As New OleDbConnection(strConn)
  67.             Using cmd As New OleDbCommand()
  68.                 Try
  69.                     With cmd
  70.                         .Connection = cn
  71.                         .CommandType = CommandType.Text
  72.                         .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =?"
  73.                     End With
  74.                     cmd.Parameters.AddWithValue("?ProvinceID", OleDbType.Integer)
  75.                     Dim ds As New DataSet()
  76.                     Dim da As New OleDbDataAdapter()
  77.                     da.SelectCommand = cmd
  78.                     cn.Open()
  79.                     da.Fill(ds)
  80.                     cn.Close()
  81.                     If ds.Tables(0).Rows.Count > 0 Then
  82.                         ComboBox3.DataSource = ds.Tables(0)
  83.                         ComboBox3.DisplayMember = "City"
  84.                         ComboBox3.ValueMember = "CityID"
  85.                     End If
  86.                 Catch ex As Exception
  87.  
  88.                 End Try
  89.             End Using
  90.         End Using
  91.     End Sub
  92.  
  93.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  94.         If ComboBox1.SelectedValue.ToString() <> "" Then
  95.             Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
  96.             LoadProvince(CountryID)
  97.             ComboBox3.SelectedIndex = 0
  98.         End If
  99.     End Sub
  100.  
  101.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  102.         Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
  103.         LoadCity(ProvinceID)
  104.     End Sub
  105.  
  106.  
  107.  
  108. End Class
Apr 10 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.