private void btnGenerate_Click(object sender, System.EventArgs e)
        {
            //This is just an example program to demonstrate how to generate an EDI 856 file in C#
            // using the Framework EDI component

            ediDocument oEdiDoc = null;
            ediSchema oSchema = null;
            ediSchemas oSchemas = null;
            ediInterchange oInterchange = null;
            ediGroup oGroup = null;
            ediTransactionSet oTransactionset = null;
            ediDataSegment oSegment = null;
            
            string sPath = AppDomain.CurrentDomain.BaseDirectory;

            string sSefFile = "856_X12-4010.SEF";
            string sEdiFile = "856OUTPUT.X12";

 
            Cursor = Cursors.WaitCursor;

            //CREATES OEDIDOC OBJECT
            ediDocument.Set(ref oEdiDoc, new ediDocument());
 
            //THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES 
            //NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO GENERATE THE EDI FILE.
            ediSchemas.Set(ref oSchemas,(ediSchemas) oEdiDoc.GetSchemas());
            oSchemas.EnableStandardReference = false;
 
            //ENABLES FORWARD WRITE, AND INCREASES BUFFER I/O TO IMPROVE PERFORMANCE
            oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite;
            oEdiDoc.set_Property(DocumentPropertyIDConstants.Property_DocumentBufferIO, 2000);
 
            //SET TERMINATORS
            oEdiDoc.SegmentTerminator = "~{13:10}";
            oEdiDoc.ElementTerminator = "*";
            oEdiDoc.CompositeTerminator = ">";
 
            //LOADS THE SEF FILE
            ediSchema.Set(ref oSchema, (ediSchema) oEdiDoc.LoadSchema(sPath + sSefFile, 0));
 
            //CREATES THE ISA SEGMENT
            ediInterchange.Set(ref oInterchange, (ediInterchange) oEdiDoc.CreateInterchange("X","004010"));
            ediDataSegment.Set(ref oSegment, (ediDataSegment) oInterchange.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1,0,"00");     //Authorization Information Qualifier
            oSegment.set_DataElementValue(2,0,"          ");     //Authorization Information
            oSegment.set_DataElementValue(3,0,"00");     //Security Information Qualifier
            oSegment.set_DataElementValue(4,0,"          ");     //Security Information
            oSegment.set_DataElementValue(5,0,"14");     //Interchange ID Qualifier
            oSegment.set_DataElementValue(6,0,"0073268795005  ");     //Interchange Sender ID
            oSegment.set_DataElementValue(7,0,"ZZ");     //Interchange ID Qualifier
            oSegment.set_DataElementValue(8,0,"RECEIVERISA    ");     //Interchange Receiver ID
            oSegment.set_DataElementValue(9,0,"960807");     //Interchange Date
            oSegment.set_DataElementValue(10,0,"1548");     //Interchange Time
            oSegment.set_DataElementValue(11,0,"U");     //Interchange Control Standards Identifier
            oSegment.set_DataElementValue(12,0,"00401");     //Interchange Control Version Number
            oSegment.set_DataElementValue(13,0,"000000020");     //Interchange Control Number
            oSegment.set_DataElementValue(14,0,"0");     //Acknowledgment Requested
            oSegment.set_DataElementValue(15,0,"T");     //Usage Indicator
            oSegment.set_DataElementValue(16,0,">");     //Component Element Separator
 
            //CREATES THE GS SEGMENT
            ediGroup.Set(ref oGroup, (ediGroup) oInterchange.CreateGroup("004010"));
            ediDataSegment.Set( ref oSegment, (ediDataSegment) oGroup.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1,0,"SH");     //Functional Identifier Code
            oSegment.set_DataElementValue(2,0,"007326879");     //Application Sender's Code
            oSegment.set_DataElementValue(3,0,"RECEIVERGS");     //Application Receiver's Code
            oSegment.set_DataElementValue(4,0,"19960807");     //Date
            oSegment.set_DataElementValue(5,0,"1548");     //Time
            oSegment.set_DataElementValue(6,0,"1");     //Group Control Number
            oSegment.set_DataElementValue(7,0,"X");     //Responsible Agency Code
            oSegment.set_DataElementValue(8,0,"004010");     //Version / Release / Industry Identifier Code
 
            //CREATES THE ST SEGMENT
            ediTransactionSet.Set(ref oTransactionset, (ediTransactionSet) oGroup.CreateTransactionSet("856"));
            ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1,0,"856");     //Transaction Set Identifier Code
            oSegment.set_DataElementValue(2,0,"00001");     //Transaction Set Control Number
 
            //BSN - BEGINNING SEGMENT FOR SHIP NOTICE
            ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("BSN"));
            oSegment.set_DataElementValue(1,0,"00");     //Transaction Set Purpose Code
            oSegment.set_DataElementValue(2,0,txtShipmentID.Text );     //Shipment Identification
            oSegment.set_DataElementValue(3,0,"20020301");     //Date
            oSegment.set_DataElementValue(4,0,"1340");     //Time
            oSegment.set_DataElementValue(5,0,"0002");     //Hierarchical Structure Code


            // hypothetical numbers of shipments, orders and items
            int nShipmentCounter  = 1;
            int nShipments  = 1;
            int nOrderCounter  = 1;
            int nOrders  = 1;
            int nItemCounter  = 1;
            int nItems  = 6;

            int nHlCounter = 0;
            int nHlOrderParent ;
            int nHlItemParent ;

            //********************************************************************************************
            //HL - HIERARCHICAL LEVEL - SHIPMENTS ********************************************************
            while (nShipmentCounter <= nShipments)
            {
                nHlCounter = nHlCounter + 1;
                nHlOrderParent = nHlCounter;

                //HL - HIERARCHICAL LEVEL
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\HL"));
                oSegment.set_DataElementValue(1,0,nHlCounter.ToString() );     //Hierarchical ID Number
                oSegment.set_DataElementValue(3,0,"S");     //Hierarchical Level Code
                oSegment.set_DataElementValue(4,0,"1");     //Hierarchical Child Code
 
                //TD1 - CARRIER DETAILS (QUANTITY AND WEIGHT)
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\TD1"));
                oSegment.set_DataElementValue(1,0,"TKT");     //Packaging Code
                oSegment.set_DataElementValue(2,0,"207");     //Lading Quantity
                oSegment.set_DataElementValue(6,0,"A3");     //Weight Qualifier
                oSegment.set_DataElementValue(7,0,"46.09");     //Weight
                oSegment.set_DataElementValue(8,0,"01");     //Unit or Basis for Measurement Code
 
                //TD5 - CARRIER DETAILS (ROUTING SEQUENCE/TRANSIT TIME)
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\TD5"));
                oSegment.set_DataElementValue(2,0,"2");     //Identification Code Qualifier
                oSegment.set_DataElementValue(3,0,"IDCODE");     //Identification Code
                oSegment.set_DataElementValue(4,0,"M");     //Transportation Method/Type Code
                oSegment.set_DataElementValue(5,0,"FREEFORM");     //Routing
 
                //TD3 - CARRIER DETAILS (EQUIPMENT)
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\TD3"));
                oSegment.set_DataElementValue(1,0,"TN");     //Equipment Description Code
                oSegment.set_DataElementValue(2,0,"BPL3");     //Equipment Initial
                oSegment.set_DataElementValue(3,0,"777777");     //Equipment Number
 
                //REF - REFERENCE IDENTIFICATION
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\REF"));
                oSegment.set_DataElementValue(1,0,"BM");     //Reference Identification Qualifier
                oSegment.set_DataElementValue(2,0,"33333");     //Reference Identification
 
                //DTM - DATE/TIME REFERENCE
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\DTM"));
                oSegment.set_DataElementValue(1,0,"011");     //Date/Time Qualifier
                oSegment.set_DataElementValue(2,0,"19971211");     //Date
 
                //DTM - DATE/TIME REFERENCE
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\DTM(2)"));
                oSegment.set_DataElementValue(1,0,"017");     //Date/Time Qualifier
                oSegment.set_DataElementValue(2,0,"19971211");     //Date
 
                //N1 - NAME
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1\\N1"));
                oSegment.set_DataElementValue(1,0,"BT");     //Entity Identifier Code
                oSegment.set_DataElementValue(2,0,"BUYSNACKS");     //Name
                oSegment.set_DataElementValue(3,0,"1");     //Identification Code Qualifier
                oSegment.set_DataElementValue(4,0,"1223334444");     //Identification Code
 
                //N3 - ADDRESS INFORMATION
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1\\N3"));
                oSegment.set_DataElementValue(1,0,"P.O. BOX 0000");     //Address Information
 
                //N4 - GEOGRAPHIC LOCATION
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1\\N4"));
                oSegment.set_DataElementValue(1,0,"TEMPLE");     //City Name
                oSegment.set_DataElementValue(2,0,"TX");     //State or Province Code
                oSegment.set_DataElementValue(3,0,"76503");     //Postal Code
 
                //N1 - NAME
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1(2)\\N1"));
                oSegment.set_DataElementValue(1,0,"ST");     //Entity Identifier Code
                oSegment.set_DataElementValue(2,0,"BUYSNACKS PORT");     //Name
                oSegment.set_DataElementValue(3,0,"1");     //Identification Code Qualifier
                oSegment.set_DataElementValue(4,0,"1223334445");     //Identification Code
 
                //N3 - ADDRESS INFORMATION
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1(2)\\N3"));
                oSegment.set_DataElementValue(1,0,"1000 N. SAMPLE HIGHWAY");     //Address Information
 
                //N4 - GEOGRAPHIC LOCATION
                ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\N1(2)\\N4"));
                oSegment.set_DataElementValue(1,0,"ATHENS");     //City Name
                oSegment.set_DataElementValue(2,0,"GA");     //State or Province Code
                oSegment.set_DataElementValue(3,0,"30603");     //Postal Code
 
                //********************************************************************************************
                //HL - HIERARCHICAL LEVEL - ORDER ************************************************************
                while (nOrderCounter <= nOrders)
                {
                    nHlCounter = nHlCounter + 1;
                    nHlItemParent = nHlCounter;

                    //HL - HIERARCHICAL LEVEL
                    ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\HL"));
                    oSegment.set_DataElementValue(1,0,nHlCounter.ToString() );     //Hierarchical ID Number
                    oSegment.set_DataElementValue(2,0,nHlOrderParent.ToString() );     //Hierarchical Parent ID Number
                    oSegment.set_DataElementValue(3,0,"O");     //Hierarchical Level Code
                    oSegment.set_DataElementValue(4,0,"1");     //Hierarchical Child Code
 
                    //PRF - PURCHASE ORDER REFERENCE
                    ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\PRF"));
                    oSegment.set_DataElementValue(1,0,"A99999-01");     //Purchase Order Number
                    oSegment.set_DataElementValue(2,0,"016900");     //Release Number
                    oSegment.set_DataElementValue(4,0,"19971207");     //Date
 
                    //REF - REFERENCE IDENTIFICATION
                    ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\REF"));
                    oSegment.set_DataElementValue(1,0,"IV");     //Reference Identification Qualifier
                    oSegment.set_DataElementValue(2,0,"00001");     //Reference Identification
 
                    //FOB - F.O.B. RELATED INSTRUCTIONS
                    ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\FOB"));
                    oSegment.set_DataElementValue(1,0,"PS");     //Shipment Method of Payment
                    oSegment.set_DataElementValue(2,0,"DE");     //Location Qualifier
 
                    //********************************************************************************************
                    //HL - HIERARCHICAL LEVEL - ITEMS ************************************************************
                    while (nItemCounter <= nItems)
                    {
                        nHlCounter = nHlCounter + 1;

                        //HL - HIERARCHICAL LEVEL
                        ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\HL"));
                        oSegment.set_DataElementValue(1,0,nHlCounter.ToString());     //Hierarchical ID Number
                        oSegment.set_DataElementValue(2,0,nHlItemParent.ToString());     //Hierarchical Parent ID Number
                        oSegment.set_DataElementValue(3,0,"I");     //Hierarchical Level Code
                        oSegment.set_DataElementValue(4,0,"0");     //Hierarchical Child Code
 
                        //LIN - ITEM IDENTIFICATION
                        ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\LIN"));
                        oSegment.set_DataElementValue(1,0,"1");     //Assigned Identification
                        oSegment.set_DataElementValue(2,0,"UA");     //Product/Service ID Qualifier
                        oSegment.set_DataElementValue(3,0,"Product ID of item 1");     //Product/Service ID
 
                        //SN1 - ITEM DETAIL (SHIPMENT)
                        ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\SN1"));
                        oSegment.set_DataElementValue(2,0,"13");     //Number of Units Shipped
                        oSegment.set_DataElementValue(3,0,"CA");     //Unit or Basis for Measurement Code
                        oSegment.set_DataElementValue(5,0,"13");     //Quantity Ordered
                        oSegment.set_DataElementValue(6,0,"CA");     //Unit or Basis for Measurement Code
                        oSegment.set_DataElementValue(8,0,"AC");     //Line Item Status Code
 
                        //PRF - PURCHASE ORDER REFERENCE
                        ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\PRF"));
                        oSegment.set_DataElementValue(1,0,"A99999-01");     //Purchase Order Number
                        oSegment.set_DataElementValue(2,0,"016900");     //Release Number
                        oSegment.set_DataElementValue(4,0,"19971207");     //Date
 
                        //PID - PRODUCT/ITEM DESCRIPTION
                        ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("HL\\PID"));
                        oSegment.set_DataElementValue(1,0,"F");     //Item Description Type
                        oSegment.set_DataElementValue(5,0,"Description of item 1");     //Description
 
                        nItemCounter = nItemCounter + 1;
                    }    //nItems

                    nOrderCounter = nOrderCounter + 1;
                }    //nOrders

                nShipmentCounter = nShipmentCounter + 1;
            }    // nShipments

  
            //CTT - TRANSACTION TOTALS
            ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("CTT"));
            oSegment.set_DataElementValue(1,0,nItems.ToString());     //Number of Line Items
 
            //TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES)
            //THE EDIDOC OBJECT INTO AN EDI FILE.
            oEdiDoc.Save(sPath + sEdiFile);
 
            Cursor = Cursors.Default;

            //DESTROYS OBJECTS
            oSegment.Dispose();
            oTransactionset.Dispose();
            oGroup.Dispose();
            oInterchange.Dispose();
            oSchema.Dispose();
            oSchemas.Dispose();
            oEdiDoc.Dispose();

            MessageBox.Show("Done. Output = " + sPath + sEdiFile);
        }
    

    Click here to download a trial version of the Framework EDI