I am having a problem trying to save records in a parent / child data relation. I have to tables tblContacts & tblMember. There is a fk relation on the ContactID field. The tblContacts table is the master record. I am using a set up bound windows forms controls to do the data collection. The problem that I am having is in saving the child record. After the form is populated in both the fields and hitting the save button the items in the child record get deleted. It does add a row in the child record but only the ContactID (from the master table) and the key for the tblMember table.

I can see that since the new master record incites the addition of a new row, the data is abandoned. Once there is a save done to the master / child record I can go back and modify the fields and it saves it successfully. I know I must be missing something small. I am not sure how to work around this so that the controls are read into memory and then completed in the new row. I have an example of my code:

Private Sub TblContactsBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblContactsBindingNavigatorSaveItem.Click

        Dim newMemberRow As DataRow 'To create a new row in the tblMember table
        Dim contactID As Integer 'To hold the contactID to insert in the new member ContactID field
        Dim rowsaffected As Integer 'To indicate the number of rows affected by update
        Dim contactIndex As Integer


        Me.Validate()
        Me.TblContactsBindingSource.EndEdit()
        Me.TblContactsTableAdapter.Update(Ds.tblContacts)

        ' Me.TblContactsBindingSource.MoveLast()


        contactIndex = Me.TblContactsBindingSource.Position()



        MessageBox.Show("The contact Index is " & contactIndex)

        contactID = Me.Ds.Tables("tblContacts").Rows(contactIndex).Item("ContactID")

        Me.TblContactsBindingSource.ResetBindings(True)

        If (Me.TblMemberBindingSource.Find("ContactID", contactID.ToString()) < 0) Then



            MessageBox.Show("Current Position in table " & TblMemberBindingSource.Position().ToString())


            MessageBox.Show("The ContactID is " & contactID.ToString())




            '  newMemberRow = Ds.Tables("tblMember").NewRow()
            '  newMemberRow.Item("ContactID") = contactID
            '  newMemberRow.Item("Status") = StatusTextBox.Text
            '  newMemberRow.Item("MemberNotes") = MemberNotesTextBox.Text
            

            Try
                Ds.Tables("tblMember").Rows.Add(newMemberRow)
                Me.Validate()
                
                Me.TblMemberBindingSource.EndEdit()
                Me.TblMemberTableAdapter.Update(Ds.tblMember)


            Catch ex As Exception

                MessageBox.Show(ex.Message)

            End Try

        ElseIf (Me.TblMemberBindingSource.Find("ContactID", contactID.ToString()) >= 0) Then

            TblMemberBindingSource.MoveFirst()

            Try
                Me.TblMemberBindingSource.EndEdit()

            Catch ex As Exception

                MessageBox.Show(ex.Message)

            End Try

        End If

        'Me.TableAdapterManager.UpdateAll(Me.Ds)

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.