Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Auto Populate Foreign Keys?
Message
From
04/03/2004 12:46:52
 
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
00882941
Message ID:
00883162
Views:
19
Kevin,

Thanks for getting back to me.

I have set the ForeignParentKeyField property in the child Biz obj and have had success when only adding one header record for each set of child records and then saving.
However when I try to batch the process ALL of the child records foreign key fields are populated with the LAST generate header record PK.

Following is a test class that produces Invoice and Invoice lines but the problem is that all the Invoice Lines are populated with the last Invoice PK.

I know I can’t expect the base business object class to do everything but I want to use as much of your framework as possible.

I have been thinking about testing using One dataset and have each biz object add rows to their respective tables using the NewRow method. By adding a relation object I could expect ADO.Net to handle the cascading of the PK to FKs.

Please advise if there is a better approach.

Best regards,

Terry Carroll
Imports OakLeaf.MM.Main.Data
Imports OakLeaf.MM.Main.Collections
Imports OakLeaf.MM.Main


'Test Class for Basic Invoice Generation from NTO Charges. Does not include updates to ARTransaction Table
'table or Billing Summary.
Public Class InvoiceBusiness2
    Inherits ABusinessObject

    Dim oInvoiceLinesBusiness As InvoiceLinesBusiness

    Sub New()
        Me.TableName = "Invoice"
        Me.PrimaryKey = "I_Pk"
        Me.UpdateSelectStatement = "SELECT * FROM Invoice"
        Me.AutoSaveOnParentSaved = True
        Me.RetrieveAutoIncrementPK = True
        'Link the Invoice Line table biz obj
        Me.oInvoiceLinesBusiness = CType(New InvoiceLinesBusiness, InvoiceLinesBusiness)
        Me.RegisterChildBizObj(Me.oInvoiceLinesBusiness)
        Me.oInvoiceLinesBusiness.RegisterParentBizObj(CType(Me, InvoiceBusiness2))
    End Sub
    Public Function GenerateInvoicesFromNTOCharges() As Integer
        'Return the number of Charges Processed

        'Set so the rows in the line items are not refreshed  when NewRow called
        'HELPED but only the last Invoice number was used as FK for ALL invoice Lines
        Me.oInvoiceLinesBusiness.AutoNewOnParentAdded = False
        Me.oInvoiceLinesBusiness.AutoEmptyOnParentAdded = False
        Me.AutoIncrementCustom = True   'Default Values

        Dim oNTOChargesBusiness As New NTOChargeBusiness

        'Get the NtoCharges 
        Dim dsNTOCharges As DataSet = oNTOChargesBusiness.GetNtoChargesNotInvoiced

        'Check if there are charges to process
        If dsNTOCharges.Tables("NTOCharge").Rows.Count = 0 Then
            Return 0
        End If

        'Dim dsI As DataSet for Invoice header
        Me.GetEmptyDataSet()

        'Dim dsIL As DataSet for invoice line items
        Me.oInvoiceLinesBusiness.GetEmptyDataSet()

        ' Now loop thru the NTOCharges to build the invoices
        Dim dtChg As DataTable = dsNTOCharges.Tables("NTOCharge")

        'Place holder to test when client changes. Trigger Invoice header.
        Dim iN_pk As Integer = 0
        'Running Invoice Amount. Set to 0 when client changes
        Dim iAmount As Integer

        Dim drChg As DataRow  ' Charges
        Dim drI As DataRow    ' Invoice header
        Dim drIL As DataRow   ' Invoice Line

        Dim i As Integer
        For i = 0 To dtChg.Rows.Count - 1
            drChg = dtChg.Rows(i)

            'NTO Charges are ordered by Client pk when client PK changes add a new Invoice header record
            If iN_pk <> drChg("N_Pk") Then
                'Add Invoice Header Record
                Me.NewRow()
                drI = Me.DataRow


                drI("I_N_Name") = drChg("N_Name")
                drI("I_N_Addr1") = drChg("N_Addr1")
                drI("I_N_Addr2") = drChg("N_Addr2")
                'other values updated/\/\/\/\/\/

                'Save Client PK as place holder
                iN_pk = drChg("N_Pk")

                'Reset the Running Invoice Total amount
                iAmount = 0
            End If

            'Add Invoice Line record
            Me.oInvoiceLinesBusiness.NewRow()
            drIL = Me.oInvoiceLinesBusiness.DataRow

            'Invoice line detail
            drIL("ILitemdesc") = drChg("ncindesc")
            drIL("IlIn_Pk") = drChg("NcIn_pk")
            drIL("IlNt_pk") = drChg("NcNt_pk")
            drIL("IlQty") = drChg("NcQty")
            drIL("IlPrice") = drChg("NcPrice")

            'Other columns updated/\/\/\/\/\\/

            'Set the running invoice amount
            iAmount += drIL("IlQty") * drIL("IlPrice")

            'Add the running total to the invoice amount and balance
            drI("I_Amount") = iAmount
        Next
        'This should call the save in the child biz objects
        Me.SaveDataSet()
        Return dsNTOCharges.Tables("NTOCharge").Rows.Count
    End Function
End Class
>Terry,
>
>>I want to generate invoice and invoice line item records in a batch, building the data from Orders and Order Line items. The orders are entered in a basic MM.Net winform using Parent child biz objs where the foreign keys are auto populated.
>>
>>Is there a way to use Biz Objs to "batch" generate related data and have the foreign keys auto populated?
>>
>>Or, should I just resort to basic ADO.Net and cascading PKs using a dataset and a relation object?
>
>Does the MM .NET business object's ForeignParentKeyField do what you need? There's a description of this property in the Dev Guide.
>
>Regards,
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform