Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urgent: how to cascade foreign key to child tables

Status
Not open for further replies.

purplehaze1

Programmer
Jul 23, 2003
86
US
I need to know how to cascade identity key from parent table down to child tables. I have been doing it one way but almost all articles I've read suggest using datarelation to cascade identity key. However, in my code, I save one table a time (see below) and I am confused about how to create relation b/w parent and child tables in my sub routine and cascade identity key.

Below, customer (parent) has patients(child).
I save customer table and get identity key (m_custID). I want to cascade this
key to child table(patient). Currently, I do it the following way in the client program. It works, but I think I should use data relation as correct way to do it.
I'd appreciate your suggestions. Thanks.

'client program

Dim oCustomer as new customer
Dim oPatient as new patient

if oCustomer.SaveCustomer() then
oPatientID.customerID = oCustomer.CustomerID
End if


' Customer class

Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As OleDbRowUpdatedEventArgs)
' Create another Command to get IDENTITY Value
Dim newID As Integer = 0
Dim myCommand As OleDbCommand
Dim strSQL As String

myCommand = New OleDbCommand("SELECT @@IDENTITY", myConnection, trans)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
newID = myCommand.ExecuteScalar()
e.Row.AcceptChanges()
m_custID = newID
End If

End Sub

Public Function SaveCustomer(Optional ByVal bolStartTran As Boolean = True) As Boolean
Dim strsql As String, bolInTran As Boolean
Dim myCommand As OleDbCommand
Dim dr As DataRow, i As Integer

Try
If Not IsDirty Then GoTo Skip_Save

If bolStartTran Then
myConnection.Open()
trans = myConnection.BeginTransaction(IsolationLevel.RepeatableRead)
bolInTran = True
End If

strsql = "SELECT * FROM t_customer WHERE cust_id=" & m_custID
myCommand = New OleDbCommand(strsql, myConnection, trans)
Dim da As New OleDbDataAdapter(myCommand)
Dim cb As New OleDbCommandBuilder(da)
Dim ds As New DataSet()

da.Fill(ds, "Customer")

If ds.Tables(0).Rows.Count = 0 Then
dr = ds.Tables(0).NewRow()
IsNew = True
GoTo SaveRecord
Else
dr = ds.Tables(0).Rows(0)
End If

SaveRecord:
dr("cust_typ_id") = m_custTypeID
dr("cust_cde") = m_custCode
dr("cust_name") = IIf(m_custName = String.Empty, Nothing, m_custName)


If IsNew Then
ds.Tables(0).Rows.Add(dr)
End If

''Delegate for Handling RowUpdated event
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
da.Update(ds, "Customer")

Skip_Save:
If bolInTran Then
trans.Commit()
bolInTran = False
End If

IsDirty = False
IsNew = False
Return True
Rollback_Save:
If bolInTran Then trans.Rollback()
myConnection.Close()
Catch ex As Exception
trans.Rollback()
myConnection.Close()
Fina
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top