|
-
Oct 20th, 2005, 03:17 PM
#1
Thread Starter
Fanatic Member
Delegate?
Here is the scenario:
I have a form that uses a Typed DataSet that contains 9 tables. In my Save routine, I go through each DataTable by name and look at whether items were added or editted and call inserts and updates accordingly. I would prefer to pass this information into a subroutine since it is so repetitive, but how do I pass the name of the insert or edit routine from my InsertData and UpdateData class modules to another routine.
For example, this section of code is the same, just different datatables in use. However, each datatable has a different Insert or Update routine.
VB Code:
If Not ds.Patients.GetChanges(DataRowState.Added) Is Nothing Then
iData.InsertPatient(ds.Patients.GetChanges(DataRowState.Added), myTrans)
End If
If Not ds.Patients.GetChanges(DataRowState.Modified) Is Nothing Then
uData.UpdatePatient(ds.Patients.GetChanges(DataRowState.Modified), myTrans)
End If
I would like to do something like:
VB Code:
Private Sub SaveData()
' Declarations and other code up here
Call CheckForAdditionsAndPost(ds.Patients, myTrans, "InsertPatient")
Call CheckForModificationsAndPost(ds.Patients, myTrans, "UpdatePatient")
End Sub
Private Sub CheckForAdditionsAndPost(ByRef inDataTable as DataTable, ByRef inTrans as SqlTransaction, byval inProcedure as String)
Dim iData as New InsertData
If Not inDataTable.GetChanges(DataRowState.Added) Is Nothing Then
iData. [COLOR=Sienna]Eval(inProcedure).[/COLOR] (inDataTable.GetChanges(DataRowState.Added), inTrans)
End If
' Cleanup
iData.Dispose()
End Sub
Some example code would be greatly appreciated!!
-
Oct 20th, 2005, 03:59 PM
#2
Thread Starter
Fanatic Member
Re: Delegate?
Maybe I have figured it out. Here is what I have. Let me know if there is a better way to do this.
VB Code:
Private Delegate Sub InsertUpdateData(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction)
Private Sub SaveData
Dim myDelegate As InsertUpdateData
' Check for Additions to Contacts
myDelegate = AddressOf iData.InsertContact
Call CheckForAdditionsAndPost(ds.ContactsL1.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
Call CheckForAdditionsAndPost(ds.ContactsR1.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
Call CheckForAdditionsAndPost(ds.ContactsL2.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
Call CheckForAdditionsAndPost(ds.ContactsR2.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
' Check for Updates to Contacts
myDelegate = AddressOf uData.UpdateContact
Call CheckForUpdatesAndPost(ds.ContactsL1.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
Call CheckForUpdatesAndPost(ds.ContactsR1.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
Call CheckForUpdatesAndPost(ds.ContactsL2.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
Call CheckForUpdatesAndPost(ds.ContactsR2.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
End Sub
Private Sub CheckForAdditionsAndPost(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction, ByRef inPatient_ID As Int32, ByRef inDelegate As InsertUpdateData)
If Not inDataTable Is Nothing Then
Dim dr As DataRow
' Set the Patient_ID for each row in the Insert
For Each dr In inDataTable.Rows
dr("Patient_ID") = inPatient_ID
Next
BindingContext(inDataTable).EndCurrentEdit()
inDelegate.Invoke(inDataTable, inTrans)
End If
End Sub
Private Sub CheckForUpdatesAndPost(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction, ByRef inDelegate As InsertUpdateData)
If Not inDataTable Is Nothing Then
inDelegate.Invoke(inDataTable, inTrans)
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|