简体   繁体   中英

fill data to combo box depending upon another combo box value

i am connecting an Access database to my program using VB and i have two combo, i want the second combo data source is depending on the first combo selected item, here's the code that i made so far:

 Try
     Dim dbcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\inventorysys.accdb;Persist Security Info=False;")
        dbcon.Open()
        Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = ' " & catogerycombotxt.SelectedItem & " ' ")
        Dim comm As New OleDb.OleDbCommand(sqlquery, dbcon)
        Dim rs As OleDb.OleDbDataReader = comm.ExecuteReader
        Dim dt As DataTable = New DataTable
        dt.Load(rs)
        ' as an example set the ValueMember and DisplayMember'
        ' to two columns of the returned table'
        brandcombotxt.DataSource = dt
        brandcombotxt.ValueMember = "Brand"
        brandcombotxt.DisplayMember = "Brand"

        dbcon.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

it works without errors but get no results, hope you can help me in that

Two things:

First, I believe your problem is that in your query you are concatenating the selected value between two spaces:

Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = ' " & catogerycombotxt.SelectedItem & " ' ")

Should be

Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = '" & catogerycombotxt.SelectedItem & "'")

Second: learn about parameterized queries . That's a better way to create and execute sql queries than string concatenation for many, many reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM