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 oGroup As ediGroup Dim oTransactionSet As ediTransactionSet Dim oSegment As ediDataSegment Dim sSql As String Dim sEdiFileName As String Dim sSefFileName 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 = "!" sEdiFileName = "810_004010_output.X12" sSefFileName = "810_004010.EVAL0.SEF" 'EVALUATION SEF FILE ' 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 (ISA). oInterchange = oEdiDoc.CreateInterchange("X", "004010") oSegment = oInterchange.GetDataSegmentHeader oSegment.DataElementValue(1) = oInterchangeRow("ISA01_AuthorizationInformationQualifier").ToString() ' Authorization Information Qualifier (I01) oSegment.DataElementValue(2) = oInterchangeRow("ISA02_AuthorizationInformation").ToString() ' Authorization Information (I02) oSegment.DataElementValue(3) = oInterchangeRow("ISA03_SecurityInformationQualifier").ToString() ' Security Information Qualifier (I03) oSegment.DataElementValue(4) = oInterchangeRow("ISA04_SecurityInformation").ToString() ' Security Information (I04) oSegment.DataElementValue(5) = oInterchangeRow("ISA05_InterchangeIdQualifier").ToString() ' Interchange ID Qualifier (I05) oSegment.DataElementValue(6) = oInterchangeRow("ISA06_InterchangeSenderId").ToString() ' Interchange Sender ID (I06) oSegment.DataElementValue(7) = oInterchangeRow("ISA07_InterchangeIdQualifier").ToString() ' Interchange ID Qualifier (I05) oSegment.DataElementValue(8) = oInterchangeRow("ISA08_InterchangeReceiverId").ToString() ' Interchange Receiver ID (I07) oSegment.DataElementValue(9) = oInterchangeRow("ISA09_InterchangeDate").ToString() ' Interchange Date (I08) oSegment.DataElementValue(10) = oInterchangeRow("ISA10_InterchangeTime").ToString() ' Interchange Time (I09) oSegment.DataElementValue(11) = oInterchangeRow("ISA11_InterchangeControlStandardsIdentifier").ToString() ' Interchange Control Standards Identifier (I10) oSegment.DataElementValue(12) = oInterchangeRow("ISA12_InterchangeControlVersionNumber").ToString() ' Interchange Control Version Number (I11) oSegment.DataElementValue(13) = oInterchangeRow("ISA13_InterchangeControlNumber").ToString() ' Interchange Control Number (I12) oSegment.DataElementValue(14) = oInterchangeRow("ISA14_AcknowledgmentRequested").ToString() ' Acknowledgment Requested (I13) oSegment.DataElementValue(15) = oInterchangeRow("ISA15_UsageIndicator").ToString() ' Usage Indicator (I14) oSegment.DataElementValue(16) = oInterchangeRow("ISA16_ComponentElementSeparator").ToString() ' Component Element Separator (I15) sSql = "select * from [FunctionalGroup] where InterchangeKey = " & oInterchangeRow("InterchangeKey").ToString() Dim oGroupAdapter As SqlDataAdapter = New SqlDataAdapter(sSql, oConnection) Dim oGroupDs As DataSet = New DataSet("dsGroup") oGroupAdapter.Fill(oGroupDs, "dsGroup") For Each oGroupRow As DataRow In oGroupDs.Tables("dsGroup").Rows ' Creates the Functional Group Header segment (GS). oGroup = oInterchange.CreateGroup("004010") oSegment = oGroup.GetDataSegmentHeader oSegment.DataElementValue(1) = oGroupRow("GS01_FunctionalIdentifierCode").ToString() ' Functional Identifier Code (479) oSegment.DataElementValue(2) = oGroupRow("GS02_ApplicationSendersCode").ToString() ' Application Sender's Code (142) oSegment.DataElementValue(3) = oGroupRow("GS03_ApplicationReceiversCode").ToString() ' Application Receiver's Code (124) oSegment.DataElementValue(4) = oGroupRow("GS04_Date").ToString() ' Date (373) oSegment.DataElementValue(5) = oGroupRow("GS05_Time").ToString() ' Time (337) oSegment.DataElementValue(6) = oGroupRow("GS06_GroupControlNumber").ToString() ' Group Control Number (28) oSegment.DataElementValue(7) = oGroupRow("GS07_ResponsibleAgencyCode").ToString() ' Responsible Agency Code (455) oSegment.DataElementValue(8) = oGroupRow("GS08_VersionReleaseIndustryIdentifierCode").ToString() ' Version / Release / Industry Identifier Code (480) sSql = "select * from [TS810_Header] where GroupKey = " & oGroupRow("GroupKey").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 ' TRANSACTION SET ID 810 - Invoice ' Creates the Transaction Set header segment (ST). oTransactionSet = oGroup.CreateTransactionSet("810") oSegment = oTransactionSet.GetDataSegmentHeader oSegment.DataElementValue(1) = oHeaderRow("ST01_TransactionSetIdentifierCode").ToString() ' Transaction Set Identifier Code (143) oSegment.DataElementValue(2) = oHeaderRow("ST02_TransactionSetControlNumber").ToString() ' Transaction Set Control Number (329) ' Beginning Segment for Invoice (BIG) oSegment = oTransactionSet.CreateDataSegment("BIG") oSegment.DataElementValue(1) = oHeaderRow("BIG01_InvoiceDate").ToString() ' Date (373) oSegment.DataElementValue(2) = oHeaderRow("BIG02_InvoiceNumber").ToString() ' Invoice Number (76) oSegment.DataElementValue(3) = oHeaderRow("BIG03_PurchaseOrderDate").ToString() ' Date (373) oSegment.DataElementValue(4) = oHeaderRow("BIG04_PurchaseOrderNumber").ToString() ' Purchase Order Number (324) oSegment.DataElementValue(7) = oHeaderRow("BIG07_TransactionTypeCode").ToString() ' Transaction Type Code (640) 'BILLTO If oHeaderRow("N102_BillToName").ToString().Trim() <> "" Then oSegment = oTransactionSet.CreateDataSegment("N1\N1") oSegment.DataElementValue(1) = "BT" ' Entity Identifier Code (98) oSegment.DataElementValue(2) = oHeaderRow("N102_BillToName").ToString() ' Name (93) oSegment.DataElementValue(3) = "1" ' Identification Code Qualifier (66) oSegment.DataElementValue(4) = oHeaderRow("N104_BillToIdentificationCode").ToString() ' Identification Code (67) ' Address Information (N3) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N3")) oSegment.DataElementValue(1) = oHeaderRow("N301_BillToAddressInformation").ToString() ' Address Information (166) ' Geographic Location (N4) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N4")) oSegment.DataElementValue(1) = oHeaderRow("N401_BillToCityName").ToString() ' City Name (19) oSegment.DataElementValue(2) = oHeaderRow("N402_BillToStateOrProvinceCode").ToString() ' State or Province Code (156) oSegment.DataElementValue(3) = oHeaderRow("N403_BillToPostalCode").ToString() ' Postal Code (116) End If 'REMIT TO If oHeaderRow("N102_RemitToName").ToString().Trim() <> "" Then oSegment = oTransactionSet.CreateDataSegment("N1\N1") oSegment.DataElementValue(1) = "RE" ' Entity Identifier Code (98) oSegment.DataElementValue(2) = oHeaderRow("N102_RemitToName").ToString() ' Name (93) oSegment.DataElementValue(3) = "1" ' Identification Code Qualifier (66) oSegment.DataElementValue(4) = oHeaderRow("N104_RemitToIdentificationCode").ToString() ' Identification Code (67) ' Address Information (N3) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N3")) oSegment.DataElementValue(1) = oHeaderRow("N301_RemitToAddressInformation").ToString() ' Address Information (166) ' Geographic Location (N4) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N4")) oSegment.DataElementValue(1) = oHeaderRow("N401_RemitToCityName").ToString() ' City Name (19) oSegment.DataElementValue(2) = oHeaderRow("N402_RemitToStateOrProvinceCode").ToString() ' State or Province Code (156) oSegment.DataElementValue(3) = oHeaderRow("N403_RemitToPostalCode").ToString() ' Postal Code (116) End If 'SHIP TO If oHeaderRow("N102_ShipToName").ToString().Trim() <> "" Then oSegment = oTransactionSet.CreateDataSegment("N1\N1") oSegment.DataElementValue(1) = "ST" ' Entity Identifier Code (98) oSegment.DataElementValue(2) = oHeaderRow("N102_ShipToName").ToString() ' Name (93) oSegment.DataElementValue(3) = "1" ' Identification Code Qualifier (66) oSegment.DataElementValue(4) = oHeaderRow("N104_ShipToIdentificationCode").ToString() ' Identification Code (67) ' Address Information (N3) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N3")) oSegment.DataElementValue(1) = oHeaderRow("N301_ShipToAddressInformation").ToString() ' Address Information (166) ' Geographic Location (N4) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N4")) oSegment.DataElementValue(1) = oHeaderRow("N401_ShipToCityName").ToString() ' City Name (19) oSegment.DataElementValue(2) = oHeaderRow("N402_ShipToStateOrProvinceCode").ToString() ' State or Province Code (156) oSegment.DataElementValue(3) = oHeaderRow("N403_ShipToPostalCode").ToString() ' Postal Code (116) End If 'SHIP FROM If oHeaderRow("N102_ShipFromName").ToString().Trim() <> "" Then oSegment = oTransactionSet.CreateDataSegment("N1\N1") oSegment.DataElementValue(1) = "SF" ' Entity Identifier Code (98) oSegment.DataElementValue(2) = oHeaderRow("N102_ShipFromName").ToString() ' Name (93) oSegment.DataElementValue(3) = "1" ' Identification Code Qualifier (66) oSegment.DataElementValue(4) = oHeaderRow("N104_ShipFromIdentificationCode").ToString() ' Identification Code (67) ' Address Information (N3) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N3")) oSegment.DataElementValue(1) = oHeaderRow("N301_ShipFromAddressInformation").ToString() ' Address Information (166) ' Geographic Location (N4) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N4")) oSegment.DataElementValue(1) = oHeaderRow("N401_ShipFromCityName").ToString() ' City Name (19) oSegment.DataElementValue(2) = oHeaderRow("N402_ShipFromStateOrProvinceCode").ToString() ' State or Province Code (156) oSegment.DataElementValue(3) = oHeaderRow("N403_ShipFromPostalCode").ToString() ' Postal Code (116) ' Reference Identification (REF) If oHeaderRow("REF02_StoreNumber").ToString().Trim() <> "" Then ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\REF")) oSegment.DataElementValue(1) = "ST" ' Reference Identification Qualifier (128) oSegment.DataElementValue(2) = oHeaderRow("REF02_StoreNumber").ToString() ' Reference Identification (127) End If End If 'VENDOR If oHeaderRow("N102_VendorName").ToString().Trim() <> "" Then oSegment = oTransactionSet.CreateDataSegment("N1\N1") oSegment.DataElementValue(1) = "VN" ' Entity Identifier Code (98) oSegment.DataElementValue(2) = oHeaderRow("N102_VendorName").ToString() ' Name (93) oSegment.DataElementValue(3) = "1" ' Identification Code Qualifier (66) oSegment.DataElementValue(4) = oHeaderRow("N104_VendorIdentificationCode").ToString() ' Identification Code (67) ' Address Information (N3) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N3")) oSegment.DataElementValue(1) = oHeaderRow("N301_VendorAddressInformation").ToString() ' Address Information (166) ' Geographic Location (N4) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\N4")) ' Syntax Rule: If 06 is present, then 05 is required. oSegment.DataElementValue(1) = oHeaderRow("N401_VendorCityName").ToString() ' City Name (19) oSegment.DataElementValue(2) = oHeaderRow("N402_VendorStateOrProvinceCode").ToString() ' State or Province Code (156) oSegment.DataElementValue(3) = oHeaderRow("N403_VendorPostalCode").ToString() ' Postal Code (116) ' Reference Identification (REF) If oHeaderRow("REF02_VendorOrderNumber").ToString().Trim() <> "" Then ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("N1\REF")) oSegment.DataElementValue(1) = "VN" ' Reference Identification Qualifier (128) oSegment.DataElementValue(2) = oHeaderRow("REF02_VendorOrderNumber").ToString() ' Reference Identification (127) End If End If ' Terms of Sale/Deferred Terms of Sale (ITD) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("ITD")) oSegment.DataElementValue(1) = oHeaderRow("ITD01_TermsTypeCode").ToString() ' Terms Type Code (336) oSegment.DataElementValue(2) = "3" ' Terms Basis Date Code (333) oSegment.DataElementValue(3) = oHeaderRow("ITD03_TermsDiscountPercent").ToString() ' Terms Discount Percent (338) oSegment.DataElementValue(5) = oHeaderRow("ITD05_TermsDiscountDaysDue").ToString() ' Terms Discount Days Due (351) oSegment.DataElementValue(7) = oHeaderRow("ITD07_TermsNetDays").ToString() ' Terms Net Days (386) oSegment.DataElementValue(12) = oHeaderRow("ITD12_Description").ToString() ' Description (352) ' Date/Time Reference (DTM) If oHeaderRow("DTM02_ShippedDate").ToString().Trim() <> "" Then ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("DTM")) oSegment.DataElementValue(1) = "0011" ' Date/Time Qualifier (374) oSegment.DataElementValue(2) = oHeaderRow("DTM02_ShippedDate").ToString() ' Date (373) End If ' F.O.B. Related Instructions (FOB) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("FOB")) oSegment.DataElementValue(1) = oHeaderRow("FOB01_ShipmentMethodOfPayment").ToString() ' Shipment Method of Payment (146) sSql = "select * from [TS810_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 ' Baseline Item Data (Invoice) (IT1) oSegment = oTransactionSet.CreateDataSegment("IT1\IT1") oSegment.DataElementValue(2) = oDetailRow("IT102_QuantityInvoiced").ToString() ' Quantity Invoiced (358) oSegment.DataElementValue(3) = oDetailRow("IT103_UnitOrBasisForMeasurementCode").ToString() ' Unit or Basis for Measurement Code (355) oSegment.DataElementValue(4) = oDetailRow("IT104_UnitPrice").ToString() ' Unit Price (212) oSegment.DataElementValue(6) = oDetailRow("IT106_ProductServiceIdQualifier").ToString() ' Product/Service ID Qualifier (235) oSegment.DataElementValue(7) = oDetailRow("IT107_ProductServiceId").ToString() ' Product/Service ID (234) ' Product/Item Description (PID) oSegment = oTransactionSet.CreateDataSegment("IT1\PID\PID") oSegment.DataElementValue(1) = "F" ' Item Description Type (349) oSegment.DataElementValue(5) = oDetailRow("PID05_Description").ToString() ' Description (352) ' Service, Promotion, Allowance, or Charge Information (SAC) oSegment = oTransactionSet.CreateDataSegment("IT1\SAC\SAC") oSegment.DataElementValue(1) = oDetailRow("SAC01_AllowanceOrChargeIndicator").ToString() ' Allowance or Charge Indicator (248) oSegment.DataElementValue(2) = oDetailRow("SAC02_ServicePromotionAllowanceOrChargeCode").ToString() ' Service, Promotion, Allowance, or Charge Code (1300) oSegment.DataElementValue(5) = oDetailRow("SAC05_Amount").ToString() ' Amount (610) oSegment.DataElementValue(12) = oDetailRow("SAC12_AllowanceOrChargeMethodOfHandlingCode").ToString() ' Allowance or Charge Method of Handling Code (331) oSegment.DataElementValue(15) = oDetailRow("SAC15_Description").ToString() ' Description (352) Next ' For Each oDetailRow ' Total Monetary Value Summary (TDS) oSegment = oTransactionSet.CreateDataSegment("TDS") oSegment.DataElementValue(1) = oHeaderRow("TDS01_TotalAmount").ToString() ' Amount (610) ' Carrier Detail (CAD) ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("CAD")) oSegment.DataElementValue(5) = oHeaderRow("CAD05_CarrierRouting").ToString() ' Routing (387) oSegment = oTransactionSet.CreateDataSegment("SAC\SAC") oSegment.DataElementValue(1) = oHeaderRow("SAC01_AllowanceOrChargeIndicator").ToString() ' Allowance or Charge Indicator (248) oSegment.DataElementValue(2) = oHeaderRow("SAC02_ServicePromotionAllowanceOrChargeCode").ToString() ' Service, Promotion, Allowance, or Charge Code (1300) oSegment.DataElementValue(5) = oHeaderRow("SAC05_Amount").ToString() ' Amount (610) oSegment.DataElementValue(12) = oHeaderRow("SAC12_AllowanceOrChargeMethodOfHandlingCode").ToString() ' Allowance or Charge Method of Handling Code (331) oSegment.DataElementValue(15) = oHeaderRow("SAC15_Description").ToString() ' Description (352) oSegment = oTransactionSet.CreateDataSegment("ISS\ISS") oSegment.DataElementValue(1) = oHeaderRow("ISS01_NumberOfUnitsShipped").ToString() ' Number of Units Shipped (382) oSegment.DataElementValue(2) = "CA" ' Unit or Basis for Measurement Code (355) Next ' For Each oHeaderRow Next ' For Each oGroupRow 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