One Form with Multiple Tables

B01I0093 I received the following question a few days ago:

“I am trying to use three tables from the same dataset in one form that I am creating in vs.net winforms my problem I guess is the setup of it as I can get them all on the form but cant get them to all post to the separate tables when I click save I am using a mysql database with the Mysql connector not the obdc can this even be done?”

Frustrating, isn’t it?  .NET makes some things SO easy, and then when it isn’t you figure you must have done something wrong.

The good news is, your problem probably has nothing to do with using mySQL–it is all in how you are using the database stuff in WinForms.

I actually had to go digging for some old code to find some samples, but here is your basic problem.

The wizardy stuff in .NET only really works with one table at a time.  Once you put multiple tables on a form you have to go in and wire up stuff on your own.

If you check your Save button’s click  event, you will probably see code that looks something like this:

private void formBindingNavigatorSaveItem_Click
    (object sender, EventArgs e)
{
    this.Validate();
    this.driversBindingSource.EndEdit();
    this.tableAdapterManager
        .UpdateAll(this.socialSubmitterDataSet);
 }

But when you put in multiple tables, you end up with multiple BindingSources, so you need to add each of those in with the EndEdit() call and then call UpdateAll() once they’ve all been closed out.

private void formBindingNavigatorSaveItem_Click
    (object sender, EventArgs e)
{
    this.Validate();
    this.dataOneBindingSource.EndEdit();
    fKdataOneDataTwoBindingSource.EndEdit();
    fKdataOneDataThreeBindingSource.EndEdit();
    dataFourBindingSource.EndEdit();
    this.tableAdapterManager
       .UpdateAll(this.socialSubmitterDataSet);
}

Notice that some of the BindingSources are actually foreign keys.  It will depend on how you have your datasets wired in to your form, but the most likely scenario is that you have foreign keys that you are bound to that are pointing to the  tables.  This is what allows you to move your cursor in to a new row in a master table and have the child tables automatically update to show the children.

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear