Link to home
Start Free TrialLog in
Avatar of cabalsdemon
cabalsdemon

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cabalsdemon
cabalsdemon

ASKER

@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
nevermind i shoudlnt say it crashes it freezes
for a little while
@all would this freeze be because of internet timeouts
should i be using a access db file or something to that nature
Maybe, It is becoz of internet timeouts, but you make use of BackgroundWorker to fill your listbox asynchronously.
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..
@cabalsdemon have you solved it??
no sir i will try your solutioin right now thank you
i keep getting an error connectiong timeout expired the timperiod elapsed prior to obtaining the connection
if i have to i will try to make it use a access database file
you know what i havent closed any mysql connections have i

conDataBase.Close();

Open in new window

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.
ok that works better but it isnt wiping the data from the first selection
it keeps stacking items
is it

listBox2.Refresh();
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

like this

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

Open in new window

solved?
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

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

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