Populating combobox with values from Table using C#
-
Hi, I have following function below but is not populating values from the table product?
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{database = new OleDbConnection(connectionString); // Create a command object and then a query which accepts a papameter System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database); database.Open(); System.Data.OleDb.OleDbDataReader dr; dr=cmd.ExecuteReader(); while (dr.Read()) { //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() }); //Change the index according to the values you want to populate in the combobox. comboBox3.Items.Add(dr\["ProductName"\]); } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
regards,
-
Hi, I have following function below but is not populating values from the table product?
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{database = new OleDbConnection(connectionString); // Create a command object and then a query which accepts a papameter System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database); database.Open(); System.Data.OleDb.OleDbDataReader dr; dr=cmd.ExecuteReader(); while (dr.Read()) { //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() }); //Change the index according to the values you want to populate in the combobox. comboBox3.Items.Add(dr\["ProductName"\]); } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
regards,
- you probably want to clear out the old values from the combo box before you populate it with new data - don't forget to close
dr
anddatabase
after you're finished using them otherwise you'll leak resources - what happens when you step through this in the debugger? does yourwhile
loop properly iterate through all the rows in the DB? - if the only column you're using from the DB table isProductName
then doing aSelect *
is rather inefficient. - just for my own curiosity, why are you doing this in the SelectedIndexChanged event handler for the combo box?
Last modified: after originally posted -- grammar correction
-
- you probably want to clear out the old values from the combo box before you populate it with new data - don't forget to close
dr
anddatabase
after you're finished using them otherwise you'll leak resources - what happens when you step through this in the debugger? does yourwhile
loop properly iterate through all the rows in the DB? - if the only column you're using from the DB table isProductName
then doing aSelect *
is rather inefficient. - just for my own curiosity, why are you doing this in the SelectedIndexChanged event handler for the combo box?
Last modified: after originally posted -- grammar correction
Hi Jimmanuel, ...What else method or way u recommend instead of SelectedIndexChanged ? can u pls gimme example? ... I have 3 fields in that table so why it is inefficient? i have changed code and following is here: to include dr.close method..
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{database = new OleDbConnection(connectionString); // Create a command object and then a query which accepts a papameter System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select distinct ProductName from Products", database); database.Open(); System.Data.OleDb.OleDbDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() }); //Change the index according to the values you want to populate in the combobox. comboBox3.Items.AddRange(new Object\[\] { dr\[1\].ToString() }); } dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; }
}
please help
-
Hi, I have following function below but is not populating values from the table product?
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{database = new OleDbConnection(connectionString); // Create a command object and then a query which accepts a papameter System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database); database.Open(); System.Data.OleDb.OleDbDataReader dr; dr=cmd.ExecuteReader(); while (dr.Read()) { //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() }); //Change the index according to the values you want to populate in the combobox. comboBox3.Items.Add(dr\["ProductName"\]); } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
regards,
-
Hi Jimmanuel, ...What else method or way u recommend instead of SelectedIndexChanged ? can u pls gimme example? ... I have 3 fields in that table so why it is inefficient? i have changed code and following is here: to include dr.close method..
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{database = new OleDbConnection(connectionString); // Create a command object and then a query which accepts a papameter System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select distinct ProductName from Products", database); database.Open(); System.Data.OleDb.OleDbDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() }); //Change the index according to the values you want to populate in the combobox. comboBox3.Items.AddRange(new Object\[\] { dr\[1\].ToString() }); } dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; }
}
please help
How often is the DB Table going to change? If it doesn't change or rarely changes then you really only need to populate the combo box values once - when the form loads. If it does change frequently then there are lots of good ways to keep the combo box up to date. You could use a timer to reload the values every so often, put a button on the form that allows the user to refresh whenever they want or work out some way of receiving a notification any time that the table changes.
database.Close()
is just as important. That closes the connection to the DB, otherwise it gets orphaned. A using[^] statement would be helpful in making sure that everything gets cleaned up whether an exception is thrown or not. Say your table has 10 rows; that means that all you want from it is 10 product names but aselect *
statement is giving you back the 10 product names plus 20 other values that you aren't using. That's 3 times as much network traffic as you actually need. It might not be a big deal now but what if there were 10,000 rows? What if later on the table changes and more columns get added? For each column that's added that's more unnecessary traffic between your app and your DB and more time and memory required to handle yourselect
.Select distinct ProductName
is the way to go here. In fact, I can't recall a time that I've ever usedselect *
. -
why are you populating comboBox3 from db when something got selected on the same combobox? You know showing any other thing except populating your combobox. You need to do this only once or when ever the data changes.
Yusuf
thanks Yusuf, i just want to see the product names in this comboBox thats all..and which will come from Products Table... regards, :confused: