Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

        Dim oEdiDoc As ediDocument
        Dim oSchema As ediSchema
        Dim oSchemas As ediSchemas
        Dim oInterchange As ediInterchange
        Dim oMessage As ediTransactionSet
        Dim oSegment As ediDataSegment
        Dim sEdiFileName As String
        Dim sSefFileName As String
        Dim sSql As String = ""

        Dim sAppPath As String = AppDomain.CurrentDomain.BaseDirectory

        ' Create the top-level application object "ediDocument". 
        oEdiDoc = New ediDocument

        ' This makes certain that Framework EDI only uses the SEF file provided,
        ' and that it does not use its built-in Standard Reference table to generate
        ' the EDI document
        oSchemas = oEdiDoc.GetSchemas
        oSchemas.EnableStandardReference = False

        ' The FORWARD-WRITE cursor increases the performance of processing the EDI
        ' document (see Technical Note 3 below).
        oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite

        oEdiDoc.Property(DocumentPropertyIDConstants.Property_DocumentBufferIO) = 200

        ' Terminators have to be specified. 
        oEdiDoc.SegmentTerminator = "'{13:10}"
        oEdiDoc.ElementTerminator = "+"
        oEdiDoc.CompositeTerminator = ":"
        oEdiDoc.ReleaseIndicator = "?"

        'Creates UNA segment
        oEdiDoc.Option(DocumentOptionIDConstants.OptDocument_ServiceSegment) = 1

        sEdiFileName = "ORDERS_D01B_Output.edi"
        sSefFileName = "ORDERS_D01B_EAN010.EVAL0.SEF"

        ' Specify SEF file to load. 
        oSchema = oEdiDoc.LoadSchema(sAppPath & sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format)


        oConnection = New SqlConnection(sConnection)
        oConnection.Open()

        sSql = "select * from [Interchange]"
        Dim oInterchangeAdapter As SqlDataAdapter = New SqlDataAdapter(sSql, oConnection)
        Dim oInterchangeDs As DataSet = New DataSet("dsInterchange")
        oInterchangeAdapter.Fill(oInterchangeDs, "dsInterchange")

        For Each oInterchangeRow As DataRow In oInterchangeDs.Tables("dsInterchange").Rows

            ' Creates the Interchange Control Header segment (UNB). 
            ediInterchange.Set(oInterchange, oEdiDoc.CreateInterchange("UN", "D01B"))
            ediDataSegment.Set(oSegment, oInterchange.GetDataSegmentHeader)
            oSegment.DataElementValue(1, 1) = oInterchangeRow("UNB01_01_SyntaxIdentifier").ToString()        ' Syntax identifier (0001) 
            oSegment.DataElementValue(1, 2) = oInterchangeRow("UNB01_02_SyntaxVersionNumber").ToString()       ' Syntax version number (0002) 
            oSegment.DataElementValue(2, 1) = oInterchangeRow("UNB02_01_InterchangeSenderIdentification").ToString()       ' Interchange sender identification (0004) 
            oSegment.DataElementValue(2, 2) = oInterchangeRow("UNB02_02_PartnerIdentificationCodeQualifier").ToString()         ' Partner identification code qualifier (0007) 
            oSegment.DataElementValue(2, 3) = oInterchangeRow("UNB02_03_InterchangeSenderInternalIdentification").ToString()
            oSegment.DataElementValue(3, 1) = oInterchangeRow("UNB03_01_RecipientIdentification").ToString()         ' Recipient identification (0010) 
            oSegment.DataElementValue(3, 2) = oInterchangeRow("UNB03_02_PartnerIdentificationCodeQualifier").ToString()         ' Partner identification code qualifier (0007) 
            oSegment.DataElementValue(3, 3) = oInterchangeRow("UNB03_03_RoutingAddress").ToString()      ' Routing address (0014) 
            oSegment.DataElementValue(4, 1) = oInterchangeRow("UNB04_01_Date").ToString()        ' Date (0017) 
            oSegment.DataElementValue(4, 2) = oInterchangeRow("UNB04_02_Time").ToString()        ' Time (0019) 
            oSegment.DataElementValue(5) = oInterchangeRow("UNB05_InterchangeControlReference").ToString()      ' Interchange control reference (0020) 
            oSegment.DataElementValue(9) = oInterchangeRow("UNB09_AcknowledgementRequest").ToString()      ' Acknowledgement request (0031) 
            oSegment.DataElementValue(11) = oInterchangeRow("UNB11_TestIndicator").ToString()         ' Test indicator (0035) 

            sSql = "select * from [Orders_Header] where InterchangeKey = " & oInterchangeRow("InterchangeKey").ToString()
            Dim oHeaderAdapter As SqlDataAdapter = New SqlDataAdapter(sSql, oConnection)
            Dim oHeaderDs As DataSet = New DataSet("dsHeader")
            oHeaderAdapter.Fill(oHeaderDs, "dsHeader")

            For Each oHeaderRow As DataRow In oHeaderDs.Tables("dsHeader").Rows

                ' Creates the Message header segment (UNH). 
                ediTransactionSet.Set(oMessage, oInterchange.CreateTransactionSet("ORDERS"))
                ediDataSegment.Set(oSegment, oMessage.GetDataSegmentHeader)
                oSegment.DataElementValue(1) = oHeaderRow("UNH01_MessageReferenceNumber").ToString()      ' Message reference number (0062) 
                oSegment.DataElementValue(2, 1) = oHeaderRow("UNH02_01_MessageTypeIdentifier").ToString()      ' Message type identifier (0065) 
                oSegment.DataElementValue(2, 2) = oHeaderRow("UNH02_02_MessageVersionNumber").ToString()       ' Message version number (0052) 
                oSegment.DataElementValue(2, 3) = oHeaderRow("UNH02_03_MessageReleaseNumber").ToString()         ' Message release number (0054) 
                oSegment.DataElementValue(2, 4) = oHeaderRow("UNH02_04_ControllingAgency").ToString()      ' Controlling agency (0051) 
                oSegment.DataElementValue(2, 5) = oHeaderRow("UNH02_05_AssociationAssignedCode").ToString()        ' Association assigned code (0057) 

                ' Beginning of message (BGM) 
                ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("BGM"))
                oSegment.DataElementValue(1, 1) = oHeaderRow("BGM01_01_DocumentMessageNameCoded").ToString()       ' Document/message name, coded (1001) 
                oSegment.DataElementValue(2, 1) = oHeaderRow("BGM02_01_DocumentMessageNumber").ToString()      ' Document/message number (1004) 
                oSegment.DataElementValue(3) = oHeaderRow("BGM03_MessageFunctionCoded").ToString()      ' Message function, coded (1225) 

                ' Date/time/period (DTM) - Delivery Requested 
                If oHeaderRow("DTM01_02_DeliveryDateTime").ToString().Trim() <> "" Then

                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("DTM"))
                    oSegment.DataElementValue(1, 1) = "2"       ' Date/time/period qualifier (2005) 
                    oSegment.DataElementValue(1, 2) = oHeaderRow("DTM01_02_DeliveryDateTime").ToString()      ' Date/time/period (2380) 
                    oSegment.DataElementValue(1, 3) = "102"       ' Date/time/period format qualifier (2379) 

                End If
                
                ' Date/time/period (DTM) - Document 
                If oHeaderRow("DTM01_02_DocumentDateTime").ToString().Trim() <> "" Then

                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("DTM"))
                    oSegment.DataElementValue(1, 1) = "137"       ' Date/time/period qualifier (2005) 
                    oSegment.DataElementValue(1, 2) = oHeaderRow("DTM01_02_DocumentDateTime").ToString()      ' Date/time/period (2380) 
                    oSegment.DataElementValue(1, 3) = "203"       ' Date/time/period format qualifier (2379) 

                End If
                
                ' Date/time/period (DTM) - Collection 
                If oHeaderRow("DTM01_02_CollectionDateTime").ToString().Trim() <> "" Then

                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("DTM"))
                    oSegment.DataElementValue(1, 1) = "235"       ' Date/time/period qualifier (2005) 
                    oSegment.DataElementValue(1, 2) = oHeaderRow("DTM01_02_CollectionDateTime").ToString()      ' Date/time/period (2380) 
                    oSegment.DataElementValue(1, 3) = "203"       ' Date/time/period format qualifier (2379) 

                End If
                
                ' Free text (FTX) 
                ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("FTX"))
                oSegment.DataElementValue(1) = oHeaderRow("FTX01_TextSubjectQualifier").ToString()        ' Text subject qualifier (4451) 
                oSegment.DataElementValue(2) = oHeaderRow("FTX02_TextFunctionCoded").ToString()      ' Text function, coded (4453) 
                oSegment.DataElementValue(4, 1) = oHeaderRow("FTX04_01_FreeText").ToString()      ' Free text (4440)

                ' Reference (RFF) 
                If oHeaderRow("RFF01_02_DeliveryScheduleNumber").ToString().Trim() <> "" Then
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("RFF\RFF"))
                    oSegment.DataElementValue(1, 1) = "AAN"         ' Reference qualifier (1153) 
                    oSegment.DataElementValue(1, 2) = oHeaderRow("RFF01_02_DeliveryScheduleNumber").ToString()      ' Reference number (1154) 

                End If
                

                'BUYER
                If oHeaderRow("NAD02_01_BuyerIdentification").ToString().Trim() <> "" Then

                    ' Name and address (NAD) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\NAD"))
                    oSegment.DataElementValue(1) = "BY"         ' Party qualifier (3035) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("NAD02_01_BuyerIdentification").ToString()      ' Party identification (3039) 
                    oSegment.DataElementValue(2, 3) = oHeaderRow("NAD02_03_BuyerAgencyCoded").ToString()       ' Code list responsible agency, coded (3055) 

                    ' Place/location identification (LOC) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\LOC"))
                    oSegment.DataElementValue(1) = "7"      ' Place/location qualifier (3227) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("LOC02_01_BuyerLocationIdentification").ToString()      ' Place/location identification (3225)
                    oSegment.DataElementValue(2, 3) = "92"      ' Code list responsible agency, coded (3055) 

                    ' Contact information (CTA) 
                    If oHeaderRow("CTA02_02_BuyerPurchasingContactName").ToString().Trim() <> "" Then
                        ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\CTA"))
                        oSegment.DataElementValue(1) = "PD"         ' Contact function, coded (3139) 
                        oSegment.DataElementValue(2, 1) = oHeaderRow("CTA02_01_BuyerPurchasingContactId").ToString()      ' Department or employee identification (3413) 
                        oSegment.DataElementValue(2, 2) = oHeaderRow("CTA02_02_BuyerPurchasingContactName").ToString()      ' Department or employee (3412) 

                        ' Communication contact (COM) - Email
                        If oHeaderRow("COM01_01_BuyerEmail").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_BuyerEmail").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "EM"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Fax
                        If oHeaderRow("COM01_01_BuyerFax").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_BuyerFax").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "FX"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Telephone
                        If oHeaderRow("COM01_01_BuyerTelephone").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_BuyerTelephone").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "TE"      ' Communication channel qualifier (3155) 
                        End If

                    End If ' Contact information (CTA) 

                End If 'BUYER

                'SHIP TO
                If oHeaderRow("NAD02_01_ShipToIdentification").ToString().Trim() <> "" Then

                    ' Name and address (NAD) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\NAD"))
                    oSegment.DataElementValue(1) = "ST"         ' Party qualifier (3035) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("NAD02_01_ShipToIdentification").ToString()      ' Party identification (3039) 
                    oSegment.DataElementValue(2, 3) = oHeaderRow("NAD02_03_ShipToAgencyCoded").ToString()       ' Code list responsible agency, coded (3055) 

                    ' Place/location identification (LOC) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\LOC"))
                    oSegment.DataElementValue(1) = "7"      ' Place/location qualifier (3227) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("LOC02_01_ShipToLocationIdentification").ToString()      ' Place/location identification (3225)
                    oSegment.DataElementValue(2, 3) = "92"      ' Code list responsible agency, coded (3055)

                    ' Contact information (CTA) 
                    If oHeaderRow("CTA02_02_ShipToPurchasingContactName").ToString().Trim() <> "" Then
                        ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\CTA"))
                        oSegment.DataElementValue(1) = "PD"         ' Contact function, coded (3139) 
                        oSegment.DataElementValue(2, 1) = oHeaderRow("CTA02_01_ShipToPurchasingContactId").ToString()      ' Department or employee identification (3413) 
                        oSegment.DataElementValue(2, 2) = oHeaderRow("CTA02_02_ShipToPurchasingContactName").ToString()      ' Department or employee (3412) 

                        ' Communication contact (COM) - Email
                        If oHeaderRow("COM01_01_ShipToEmail").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_ShipToEmail").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "EM"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Fax
                        If oHeaderRow("COM01_01_ShipToFax").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_ShipToFax").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "FX"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Telephone
                        If oHeaderRow("COM01_01_ShipToTelephone").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_ShipToTelephone").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "TE"      ' Communication channel qualifier (3155) 
                        End If

                    End If ' Contact information (CTA) 

                End If ' SHIP TO

                'SUPPLIER
                If oHeaderRow("NAD02_01_SupplierIdentification").ToString().Trim() <> "" Then

                    ' Name and address (NAD) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\NAD"))
                    oSegment.DataElementValue(1) = "SU"         ' Party qualifier (3035) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("NAD02_01_SupplierIdentification").ToString()      ' Party identification (3039) 
                    oSegment.DataElementValue(2, 3) = oHeaderRow("NAD02_03_SupplierAgencyCoded").ToString()       ' Code list responsible agency, coded (3055) 

                    ' Place/location identification (LOC) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\LOC"))
                    oSegment.DataElementValue(1) = "7"      ' Place/location qualifier (3227) 
                    oSegment.DataElementValue(2, 1) = oHeaderRow("LOC02_01_SupplierLocationIdentification").ToString()      ' Place/location identification (3225)
                    oSegment.DataElementValue(2, 3) = "92"      ' Code list responsible agency, coded (3055)

                    ' Contact information (CTA) 
                    If oHeaderRow("CTA02_02_SupplierPurchasingContactName").ToString().Trim() <> "" Then

                        ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\CTA"))
                        oSegment.DataElementValue(1) = "PD"         ' Contact function, coded (3139) 
                        oSegment.DataElementValue(2, 1) = oHeaderRow("CTA02_01_SupplierPurchasingContactId").ToString()      ' Department or employee identification (3413) 
                        oSegment.DataElementValue(2, 2) = oHeaderRow("CTA02_02_SupplierPurchasingContactName").ToString()      ' Department or employee (3412) 

                        ' Communication contact (COM) - Email
                        If oHeaderRow("COM01_01_SupplierEmail").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_SupplierEmail").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "EM"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Fax
                        If oHeaderRow("COM01_01_SupplierFax").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_SupplierFax").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "FX"      ' Communication channel qualifier (3155) 
                        End If

                        ' Communication contact (COM) - Telephone
                        If oHeaderRow("COM01_01_SupplierTelephone").ToString().Trim() <> "" Then
                            ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("NAD\CTA\COM"))
                            oSegment.DataElementValue(1, 1) = oHeaderRow("COM01_01_SupplierTelephone").ToString()      ' Communication number (3148) 
                            oSegment.DataElementValue(1, 2) = "TE"      ' Communication channel qualifier (3155) 
                        End If

                    End If ' Contact information (CTA) 

                End If 'SUPPLIER

                sSql = "select * from [Orders_Detail] where HeaderKey = " & oHeaderRow("HeaderKey").ToString()
                Dim oDetailAdapter As SqlDataAdapter = New SqlDataAdapter(sSql, oConnection)
                Dim oDetailDs As DataSet = New DataSet("dsDetail")
                oDetailAdapter.Fill(oDetailDs, "dsDetail")

                For Each oDetailRow As DataRow In oDetailDs.Tables("dsDetail").Rows

                    ' Line item (LIN) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\LIN"))
                    oSegment.DataElementValue(1) = oDetailRow("LIN01_LineItemNumber").ToString()         ' Line item number (1082) 
                    oSegment.DataElementValue(2) = "1"      ' Action request/notification, coded (1229) 
                    oSegment.DataElementValue(3, 1) = oDetailRow("LIN03_01_EAN_GlobalTradeItemNumber").ToString()      ' Item number (7140) 
                    oSegment.DataElementValue(3, 2) = "SRV"      ' Item number type, coded (7143) 

                    ' Additional product id (PIA) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\PIA"))
                    oSegment.DataElementValue(1) = oDetailRow("PIA01_ProductIdFunctionQualifier").ToString()      ' Product id. function qualifier (4347) 
                    oSegment.DataElementValue(2, 1) = oDetailRow("PIA02_01_ItemNumber").ToString()      ' Item number (7140) 
                    oSegment.DataElementValue(2, 2) = oDetailRow("PIA02_02_ItemNumberTypeCoded").ToString()      ' Item number type, coded (7143) 
                    oSegment.DataElementValue(3, 1) = oDetailRow("PIA03_01_ItemNumber").ToString()      ' Item number (7140) 
                    oSegment.DataElementValue(3, 2) = oDetailRow("PIA03_02_ItemNumberTypeCoded").ToString()      ' Item number type, coded (7143) 

                    ' Item description (IMD) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\IMD"))
                    oSegment.DataElementValue(1) = "F"      ' Item description type, coded (7077) 
                    oSegment.DataElementValue(3, 4) = oDetailRow("IMD03_04_ItemDescription").ToString()      ' Item description (7008)  

                    ' Measurements (MEA) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\MEA"))
                    oSegment.DataElementValue(1) = "PD"        ' Measurement purpose qualifier (6311) 
                    oSegment.DataElementValue(2, 1) = oDetailRow("MEA02_01_PropertyMeasuredCoded").ToString()       ' Property measured, coded (6313) 
                    oSegment.DataElementValue(3, 1) = "NAR"         ' Measure unit qualifier (6411) 
                    oSegment.DataElementValue(3, 2) = oDetailRow("MEA03_02_MeasurementValue").ToString()      ' Measurement value (6314) 

                    ' Quantity (QTY) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\QTY"))
                    oSegment.DataElementValue(1, 1) = oDetailRow("QTY01_01_QuantityQualifier").ToString()       ' Quantity qualifier (6063) 
                    oSegment.DataElementValue(1, 2) = oDetailRow("QTY01_02_QuantityInKilograms").ToString()      ' Quantity (6060) 
                    oSegment.DataElementValue(1, 3) = "KGM"         ' Measure unit qualifier (6411) 

                    ' Date/time/period (DTM) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\DTM"))
                    oSegment.DataElementValue(1, 1) = "364"       ' Date/time/period qualifier (2005) 
                    oSegment.DataElementValue(1, 2) = oDetailRow("DTM01_02_MinimumShelfLife").ToString()      ' Date/time/period (2380) 
                    oSegment.DataElementValue(1, 3) = oDetailRow("DTM01_03_DateTimePeriodFormatQualifier").ToString()       ' Date/time/period format qualifier (2379) 

                    ' Price details (PRI) 
                    ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("LIN\PRI\PRI"))
                    oSegment.DataElementValue(1, 1) = "1E"         ' Price qualifier (5125) 
                    oSegment.DataElementValue(1, 2) = oDetailRow("PRI01_02_NetPrice").ToString()      ' Price (5118) 

                Next ' For Each oDetailRow

                ' Section control (UNS) 
                ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("UNS"))
                oSegment.DataElementValue(1) = "S"      ' Section identification (0081) 

                ' Monetary amount (MOA) 
                ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("MOA"))
                oSegment.DataElementValue(1, 1) = "86"       ' Monetary amount type qualifier (5025) 
                oSegment.DataElementValue(1, 2) = oHeaderRow("MOA01_02_TotalMonetaryAmount").ToString()      ' Monetary amount (5004) 

                ' Control total (CNT) 
                ediDataSegment.Set(oSegment, oMessage.CreateDataSegment("CNT"))
                oSegment.DataElementValue(1, 1) = oHeaderRow("CNT01_01_ControlQualifier").ToString()       ' Control qualifier (6069) 
                oSegment.DataElementValue(1, 2) = oHeaderRow("CNT01_02_ControlValue").ToString()      ' Control value (6066) 

            Next ' For Each oHeaderRow

        Next ' For Each oInterchangeRow

        ' Trailing segments are automatically created when Framework EDI commits
        ' (saves) the instance of "oEdiDoc" object into an EDI file.
        oEdiDoc.Save(sAppPath & sEdiFileName)

        MessageBox.Show(oEdiDoc.GetEdiString(), sEdiFileName)

    End Sub

    Click here to download a trial version of the Framework EDI