Fill TableAdapter from another form

FreeriderUK

Well-known member
Joined
Jun 27, 2006
Messages
100
Location
London
Programming Experience
10+
Hi all,

I am editing a Customers table from Access database on Form2.
On Form1 I have a combobox bound to the Customers table.

When I update the changes and close Form2, I want the combobox to include the changes I made.

I have tried the following code in my close form event on Form2.
VB.NET:
 Form1.MyCustomersTableAdapter.Fill(Me.MyDataSet.Customers)

But it doesn't fill the dataset on Form1.
I can execute this on Form1:
VB.NET:
[B]Me[/B].MyCustomersTableAdapter.Fill(Me.MyDataSet.Customers)
and it updates ok.

How do I update the dataset when I close Form2?

Thanks in advance.
 
Hi all,

I am editing a Customers table from Access database on Form2.
On Form1 I have a combobox bound to the Customers table.

When I update the changes and close Form2, I want the combobox to include the changes I made.
Then, form2 and form1 should use the SAME dataset as a datasource for the combo box.. Its a bit daft to have form2 open, edit the same data down from the database, send it back to the db, and then have form1 pull that data back again.

I have tried the following code in my close form event on Form2.
VB.NET:
 Form1.MyCustomersTableAdapter.Fill(Me.MyDataSet.Customers)
Given that "Me" is contextual and in this context, refers to Form2.. would you expect it to fill Form1's dataset? Youre actually using Form1's tableadapter to fill form2's dataset.. Think about it some more ;)

And then dont do it like that :) Form1 and 2 should share the same datasource. Changes made will be reflected automatically


I can execute this on Form1:
VB.NET:
[B]Me[/B].MyCustomersTableAdapter.Fill(Me.MyDataSet.Customers)
and it updates ok.
Well.. Yeah. Me in that context refers to Form1, so Form1's TA is filling Form1's DS

How do I update the dataset when I close Form2?
As noted, if Form1 and 2 share a dataset, all changes are live.

Remember! Its not enough to simply say:

Form2.MyDataSet = Form1.MyDataSet

Because the BindingSOurce on Form2 is already pointing to the Form2's MyDataSet:

VB.NET:
[FONT=Courier New]Form1.MyDataSet --pointer--> [MyDataSetObjectInMemory][/FONT]
[FONT=Courier New]Form1.XYZBindingSource --pointer--^[/FONT]
 
[FONT=Courier New]Form2.MyDataSet --pointer--> [ADifferentMyDataSetObjectInMemory][/FONT]
[FONT=Courier New]Form2.XYZBindingSource --pointer--^[/FONT]
 
 
[FONT=Courier New][B]If you say Form2.MyDataSet = Form1.MyDataSet, you get:[/B][/FONT]
 
[FONT=Courier New]Form1.MyDataSet --pointer--> [MyDataSetObjectInMemory][/FONT]
[FONT=Courier New]Form1.XYZBindingSource --pointer--^ |[/FONT]
[FONT=Courier New]Form2.MyDataSet --pointer-----------/ [/FONT]
 
[FONT=Courier New]                            [ADifferentMyDataSetObjectInMemory][/FONT]
[FONT=Courier New]Form2.XYZBindingSource --pointer--^[/FONT]
[FONT=Courier New]
[/FONT]

Because your controls are bound to the bindingsource and it is still pointing to form2's default instance, you actually have to change what the BindingSource is pointing to!

Form2.XYZBindingSource.DataSource = Form1.MyDataSet
 
Thanks cjard.

After reading your post about six times, I eventually understood what you were saying!

For anyone with less patience - the bit you need is:

VB.NET:
[FONT=Arial]... you actually have to change what the BindingSource is pointing to![/FONT]
 
[FONT=Arial]Me.XYZBindingSource.DataSource = Form1.MyDataSet[/FONT]

Which should be put in Form2 load event.
 
Back
Top