Save form data in two tables

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
So I need to update two tables from one form. The first table is the main table, the second has the id of the main table in it.

Table One

flightcard_id
name
revision
runtime
et

Table Two
system_setup_id
flightcard_id - the id from the first table
position
tilt
etc

Table two i have in details view, not DGV. They are combo boxes for a selection.

So when I create a new record, i need to save both tables. How can I do this, i tried several different things.

VB.NET:
    Private Sub FlightcardBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FlightcardBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.System_setupBindingSource.EndEdit()
        Me.FlightcardBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.FlightTestApp2DataSet)

        System_setupTableAdapter.Insert(Me.ComboBox1.SelectedValue, Me.ComboBox2.SelectedValue, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, DirectCast(DirectCast(Me.FlightcardBindingSource.Current, System.Data.DataRowView).Row, Flight_Test_App.FlightTestApp2DataSet.flightcardRow).flightcard_id)

    End Sub

Please help, i am stuck
 
Hi dsk96m

Did you ever sort out this problem? I am experiencing the same behaviour.

- Two tables in SQLServer. Parent/Child. Relation and Foreign Key Constraint in place. Update and Delete Rule = Cascade.
- Trying to add new record to Parent table (Contacts) and related record to Child table (Locations).
- Using Table AdapterManager.UpdateAll

When I do this using a simple form with 2 x DataGridViews it works as expected. It backfills the Parent primary key to the Child etc. All fine!

If I use exactly the same project, but swap out the DataGridViews for a 'Details' view, created by dragging from the DataSources, it does not work.

- When I Add New it clears all the text boxes in Parent and Child fields, and inserts temporary Primary Key in both = -1.
- When I save, it saves the Parent Record successfully, but does not save the Child. It just clears all the child fields including the temporary primary key = -1.

I agree with the comments above that the GUI should be irrelevant, but it keeps happening to me.

Appreciate your feedback if you ever sorted it out. Otherwise I may create a new thread.

Cheers - Paul
 
My code ...

This is the only code in the small test project - works with DGV, not with Details view.



Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'DataSet1.Contacts' table. You can move, or remove it, as needed.
Me.ContactsTableAdapter.Fill(Me.DataSet1.Contacts)
Me.LocationsTableAdapter.Fill(Me.DataSet1.Locations) ' CHANGED THE ORDER OF THESE SO PARENT LOADS FIRST...

End Sub
 
 

Private Sub AddNewButton_Click(sender As Object, e As EventArgs) Handles AddNewButton.Click

Me.ContactsBindingSource.AddNew()
Me.LocationsBindingSource.AddNew()

End Sub
 
 

Private Sub ContactsBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles ContactsBindingNavigatorSaveItem.Click

Me.Validate()
Me.ContactsBindingSource.EndEdit()
Me.LocationsBindingSource.EndEdit() ' ADDED EndEdit() HERE TO THE CHILD BINDING SOURCE TOO ...

Me.TableAdapterManager.UpdateAll(Me.DataSet1)


End Sub

End Class
 
Last edited:
Back
Top