Combos Dinámicos con Visual Basic.Net 2010 y SQL

Para empezar a crear nuesta aplicación de Combos Dinámicos, debemos crear un proyecto en Visual Basic 2010 con el nombre: Combos_Dinamicos(Sugerencia).
Después diseñar el siguiente formulario: 

 


Al combobox Departamento le asignamos el siguiente nombre: cbodepartamento.
Al combobox Provincia le asignamos el siguiente nombre: cboprovincia.
Al combobox Distrito le asignamos el siguiente nombre: cbodistrito.

Después agregamos una clase a nuestro proyecto con el nombre: cls_ubigeo y colocamos el siguiente
código:

Imports System.Data
Imports System.Data.SqlClient
Public Class cls_ubigeo
    Shared cn As New SqlConnection("server=.;database=ubigeo;integrated security=true")

    Shared Function listarDeptamento() As DataTable
        Dim tabla As New DataTable
        Try
            Dim da As New SqlDataAdapter("select IDDEP,NOMDEP from DEPARTAMENTO", cn)
            da.SelectCommand.CommandType = CommandType.Text
            da.Fill(tabla)
        Catch ex As SqlException
            MsgBox(ex.Message, "Ubigeo")
        End Try
        Return tabla
    End Function
    Shared Function listarProvincia(ByVal departamento As String) As DataTable
        Dim tabla As New DataTable
        Try
            Dim da As New SqlDataAdapter("select IDPRO,NOMPRO from PROVINCIA P, " &
                                         "DEPARTAMENTO D WHERE P.IDDEP=D.IDDEP AND D.IDDEP=@dep", cn)
            da.SelectCommand.Parameters.AddWithValue("@dep", departamento)
            da.SelectCommand.CommandType = CommandType.Text
            da.Fill(tabla)
        Catch ex As SqlException
            MsgBox(ex.Message, "Ubigeo")
        End Try
        Return tabla
    End Function

    Shared Function listarDistrito(ByVal provincia As String) As DataTable
        Dim tabla As New DataTable
        Try
            Dim da As New SqlDataAdapter("select IDDIS,NOMDIS from DISTRITO D, PROVINCIA P WHERE " &"D.IDPRO=P.IDPRO AND P.IDPRO=@pro", cn)
            da.SelectCommand.Parameters.AddWithValue("@pro", provincia)
            da.SelectCommand.CommandType = CommandType.Text
            da.Fill(tabla)
        Catch ex As SqlException
            MsgBox(ex.Message, "Ubigeo")
        End Try
        Return tabla
    End Function
End Class


No olvidar que para cargar dinámicamente los combobox debemos relacionar las tablas departamento, provincia y distrito, dichas consultas ya estan en las funciones de la clase cls_ubigeo.

En el evento load del formulario escribimos el siguiente código para listar los departamento:








En el evento SelectedValueChanged y SelectedIndexChanged del combobox cbodepartemento escribimos el siguiente código para cargar las provincias de acuerdo al departamento:



 
Y finalmente el evento SelectedIndexChanged del combobox cboprovincia escribimos el siguiente código para listar los distritos de acuerdo a las provincias:



  Descargar Código Fuente (Base Datos)
Descargar