954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to refresh Combobox items

Hi

I am populating my combobox with table's data (example table name is "ITEMS" and table is having only one column and that is displayed as combobox items). In my form i am having i combobox,button and textbox. I will enter a new item in textbox and if i click on button, that new item will be stored in a table i.e. "ITEMS"). my problem is that new item has to be displayed in combobox items without closing my form and again opening it.
If i click button by entering new item in textbox that value has to be stored in table and combobox has to be populated with new item.

Thanks

nmakkena
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

A form doesn't close by itself. Could you send your code with the eventhandler of the button?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

When you "open" the form, include as part of the calling to open method an update of your combobox items.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Yo dude, you can always just refresh the connection, you don't need to close your form,

i take it you want something like this

using System.Data.SqlClient;


namespace TestDummyTWO
{
    public partial class Form1 : Form
    {
        SqlConnection sqlcon =
            new SqlConnection("Data Source=.;Initial Catalog=your_table;Integrated Security=True;Asynchronous Processing = True");

        SqlCommand sqlCom = new SqlCommand("SELECT ITEMS FROM YOUR_TABLENAME");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlDataReader reader = sqlCom .ExecuteReader();

            // Looping throuhg the list of data
            while (reader.Read())
            {
                // Fill combo Box with data
                comboBox1.Items.Add(reader["ITEMS"]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // you added the new item from the tex box into the DATA TABLE
            // Not the combobox
            // use sqlCom again to do so

            sqlCom.CommandText = "Insert into blah blah";

            /* THIS IS WHAT YOU HAVE TO DO */
            sqlcon.Close();
            sqlcon.Open();
        }


Hope that helps ...

cVz
Junior Poster
140 posts since Mar 2008
Reputation Points: 29
Solved Threads: 7
 

Hi
Thanks alot. it worked for me

nmakkena
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Cooooollll

cVz
Junior Poster
140 posts since Mar 2008
Reputation Points: 29
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You