[RESOLVED] Fill combobox depending on value of other combo
Hello,
I have 2 comboboxes.
When I choose a country in cboLand I want only the states to show in cboStaat that are associated with the country from cboLand.
I tried the code below but I get all different error messages.
Perhaps you can see something in my code that I missed.
Code:
Public Sub SelectLand()
Dim adapter As New OleDbDataAdapter("SELECT Land_ID,Land FROM tblLAND", connection)
Dim table As New DataTable
adapter.Fill(table)
With Me.cboLand
.DataSource = table
.DisplayMember = "Land"
.ValueMember = "Land_ID"
.SelectedIndex = 0
End With
End Sub
Public Sub SelectStaat()
Dim adapter As New OleDbDataAdapter("SELECT Staat_ID,Staat FROM tblSTAAT WHERE Land_ID = " & Me.cboLand.SelectedItem, connection)
Dim table As New DataTable
adapter.Fill(table)
With Me.cboStaat
.DataSource = table
.DisplayMember = "Staat"
.ValueMember = "Staat_ID"
.SelectedIndex = 0
End With
End Sub
Thanks in advance
Re: Fill combobox depending on value of other combo
On which line is the error?
I think in the select statement the "Me.cboLand.SelectedItem" is wrong. it should be "CInt(Me.cboLand.SelectedValue)" if Land_ID type is integer.
Re: Fill combobox depending on value of other combo
Quote:
Originally Posted by
VBDT
On which line is the error?
I think in the select statement the "Me.cboLand.SelectedItem" is wrong. it should be "CInt(Me.cboLand.SelectedValue)" if Land_ID type is integer.
Thanks for the quick reply.
When I use CInt(Me.cboLand.SelectedValue), the error is on line
Dim adapter As New OleDbDataAdapter("SELECT Staat_ID,Staat FROM tblSTAAT WHERE Land_ID = " & Me.cboLand.SelectedItem, connection)
The error reads: Conversion of type DataRowView to type integer is not valid.
Re: Fill combobox depending on value of other combo
I solved it using this code:
Code:
Public Sub SelectLand()
Dim adapter As New OleDbDataAdapter("SELECT Land_ID,Land FROM tblLAND", connection)
Dim table As New DataTable
adapter.Fill(table)
With Me.cboLand
.DataSource = table
.DisplayMember = "Land"
.ValueMember = "Land_ID"
.SelectedIndex = 1
End With
End Sub
Public Sub SelectStaat()
Dim cmd As New OleDbCommand("SELECT Staat_ID,Land_ID,Staat FROM tblSTAAT WHERE Land_ID =?", connection)
cmd.Parameters.Add(New OleDbParameter("Land_ID", OleDbType.VarChar))
cmd.Parameters("Land_ID").Value = cboLand.SelectedValue.ToString
Dim adapter As New OleDbDataAdapter(cmd)
Dim table As New DataTable
adapter.Fill(table)
With Me.cboStaat
.DataSource = table
.DisplayMember = "Staat"
.ValueMember = "Staat_ID"
'.SelectedIndex = 1
End With
End Sub
Private Sub cboLand_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLand.SelectedIndexChanged
Me.txttest.Text = Me.cboLand.SelectedValue.ToString
Call SelectStaat()
End Sub
End Class