C# mysql combobox listbox value wont change

helo to all i am making a program to grab the data from a mysql database and it is working,thsi listbox grabs the data from the first available combobox item but when i click the next dropdown item the listbox does not change value to the value of that dropdown item it stays the same
thank you in advanced for your help

here is my code

using System;
using System.Collections.Generic<wbr ></wbr><wbr ></wbr><wbr ></wbr>;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Fillcombo();
            Fill_listbox1();
            
        }


        

        void Fill_listbox1()
        {
            sstring constring = @"server=externalmysqlserv<wbr ></wbr><wbr ></wbr><wbr ></wbr>er;userid=<wbr ></wbr>user;passw<wbr ></wbr>ord=pw";
            string Query = "select * from db.table where state= '"+ comboBox1.SelectedItem +"'";
            MySqlConnection conDataBase = new MySqlConnection(constring)<wbr ></wbr><wbr ></wbr><wbr ></wbr>;
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader(<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
                while (myReader.Read())
                {
                    string sName = myReader.GetString("state"<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
                   listBox1.Items.Add(sName);<wbr ></wbr><wbr ></wbr><wbr ></wbr>
                   listBox1.SelectedIndex = 0;
                  
                   


               }



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
            }


        }

        void Fillcombo()
        {
            string constring = @"server=externalmysqlserv<wbr ></wbr><wbr ></wbr><wbr ></wbr>er;userid=<wbr ></wbr>user;passw<wbr ></wbr>ord=pw";
            string Query = "select * from db.table";
            MySqlConnection conDataBase = new MySqlConnection(constring)<wbr ></wbr><wbr ></wbr><wbr ></wbr>;
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader(<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
                while (myReader.Read())
                {
                    string sName = myReader.GetString("state"<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
                    comboBox1.Items.Add(sName)<wbr ></wbr><wbr ></wbr><wbr ></wbr>;
                    comboBox1.SelectedIndex = 0;
                    

                }



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message<wbr ></wbr><wbr ></wbr><wbr ></wbr>);
            }
        }

        

        
    }
}

Open in new window

cabalsdemonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
You need to handle SelectedIndexChanged event of the ComboBox:

Add this to Fillcombo() function after you fill in data:

comboBox1.SelectedIndexChanged += 
			new System.EventHandler(comboBox1_SelectedIndexChanged);

Open in new window


And add handler function for SelectedIndexChanged:

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
       Fill_listbox1();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
just subscribe to the combobox1 SelectedIndexChanged event and in the event handler change the content of the listBox1 by calling Fill_listbox1() method.

please refer below updated code..


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Fillcombo();
            Fill_listbox1();
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }
       

        void Fill_listbox1()
        {
            sstring constring = @"server=externalmysqlserver;userid=<wbr ></wbr>user;passw<wbr ></wbr>ord=pw";
            string Query = "select * from db.table where state= '"+ comboBox1.SelectedItem +"'";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                while (myReader.Read())
                {
                    string sName = myReader.GetString("state");
                   listBox1.Items.Add(sName);
                   listBox1.SelectedIndex = 0;
               }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        void Fillcombo()
        {
            string constring = @"server=externalmysqlserver;userid=<wbr ></wbr>user;passw<wbr ></wbr>ord=pw";
            string Query = "select * from db.table";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                while (myReader.Read())
                {
                    string sName = myReader.GetString("state");
                    comboBox1.Items.Add(sName);
                    comboBox1.SelectedIndex = 0;
                   

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           Fill_listbox1();
        }
    }
}
0
cabalsdemonAuthor Commented:
@pham ok now it kinda works
when i try to change the value by clicking next selection the program crashes
is there too much data flowing through it

or what do you think
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

cabalsdemonAuthor Commented:
nevermind i shoudlnt say it crashes it freezes
for a little while
0
cabalsdemonAuthor Commented:
@all would this freeze be because of internet timeouts
0
cabalsdemonAuthor Commented:
should i be using a access db file or something to that nature
0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
Maybe, It is becoz of internet timeouts, but you make use of BackgroundWorker to fill your listbox asynchronously.
0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
In your code, while you are filing combobox and listbox you have used * in sql query but your not going to use all columns returned by the database. You jsut need "state" column so your query should only have "state" column.

please update your both the queries as below.

select state from db.table

Open in new window


Becoz It can also affect on the performance..
0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
@cabalsdemon have you solved it??
0
cabalsdemonAuthor Commented:
no sir i will try your solutioin right now thank you
0
cabalsdemonAuthor Commented:
i keep getting an error connectiong timeout expired the timperiod elapsed prior to obtaining the connection
0
cabalsdemonAuthor Commented:
if i have to i will try to make it use a access database file
0
cabalsdemonAuthor Commented:
you know what i havent closed any mysql connections have i

conDataBase.Close();

Open in new window

0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
The error means the connection pool is out of connections. The usual cause is forgetting to close a connection after you use it.

You are opening the connection read data using datareader but you have not losing the connection anywhere in the is that the actual working code? if it is you should close connection every time you are opening it.
0
cabalsdemonAuthor Commented:
ok that works better but it isnt wiping the data from the first selection
0
cabalsdemonAuthor Commented:
it keeps stacking items
0
cabalsdemonAuthor Commented:
is it

listBox2.Refresh();
0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
You have to clear your listbox before filling new item in it use below code in as first line of Fill_listbox1() method

listbox1.Items.Clear();

Open in new window

0
cabalsdemonAuthor Commented:
like this

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            Fill_listbox2();
        }

Open in new window

0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
solved?
0
cabalsdemonAuthor Commented:
yes sir that worked
 
comboBox1.SelectedIndexChanged += 
			new System.EventHandler(comboBox1_SelectedIndexChanged);

Open in new window


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            Fill_listbox2();
        }

Open in new window


 

thank you very much

you guys rock
0
cabalsdemonAuthor Commented:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            Fill_listbox2();
        }

Open in new window

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            Fill_listbox2();
        }

Open in new window

comboBox1.SelectedIndexChanged += 
			new System.EventHandler(comboBox1_SelectedIndexChanged);

Open in new window

0
Shaikh MubeenSenior Software, Web & Mobile Apps DeveloperCommented:
(:
0
cabalsdemonAuthor Commented:
yah i think i need a better mysql server if i want to use external
so i may have to use either ms access file if it is possible
or i may have to put it all in the code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.