Protected Sub btnGeneration_Click(sender As Object, e As EventArgs) Handles btnGeneration.Click
Dim oEdiDoc As ediDocument = Nothing
Dim oInterchange As ediInterchange = Nothing
Dim oGroup As ediGroup = Nothing
Dim oTransactionset As ediTransactionSet = Nothing
Dim oSegment As ediDataSegment = Nothing
Dim oSchemas As ediSchemas = Nothing
Dim i As Short
Dim sEdiString As String
Dim dtRow As Data.DataRow
Dim sEdiFile As String = Trim(Format(Now(), "yyMMddhhmmss")) & ".x12"
Dim sPath As String = AppDomain.CurrentDomain.BaseDirectory
'Instantiate oEdiDoc object
ediDocument.Set(oEdiDoc, New ediDocument)
'use CR/LF as segment terminator in EDI file
oEdiDoc.SegmentTerminator = vbCrLf
'change cursor type to forward write to use less memory and improve performance
oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite
'increase buffer so that hard drive does not become a 'bottle-neck'
oEdiDoc.Property(DocumentPropertyIDConstants.Property_DocumentBufferIO) = 200
ediSchemas.Set(oSchemas, oEdiDoc.GetSchemas)
oSchemas.EnableStandardReference = False
'load standard exchange format (SEF) file
oEdiDoc.LoadSchema(sPath & "files\810_004010.SEF", SchemaTypeIDConstants.Schema_Standard_Exchange_Format)
'Creating ISA segment. In this example, the values are fixed.
ediInterchange.Set(oInterchange, oEdiDoc.CreateInterchange("X", "004010"))
ediDataSegment.Set(oSegment, oInterchange.GetDataSegmentHeader)
oSegment.DataElementValue(1) = "00" ' Authorization Information Qualifier (I01)
'oSegment.DataElementValue(2) = "" ' Authorization Information (I02)
oSegment.DataElementValue(3) = "00" ' Security Information Qualifier (I03)
'oSegment.DataElementValue(4) = "" ' Security Information (I04)
oSegment.DataElementValue(5) = "ZZ" ' Interchange ID Qualifier (I05)
oSegment.DataElementValue(6) = "SENDER ID" ' Interchange Sender ID (I06)
oSegment.DataElementValue(7) = "ZZ" ' Interchange ID Qualifier (I05)
oSegment.DataElementValue(8) = "RECEIVER ID" ' Interchange Receiver ID (I07)
oSegment.DataElementValue(9) = "010101" ' Interchange Date (I08)
oSegment.DataElementValue(10) = "0101" ' Interchange Time (I09)
oSegment.DataElementValue(11) = "U" ' Interchange Control Standards Identifier (I10)
oSegment.DataElementValue(12) = "00401" ' Interchange Control Version Number (I11)
oSegment.DataElementValue(13) = "000000001" ' Interchange Control Number (I12)
oSegment.DataElementValue(14) = "0" ' Acknowledgment Requested (I13)
oSegment.DataElementValue(15) = "T" ' Usage Indicator (I14)
oSegment.DataElementValue(16) = "!" ' Component Element Separator (I15)
'Creating GS segment
ediGroup.Set(oGroup, oInterchange.CreateGroup("004010"))
ediDataSegment.Set(oSegment, oGroup.GetDataSegmentHeader)
oSegment.DataElementValue(1) = "IN" ' Functional Identifier Code (479)
oSegment.DataElementValue(2) = "SENDER ID" ' Application Sender's Code (142)
oSegment.DataElementValue(3) = "APP RECEIVER" ' Application Receiver's Code (124)
oSegment.DataElementValue(4) = "01010101" ' Date (373)
oSegment.DataElementValue(5) = "01010101" ' Time (337)
oSegment.DataElementValue(6) = "1" ' Group Control Number (28)
oSegment.DataElementValue(7) = "X" ' Responsible Agency Code (455)
oSegment.DataElementValue(8) = "004010" ' Version / Release / Industry Identifier Code (480)
'Creating ST segment
ediTransactionSet.Set(oTransactionset, oGroup.CreateTransactionSet("810"))
ediDataSegment.Set(oSegment, oTransactionset.GetDataSegmentHeader)
'oSegment.DataElementValue(1) = "810" ' Transaction Set Identifier Code (143)
oSegment.DataElementValue(2) = "0001" ' Transaction Set Control Number (329)
'Creating BIG segment
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("BIG"))
oSegment.DataElementValue(1) = "20021208" ' Date (373)
oSegment.DataElementValue(2) = "00001" ' Invoice Number (76)
oSegment.DataElementValue(4) = "A999" ' Purchase Order Number (324)
'Creating N1 Loops
'Ship To information
'Creating the N1 data segment in the N1 loop (group)
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N1"))
oSegment.DataElementValue(1) = "ST" ' Entity Identifier Code (98)
oSegment.DataElementValue(2) = txtShipToCompany.Text ' Name (93)
oSegment.DataElementValue(3) = "9" ' Identification Code Qualifier (66)
oSegment.DataElementValue(4) = "122334455" ' Identification Code (67)
'Creating the N3 data segment in the N1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N3"))
oSegment.DataElementValue(1) = txtShipToAddr.Text ' Address Information (166)
'Creating the N4 data segment in the N1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N4"))
oSegment.DataElementValue(1) = txtShipToCity.Text ' City Name (19)
oSegment.DataElementValue(2) = txtShipToState.Text ' State or Province Code (156)
oSegment.DataElementValue(3) = txtShipToZip.Text ' Postal Code (116)
'Creating a second instance of the N1 loop
'Bill-to information
'Creating the N1 data segment in the second instance of the N1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N1"))
oSegment.DataElementValue(1) = "BT"
oSegment.DataElementValue(2) = txtBillToCompany.Text
oSegment.DataElementValue(3) = "9"
oSegment.DataElementValue(4) = "122334455"
'Creating the N3 data segment in the second instance of the N1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N3"))
oSegment.DataElementValue(1) = txtBillToAddr.Text
'Creating the N4 data segment in the second instance of the N1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("N1\N4"))
oSegment.DataElementValue(1) = txtBillToCity.Text
oSegment.DataElementValue(2) = txtBillToState.Text
oSegment.DataElementValue(3) = txtBillToZip.Text
'Terms
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("ITD"))
oSegment.DataElementValue(1) = "01" ' Terms Type Code (336)
oSegment.DataElementValue(2) = "3" ' Terms Basis Date Code (333)
oSegment.DataElementValue(3) = "2" ' Terms Discount Percent (338)
oSegment.DataElementValue(5) = "30" ' Terms Discount Days Due (351)
oSegment.DataElementValue(7) = "30" ' Terms Net Days (386)
oSegment.DataElementValue(12) = "60" ' Description (352)
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("FOB"))
oSegment.DataElementValue(1) = "PP" ' Shipment Method of Payment (146)
'Creating the IT1 Loop
For i = 1 To 2
dtRow = dt.Rows(i - 1)
'Creating the IT1 data segment in the IT1 loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("IT1\IT1"))
oSegment.DataElementValue(2) = dtRow("Qty") ' Quantity Invoiced (358)
oSegment.DataElementValue(3) = dtRow("Unit") ' Unit or Basis for Measurement Code (355)
oSegment.DataElementValue(4) = dtRow("Price") ' Unit Price (212)
oSegment.DataElementValue(6) = "UA" ' Product/Service ID Qualifier (235)
oSegment.DataElementValue(7) = "EAN" ' Product/Service ID (234)
'Creating the PID data segment in the IT1\PID loop
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("IT1\PID\PID"))
oSegment.DataElementValue(1) = "F" ' Item Description Type (349)
oSegment.DataElementValue(5) = dtRow("Desc") ' Description (352)
Next
'Creating the TDS segment
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("TDS"))
oSegment.DataElementValue(1) = 20 * 100
'Creating the CAD segment
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CAD"))
oSegment.DataElementValue(5) = "Routing"
'Creating the ISS segment in Loop ISS, then populates its elements
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("ISS\ISS"))
oSegment.DataElementValue(1) = "30"
oSegment.DataElementValue(2) = "CA"
'Creating the CTT segment
ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CTT"))
oSegment.DataElementValue(1) = 50
'EDI file can be saved to a file
oEdiDoc.Save(sPath & "edi\" & sEdiFile)
'gets the EDI file as a string
sEdiString = oEdiDoc.GetEdiString(0)
'display EDI string
txtEdiString.Text = sEdiString
End Sub