freaky
freaky

Reputation: 294

Changing value of list box dynamically when selecting values from combo box

I have a combo box which gets data from a database and a list box which also gets data from a database.

Now what I'd like to do is change the value on the list box depending on the value of the combo box.

For example of if I select “apple” from a combo box, then in the list it should display the types of apples.

In this case let’s say the types of apples are Granny Smith, Red Delicious, and so on. I hope you guys get what I am trying to say. I have not started to code but I need a method that will allow me to get the desired output. And also I am new to asp.net with vb.

Upvotes: 0

Views: 806

Answers (2)

SysDragon
SysDragon

Reputation: 9878

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    ListBox1.Items.Clear()

    Dim com As New SqlClient.SqlCommand("SELECT type FROM Tbl WHERE category = '" & ComboBox1.Text & "'", sqlConn)
    Dim sql As SqlClient.SqlDataReader = com.ExecuteReader()

    While sql.Read
        ListBox1.Items.Add(sql.GetString(0))
    End While
    sql.Close()
End Sub

Upvotes: 1

Ravindra Gullapalli
Ravindra Gullapalli

Reputation: 9188

Use OnSelectedIndexChanged function of the ComboBox. Check http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.onselectedindexchanged.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 and then retrieve the values for the ListBox from the database.

Upvotes: 1

Related Questions