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
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>);
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
nevermind i shoudlnt say it crashes it freezes
for a little while
for a little while
ASKER
@all would this freeze be because of internet timeouts
ASKER
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.
Becoz It can also affect on the performance..
please update your both the queries as below.
select state from db.table
Becoz It can also affect on the performance..
@cabalsdemon have you solved it??
ASKER
no sir i will try your solutioin right now thank you
ASKER
i keep getting an error connectiong timeout expired the timperiod elapsed prior to obtaining the connection
ASKER
if i have to i will try to make it use a access database file
ASKER
you know what i havent closed any mysql connections have i
conDataBase.Close();
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.
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.
ASKER
ok that works better but it isnt wiping the data from the first selection
ASKER
it keeps stacking items
ASKER
is it
listBox2.Refresh();
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();
ASKER
like this
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
Fill_listbox2();
}
solved?
ASKER
yes sir that worked
thank you very much
you guys rock
comboBox1.SelectedIndexChanged +=
new System.EventHandler(comboBox1_SelectedIndexChanged);
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
Fill_listbox2();
}
thank you very much
you guys rock
ASKER
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
Fill_listbox2();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
Fill_listbox2();
}
comboBox1.SelectedIndexChanged +=
new System.EventHandler(comboBox1_SelectedIndexChanged);
(:
ASKER
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
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
ASKER
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