Adding a row to a child table

We have a DataSet with parent/child relations set up. We have a BindingSource and a BindingNavigator configured for each table. When we use the BindingNavigator for the child table to add a new record (click the Add button...) the foreign key value in the child table is not getting filled in. When we call EndEdit on the BindingSource we are getting an exception because this field does not allow nulls in the DataSet.

The relationship between the tables is just like Order/OrderDetails where the OrderDetails table has a PK column (OrderDetailsID) and an FK column (OrderID).

First of all, if we have things configured right between the tables the OrderID column in the child table is supposed to be filled in automatically with the value of the ID in the current row in the parent table, right

If this *is* supposed to happen automatically, what is the checklist of things that must be configured properly for this to happen

Thanks.


Answer this question

Adding a row to a child table

  • voidlogic

    Well this seems to work but it doesn't seem very elegant. I still think I'm missing something...

    Private Sub Parent_textTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Parent_textTextBox.Leave

    Me.Parent1BindingSource.EndEdit()

    If Child2BindingSource.Current Is Nothing Then

    Me.Child2BindingSource.AddNew()

    End If

    Me.Child2BindingSource.ResetCurrentItem()

    End Sub


  • John Talbott

    I am trying to do the same thing in VB2005 and getting no where. I have set up a situation that basically follows the before mentioned scenario. When I press the add button on the navigator a new record is created for the parent table but not the child table. I have tried explicitly doing an addnew on the child binding source. It looks like it creates a new record but byt the time I press the save button the row count is zero.

    I feel like I am missing a basic assumption. Is it correct to assume that doing an addnew on the parent binding source is supposed to automagically create a child record

    Thanks!


  • Kiran_deep

    I've got it figured out. You have to have the following in place for this to work right:

    1. MasterBindingSource, DataSource = DataSet, DataMember = parent table (name)
    2. Relation in the DataSet linking the FK column in the child table to the PK column in the parent table
    3. ChildBindingSource, DataSource = MasterBindingSource, DataMember = FK relation

    Once you have those things in place, and use the BindingSource to add rows to the DataSet, the FK values get filled in automatically (provided you don't disable constraints or whatever).


  • GoodNews

    Thanks for following up with the information you found.

    David Sceppa

    ADO.NET Program Manager

    Microsoft



  • derekslager

    OK, I have an answer to my problem, sort of..... After much frustration, I just deleted all me datasets in my project and reimported my Access database. Visual Studio recreated all my tables and relations etc, and everything worked as it should have.

    I believe I must of made some changes in my Access database that didnt jive with the designer in VS although I was never getting an error, it just didnt work. Just thought I would update this post in the event somebody else ran into this problem

    Mike


  • Thomas_K

    OK - so it seems to work as expected when using datagrids. When you fill in the parent grid and leave it to go to the child grid, the parentID is magically filled in. But we don't want to use the grids - we want to display the data in textboxes, comboboxes, etc. What is the magic that is happening when you leave the parent grid and how can I replicate this without using a grid
  • Harinadh

    I am having the same sort of problem and thought I would reinitiate this thread as it doesnt look like a final solution has been provided.

    I have a simple 'one-to-many' data relationship which I have established using the VS 2005 designer. I am using a tab control to seperate the Master/Parent table from the Detail/Child table. When I click on the Detail tab, my form correctly populates all the appropriate detail records into all all the relevant bound controls (text boxes/comboboxes). It even includes the correct Foreign Key value (which is bound to the Foreign Key Field of my binding source). So, everything seems to work splendidly except for the fact that when I go look at the backend database (MS Access) the Foreign Key of my Child table is not being entered into my database! It just uses the default value of 0 for every detail record I have entered. I cant figure out why it is not populating correctly. Does anyone have an idea what I may doing wrong

    Thanks!!!

    Mike


  • Adding a row to a child table