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:
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
Comment