Nicolas
Nicolas

Reputation: 2376

Refreshing ListBox Database values on database Update C#

So i have read this question a few times somewhere else but after reading all those question i still have a working solution for it.

The problem is that i am populating a ListBox with Access Database information which you can see below:

private void FinancienData_Load(object sender, EventArgs e)
   {
   try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from Betaald where Maand='Januari'";

            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {                                    
                listBox4.Items.Add(reader["Autoverzekering"].ToString());                   
                listBox4.Items.Add(reader["Brabant Water"].ToString());                   
                listBox4.Items.Add(reader["Eigen Risico Nicolas"].ToString());                    
                listBox4.Items.Add(reader["Essent"].ToString());                  
                listBox4.Items.Add(reader["Extra"].ToString());                    
                listBox4.Items.Add(reader["Gemeenschappelijke Heffingen"].ToString());                    
                listBox4.Items.Add(reader["Huur"].ToString());
                listBox4.Items.Add(reader["Reiskosten Nicolas"].ToString());                    
                listBox4.Items.Add(reader["RKDVC"].ToString());
                listBox4.Items.Add(reader["Telefoonrekening Nicolas"].ToString());                    
                listBox4.Items.Add(reader["Telefoonrekening Wendy"].ToString());                    
                listBox4.Items.Add(reader["Woonverzekering"].ToString());                          
                listBox4.Items.Add(reader["Ziggo"].ToString());
                listBox4.Items.Add(reader["Zorgverzekering Nicolas"].ToString());
                listBox4.Items.Add(reader["Zorgverzekering Wendy"].ToString());                             
            }

            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
  }

This all works fine, BUT there is also a way which a user can update the database values. This is being done as follow:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "update Betaald set Autoverzekering='JA'";
                command.CommandText = query;
                command.ExecuteNonQuery();  //is nu non-query omdat je data insert

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }

This update works, the value is being changed in the database. Though the problem is that the value wont update in the ListBox where it is displayed... I need to find an easy way that when the user presses that button, it wont only update in the Access database but also in the ListBox (without having to restart the entire form).

Already try'd multiple things i read about (listBox4.DataSource, ListBox RefreshItems, etc. ) but for some reason i cant get it to work (probably because im quite new to C# and .NET).

Could anyone help me out?

Thanks in advance!

Upvotes: 0

Views: 787

Answers (1)

Live
Live

Reputation: 120

In your private void FinancienData_Load method, you don't bind your database set to your listbox, but instead you are reading the rows from a table, adding ListBoxItems containing the row results to your listbox. This is mainly why the listBox4.DataSource binding doesn't work.

To make the update work, I'd recommend refactoring the code a little: How about putting the code that loads the values from the database into its own method, e.g. void LoadDataFromDb() . Once done, you can simply call this method after you updated the values. The method will then fetch the updated values from the database table and fill the listBox again. Don't forget to add listBox4.Items.Clear() to your LoadDataFromDb() method, otherwise you'll continously add new items to the listBox, instead of "refreshing" items.

Upvotes: 1

Related Questions