Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Hi everybody,

I'm developping an application with visual studio 2008 and i'm trying to find the exact code to fill the second combobox on depending on the selected value of the first combobox from my mysql database with two tables activity and groupe.
This is my code:
'fill the combobox1 with table activity
   Sub fillCbbActivitychoice()
       strsql = "select * from activity
       objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
       objdr = objcmd.ExecuteReader
       While (objdr.Read())
           With ComboBox1.Items.Add(objdr("Activity_Name"))
           End With
       End While
       objcmd.Dispose()
       objdr.Close()
   End Sub


'fill the combobox2 with table groupe
    Sub fillCbbGpe()
        strsql = "select * from groupe where Activity_Name=' " & ComboBox1.Text & " '"
        objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
        objdr = objcmd.ExecuteReader
        While (objdr.Read())
            With ComboBox2.Items.Add(objdr("Libelle_Gpe"))
            End With
        End While
        objcmd.Dispose()
        objdr.Close()
    End Sub

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fillCbbActivitychoice()
    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        
        strsql = "select * from groupe where Activity_Name=' " & ComboBox1.Text & " '"
        objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
        'With objcmd
        '.Parameters.AddWithValue("@field3", ComboBox1.Text)
        'End With
        objdr = objcmd.ExecuteReader
        If (objdr.Read()) = True Then
            ComboBox2.Items.Add(objdr("Gpe_Name"))
        End If
        objcmd.Dispose()
        objdr.Close()
    End Sub
Posted
Updated 26-Mar-13 5:33am
v2
Comments
joshrduncan2012 26-Mar-13 11:13am    
You need to be careful with how you declare certain variables. You have a couple of lines that are prone for SQL injection attacks. Also, where are you declaring @field3 parameter? You need to replace combobox1.text in the first line with the parameter name and that should take care of your sql injection prevention.
Sergey Alexandrovich Kryukov 26-Mar-13 11:52am    
True. Parametrized statements should be used.
—SA
Sergey Alexandrovich Kryukov 26-Mar-13 11:53am    
Also, tag your UI library or application type. If the type is ComboBox, give it a fully-qualified name. Do you think there is only one "ComboBox" type? No.
-SA
[no name] 26-Mar-13 12:15pm    
Okay and what exactly is wrong with your code? Are we supposed to guess?
xxdaw 28-Mar-13 2:20am    
The code for the combox1 is Ok, it works correctly.
But when I click on the activity choice, the combobox2 is not filled correctly. All groupe is showned even groups which don't belong to the selected activity.

1 solution

From to the combobox you have a Member value and Display value when you are add some item to the combobox but you can`t use them with
ComboBox2.Items.Add(objdr("Libelle_Gpe"))
so you can use
ComboBox1.DataSource
but don`t forget before than use it you should initial DisplayValue and MemberValue properties.

For Example:

SQL
strsql = "select id,Title from Table1 "

VB
ComboBox1.DisplayMember = "Title"
ComboBox1.ValueMember = "id"
ComboBox1.DataSource = MyDataSource

and you can fill your data source (MyDataSource) with SqlDataAdapter object with fetching data by your query. finally when you want to the bind second ComboBox you should use SelectedValue properties of fist ComboBox for example:
SQL
strsql = "select * from groupe where Activity_Name=' " & ComboBox1.SelectedValue& " '"

Of course if you want use ComboBox.Text you can use it but, you should change something to the from of your database.

Best Regards.
Homay
 
Share this answer
 
v2

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