Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all! i have two combo box in my program

one has the subteam for example if you click the 1st combo box, the items would be
A1
A2
A3
A4
A5
what i want to happen is that if i chose A1 only the members of team A1 would appear in my combo box2. and by that, you need an sql statement. but how do i code that? can someone help me?

[Addition from OP - posted as answer - answer deleted]

C#
dim sql as string
sql = "Select * from finaltickettable where Subteam = '" & cboselect.Text & "'"
If cboselect.SelectedIndex = 0 Then cboselect2.Items.Add(sql)


i tried this one but

whenever i execute the program, the data in my 2nd combo box is Select * from finaltickettable where Subteam = '" & cboselect.Text &
Posted
Updated 23-Nov-11 20:38pm
v2
Comments
Wayne Gaylard 24-Nov-11 2:40am    
I have deleted your response that you posted as an answer, and added it to your original post. Please see my edited answer.

You will need an SQL query something like This :-

SQL
SELECT field1, field2, field3 FROM yourTable WHERE team = @selectedTeam;


[Edited]
You are going to have to put the SQL in a query to the database in a method of it's own, and call this method whenever the combo selected item is changed. Something like this:-

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetTeamMembers(comboBox1.SelectedText);
        }

        void GetTeamMembers(string subTeam)
        {
            using (OleDbConnection con = new OleDbConnection("yourConnectionString"))
            {
                OleDbCommand cmd = new OleDbCommand("Select * from finaltickettable where Subteam = @subTeam", con);
                cmd.Parameters.AddWithValue("@subTeam", subTeam);
                OleDbDataReader dr = cmd.ExecuteReader();
                List<string> teamMembers = new List<string>();
                while (dr.Read())
                {
                    comboBox2.Items.Add((string)dr[0]);
                }
            }
        }


Please not this is just an example. Ideally, you should never have data access code in your user interface. Prefereably you should have a class that describes a Team and the data access method should be in that class and returns all the team members in that team.

[/Edited]
 
Share this answer
 
v2
is this for vb.net? im using vb.net for my program i think this is not vb.net. kindly give me vb.net codes. thanks
 
Share this answer
 
VB
If TypeOf cboselect.SelectedItem Is String Then
            Dim Connection As New MySqlConnection(connStr)
            Dim Command As MySqlCommand = Connection.CreateCommand
            Dim Reader As MySqlDataReader = Nothing
            Command.CommandText = "Select Distinct user" & vbCrLf & _
            "From finaltickettable" & vbCrLf & _
            "Where Subteam = @SubTeam"
            Command.Parameters.AddWithValue("@SubTeam", CStr(cboselect.SelectedItem))
            cboselect2.BeginUpdate()
            cboselect2.Items.Clear()
            Try
                Connection.Open()
                Reader = Command.ExecuteReader
                Do While Reader.Read
                    cboselect2.Items.Add(CStr(Reader("user")))
                Loop
            Catch ex As Exception
            Finally
                Connection.Dispose()
                Command.Dispose()
                'dispose may not be available with sqldatareader
                If Reader IsNot Nothing Then Reader.Dispose()
            End Try
            cboselect2.EndUpdate()
        End If


THIS ONE WORKED.
 
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