Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB
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 26-Mar-13 6:08am
xxdaw260
Edited 26-Mar-13 6:33am
v2
Comments
joshrduncan2012 at 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 at 26-Mar-13 11:52am
   
True. Parametrized statements should be used.
—SA
Sergey Alexandrovich Kryukov at 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
ThePhantomUpvoter at 26-Mar-13 12:15pm
   
Okay and what exactly is wrong with your code? Are we supposed to guess?
xxdaw at 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.
AYDIN EBRAHIMI HOMAY at 6-Apr-13 23:56pm
   
Dear xxdaw
 
First check your table or view from the database with your generated query when you select each items from the combobox1. for example if your combobox have these items('A','B','C') when you select first item from the combobox1, you should have a query same as: "select * from Table1 where title='A'" ;) and if you get result with this query form your DBMS (Sql Server, Access or ...) combobox2 binding.
 
Best Regards.
Homay

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
strsql = "select id,Title from Table1 "
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:
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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100