How to update the DataSource of a BindingSource?

DoJa

Active member
Joined
Mar 23, 2014
Messages
33
Programming Experience
1-3
I'm not sure if I'm asking the right question here, but after trying various things for the last few hours with no joy I come here seeking greater wisdom.

I have a form with various text boxes which are bound to a binding source and are used to display and update the associated rows in a database.

The code I have looks like this:

C#:
//Dataset
DataSet dsCustomer;

//Get data on currently selected member
dsCustomer = dbCustomer.queryDb("SELECT * FROM customer WHERE id = ..

//Create new binding source
            bsourceCustomer = new BindingSource();

//Set datasource for binding sources
            bsourceCustomer.DataSource = dsCustomer.Tables["customer"];


//Bind customer text/comboboxes to binding source (varchar datatype)
                txtCustomerid.DataBindings.Add("Text", bsourceCustomer, "id");
                // lots more lines like the above - one for each text box
So this works great, and does what I want it to do. The only problem is, I can only use it once. If I try to run the same code again I get an error saying the bindings already exist. If I only do the database query (with different id) then I get no errors but it is still only showing the data for the first person I viewed.

How do I update the page so when I re-run the db query with a new id, all my textboxes update to reflect the new values?

I've tried all sorts of things such as databindings.remove and bsourceCustomer.ResetBindings(true); but none of it appears to yield the desired effect.

What have I missed?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
Don't add the data-bindings to the controls more than once. Once the controls are bound they are bound. You can do whatever you want to the data behind that but don't touch the bindings. You can make modifications to the DataSet you've already got or you can get a new DataSet and bind that to the BindingSource but do not touch the data-bindings that already exist.

What you should be doing is adding the BindingSource in the designer rather than in code and then separating your code into two parts: the part that gets the data and binds it to the BindingSource and then the part that binds the BindingSource to the controls. The first time, you run both parts but then after that you only run the first part because the controls are already bound.

Even better, if you get data from the database a second time or later, you should be merging that data into the DataSet that's already bound to the BindingSource, not throwing away everything you've got that's already bound. For one thing, that will prevent the selection changing in the controls. If you unbind the first DataSet and bind another then the bound controls will show the first record again because the record that they were displaying doesn't exist any more.
 

DoJa

Active member
Joined
Mar 23, 2014
Messages
33
Programming Experience
1-3
What you should be doing is adding the BindingSource in the designer rather than in code and then separating your code into two parts: the part that gets the data and binds it to the BindingSource and then the part that binds the BindingSource to the controls. The first time, you run both parts but then after that you only run the first part because the controls are already bound.
Done. So Far so good...

Next I made a new dataset called dscustomer2 , put some data into it and bound to the binding source. No errors, but no change either.

I was expecting this to update all my bound controls to display the result from the new query but when I click the button nothing happens, and it still displays the info from the previous customer.

C#:
private void button1_Click(object sender, EventArgs e)
        {
            DataSet dsCustomer2;
            dsCustomer2 = dbCustomer.queryDb("SELECT * FROM customer WHERE id = " + clsGlobalvars.varCustomerid.ToString(), "customer");
            bsourceCustomer.DataSource = dsCustomer2.Tables["customer"];
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
I wouldn't have thought that it would be necessary but try calling ResetBindings on the BindingSource after setting the DataSource. If you still don't see what you expect then I would suggest that your expectations may be incorrect. You check easily enough though, by comparing what's in the BindingSource to what's in the controls with a few lines of code.
 
Top Bottom