Private Sub caldistrict()
Dim conn As New SqlConnection
Dim strcon As String = "Data Source=KAN-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.ConnectionString = strcon
conn.Open()
If cbprovince.SelectedIndex <> 0 Then
'If cbprovince.ValueMember = "" Then
Dim sql As String = "SELECT * FROM district where id_province = '" + cbprovince.SelectedValue + "' "
Dim da As New SqlDataAdapter(sql, conn)
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
'cbdistrict.Items.Clear()
Try
With cbdistrict
.DataSource = dt
.DisplayMember = dt.Columns.Item("district").ColumnName
'คือตัวที่ใช้แสดงใน Combobox
.ValueMember = dt.Columns.Item("id_district").ColumnName 'Code หรือรหัสที่เราสร้างขึ้นมาเช่น Primary Key
End With
Catch ex As Exception
cbdistrict.Text = Nothing
End Try
End If
'End If
End Sub
Private Sub calsubdistrict()
Dim conn As New SqlConnection
Dim strcon As String = "Data Source=KAN-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.ConnectionString = strcon
conn.Open()
If cbdistrict.SelectedIndex > 0 Then
If cbdistrict.ValueMember = "" Then
Dim sql As String = "SELECT * FROM subdistrict where id_district = '" + cbdistrict.SelectedValue + "' "
Dim da As New SqlDataAdapter(sql, conn)
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
Try
With cbdistrict
.DataSource = dt
.DisplayMember = dt.Columns.Item("subdistrict").ColumnName
'คือตัวที่ใช้แสดงใน Combobox
.ValueMember = dt.Columns.Item("id_subdistrict").ColumnName 'Code หรือรหัสที่เราสร้างขึ้นมาเช่น Primary Key
End With
Catch ex As Exception
cbsubdistrict.Text = Nothing
End Try
End If
End If
End Sub
Private Sub cbprovince_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbprovince.SelectedIndexChanged
caldistrict()
End Sub
Private Sub cbdistrict_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbdistrict.SelectedIndexChanged
calsubdistrict()
End Sub