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

  • Swapping Out the DALSwapping Out the DAL One of the comments that showed up on the blog post about using 3-tiered with DataSets and the ObjectDataSource was, "How do we set this up so that we can swap out the DAL?"  The simple answer...
  • Windows Forms – Passing Parameters at RuntimeWindows Forms – Passing Parameters at Runtime I received the following question over the weekend: I've made a C# form application and I need to send a report name at runtime. How do I add an incoming parameter to the command line? Such as "c...
  • DataSets, TableAdapters, and Transient Retry Logic For SqlAzureDataSets, TableAdapters, and Transient Retry Logic For SqlAzure The main project I’m working on these days is moving several web sites to Azure.  It is something I’ve wanted to be able to try for a while.  I’m working with several other agencies on this project...
  • WinForms – Database Changes not sticking.WinForms – Database Changes not sticking. Saturday afternoon I spent trying to get a Windows Forms application to add data to a database.  It all seemed to work correctly.  Add changes, no errors.  Close window, open window....
  • WinForms – Change The Active TabWinForms – Change The Active Tab This question came in last Friday: I'm trying to code a windows form in vb.net 2005. In my form I have 2 TabControls and a command button. The button is in the first TabControl, so what I want to...