Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to link combobox2 with acssess database based on selected value from combobox1

What I have tried:

Cmd.Connection = conn 'open connection to database
Cmd.CommandText = "select * from Tabel where filed1= 'ComboBox1.selecteditem'"

        conn.Open()
        dr = Cmd.ExecuteReader
        While dr.Read
            ComboBox2.Items.Add(dr("filed2").ToString())
        End While
        dr.Close()
        conn.Close()
Posted
Updated 27-May-20 20:33pm

1 solution

That won't work, use:
Cmd.CommandText = "select * from Tabel where filed1= '" & ComboBox1.selecteditem & "'"

But doing it this way is dangerous because of the risk of SQL injection, it is better to use a parameterized query.
Read about it here:
How do I parameterized the queries of VB.NET code[^]
And here: Using C# to connect to and query from a SQL database | Sander Rossel[^]
 
Share this answer
 
v3
Comments
katkot_rewsh 28-May-20 6:26am    
Works perfect, Many thanks
katkot_rewsh 28-May-20 13:58pm    
what about making double condition
i think i have also something wrong at syntax

Cmd.CommandText = "select * from Table1 where Filed1= '" & ComboBox1.SelectedItem & "' and Filed2= '" & ComboBox2.SelectedItem & "'"
RickZeeland 28-May-20 15:08pm    
The syntax looks ok to me, but of course I can't see your database so it might be that the datatypes of the Filed1 and Filed2 are different. Maybe you can check in Access and test with a query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900