private void btnGenerate_Click(object sender, EventArgs e)
        {
            ediDocument oEdiDoc;
            ediSchema oSchema;
            ediSchemas oSchemas;
            ediInterchange oInterchange;
            ediGroup oGroup;
            ediTransactionSet oTransactionSet;
            ediDataSegment oSegment;
            string sEdiFileName = "";
            string sSefFileName = "";
            string sPath = 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 = (ediSchemas)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;

            // Terminators have to be specified. 
            oEdiDoc.SegmentTerminator = "~{13:10}";
            oEdiDoc.ElementTerminator = "*";
            oEdiDoc.CompositeTerminator = "!";

            sEdiFileName = "940_004010.X12";
            sSefFileName = "940_004010.EVAL0.SEF";//EVALUAION SEF FILES

            // Specify SEF file to load. 
            oSchema = (ediSchema)oEdiDoc.LoadSchema(sPath + sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format);

            // Creates the Interchange Control Header segment (ISA). 
            oInterchange = (ediInterchange)oEdiDoc.CreateInterchange("X", "004010");
            oSegment = (ediDataSegment)oInterchange.GetDataSegmentHeader();
            oSegment.set_DataElementValue(1, 0, "00");         // Authorization Information Qualifier (I01) 
            oSegment.set_DataElementValue(2, 0, "          ");         // Authorization Information (I02) 
            oSegment.set_DataElementValue(3, 0, "00");         // Security Information Qualifier (I03) 
            oSegment.set_DataElementValue(4, 0, "          ");         // Security Information (I04) 
            oSegment.set_DataElementValue(5, 0, "01");         // Interchange ID Qualifier (I05) 
            oSegment.set_DataElementValue(6, 0, "SENDER ID      ");         // Interchange Sender ID (I06) 
            oSegment.set_DataElementValue(7, 0, "01");         // Interchange ID Qualifier (I05) 
            oSegment.set_DataElementValue(8, 0, "RECEIVER ID    ");         // Interchange Receiver ID (I07) 
            oSegment.set_DataElementValue(9, 0, "140815");         // Interchange Date (I08) 
            oSegment.set_DataElementValue(10, 0, "1145");         // Interchange Time (I09) 
            oSegment.set_DataElementValue(11, 0, "U");         // Interchange Control Standards Identifier (I10) 
            oSegment.set_DataElementValue(12, 0, "00401");         // Interchange Control Version Number (I11) 
            oSegment.set_DataElementValue(13, 0, "000000001");         // Interchange Control Number (I12) 
            oSegment.set_DataElementValue(14, 0, "0");         // Acknowledgment Requested (I13) 
            oSegment.set_DataElementValue(15, 0, "T");         // Usage Indicator (I14) 
            oSegment.set_DataElementValue(16, 0, "!");         // Component Element Separator (I15) 

            // Creates the Functional Group Header segment (GS). 
            oGroup = (ediGroup)oInterchange.CreateGroup("004010");
            ediDataSegment.Set(ref oSegment, (ediDataSegment)oGroup.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1, 0, "OW");         // Functional Identifier Code (479) 
            oSegment.set_DataElementValue(2, 0, "SENDER ID");         // Application Sender's Code (142) 
            oSegment.set_DataElementValue(3, 0, "RECEIVER ID");         // Application Receiver's Code (124) 
            oSegment.set_DataElementValue(4, 0, "20140815");         // Date (373) 
            oSegment.set_DataElementValue(5, 0, "0040100");         // Time (337) 
            oSegment.set_DataElementValue(6, 0, "1");         // Group Control Number (28) 
            oSegment.set_DataElementValue(7, 0, "X");         // Responsible Agency Code (455) 
            oSegment.set_DataElementValue(8, 0, "004010");         // Version / Release / Industry Identifier Code (480) 

            // Creates the Transaction Set header segment (ST). 
            oTransactionSet = (ediTransactionSet)oGroup.CreateTransactionSet("940");
            ediDataSegment.Set(ref oSegment, (ediDataSegment)oTransactionSet.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1, 0, "940");         // Transaction Set Identifier Code (143) 
            oSegment.set_DataElementValue(2, 0, "0001");         // Transaction Set Control Number (329) 

            // Shipping Order Identification (W05) 
            oSegment = oTransactionSet.CreateDataSegment("W05");
            // Syntax Rule: If any of 04 or 05 are present, then all are required. 
            oSegment.set_DataElementValue(1, 0, "N");         // Order Status Code (473) 
            oSegment.set_DataElementValue(2, 0, "DepositorOrderNo");         // Depositor Order Number (285) 
            oSegment.set_DataElementValue(3, 0, "PurchaseOrderNo");         // Purchase Order Number (324) 

            if (true)   //Depositor group
            {
                oSegment = oTransactionSet.CreateDataSegment("N1\\N1");
                oSegment.set_DataElementValue(1, 0, "DE");         // Entity Identifier Code (98) 
                oSegment.set_DataElementValue(2, 0, "WarehouseName");         // Name (93) 
                oSegment.set_DataElementValue(3, 0, "6");         // Identification Code Qualifier (66) 
                oSegment.set_DataElementValue(4, 0, "PlantCode");         // Identification Code (67) 
                oSegment.set_DataElementValue(6, 0, "WH");         // Entity Identifier Code (98) 

                ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N1\\N3"));
                oSegment.set_DataElementValue(1, 0, "WarehouseAddr1");         // Address Information (166) 
                oSegment.set_DataElementValue(2, 0, "WarehouseAddr2");         // Address Information (166) 

                ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N1\\N4"));
                // Syntax Rule: If 06 is present, then 05 is required. 
                oSegment.set_DataElementValue(1, 0, "PARIS");         // City Name (19) 
                oSegment.set_DataElementValue(2, 0, "CA");         // State or Province Code (156) 
                oSegment.set_DataElementValue(3, 0, "91506");         // Postal Code (116) ) 
            }

            if (true)   //Bill-To group
            {
                oSegment = oTransactionSet.CreateDataSegment("N1\\N1");
                oSegment.set_DataElementValue(1, 0, "BT");         // Entity Identifier Code (98) 
                oSegment.set_DataElementValue(2, 0, "Bill-To Name");         // Name (93) 
                oSegment.set_DataElementValue(3, 0, "6");         // Identification Code Qualifier (66) 
                oSegment.set_DataElementValue(4, 0, "PlantCode");         // Identification Code (67) 
                oSegment.set_DataElementValue(6, 0, "WH");         // Entity Identifier Code (98) 

                ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N1\\N3"));
                oSegment.set_DataElementValue(1, 0, "Bill-To Addr");         // Address Information (166) 
                oSegment.set_DataElementValue(2, 0, "Bill-To Addr2");         // Address Information (166) 

                ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N1\\N4"));
                // Syntax Rule: If 06 is present, then 05 is required. 
                oSegment.set_DataElementValue(1, 0, "Carson");         // City Name (19) 
                oSegment.set_DataElementValue(2, 0, "CA");         // State or Province Code (156) 
                oSegment.set_DataElementValue(3, 0, "90745");         // Postal Code (116) ) 
            }

            // Reference Identification (N9) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N9"));
            oSegment.set_DataElementValue(1, 0, "BM");         // Reference Identification Qualifier (128) 
            oSegment.set_DataElementValue(2, 0, "BillOfLadingNumber");         // Reference Identification (127)

            // Reference Identification (N9) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("N9(2)"));
            oSegment.set_DataElementValue(1, 0, "LO");         // Reference Identification Qualifier (128) 
            oSegment.set_DataElementValue(2, 0, "LoadPlanningNo");         // Reference Identification (127)

            // Contact (G61) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("G61"));
            oSegment.set_DataElementValue(1, 0, "DC");         // Contact Function Code (366) 
            oSegment.set_DataElementValue(2, 0, "DeliveryContactName");         // Name (93) 
            oSegment.set_DataElementValue(3, 0, "TE");         // Communication Number Qualifier (365) 
            oSegment.set_DataElementValue(4, 0, "8185551234");         // Communication Number (364) 

            // Date/Time (G62) Delivery Requested Date
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("G62"));
            oSegment.set_DataElementValue(1, 0, "02");         // Date Qualifier (432) 
            oSegment.set_DataElementValue(2, 0, "20140815");         // Delivery Requested on This Date 

            // Date/Time (G62) Purchase Order Date
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("G62"));
            oSegment.set_DataElementValue(1, 0, "04");         // Date Qualifier (432) 
            oSegment.set_DataElementValue(2, 0, "20141022");         // Purchase Order Date

            // Date/Time (G62) Delivery No Later Date
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("G62"));
            oSegment.set_DataElementValue(1, 0, "54");         // Date Qualifier (432) 
            oSegment.set_DataElementValue(2, 0, "20140815");         // Delivery No Later Date


            // Note/Special Instruction (NTE) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("NTE"));
            oSegment.set_DataElementValue(1, 0, "DEL");         // Note Reference Code (363) 
            oSegment.set_DataElementValue(2, 0, "Delivery Notes");         // Description (352) 

            // Note/Special Instruction (NTE) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("NTE(2)"));
            oSegment.set_DataElementValue(1, 0, "WHI");         // Note Reference Code (363) 
            oSegment.set_DataElementValue(2, 0, "Warehouse Instructions");         // Description (352) 

            // Warehouse Carrier Information (W66) 
            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("W66"));
            oSegment.set_DataElementValue(1, 0, "CC");         // Shipment Method of Payment (146) 
            oSegment.set_DataElementValue(2, 0, "A");         // Transportation Method/Type Code (91) 
            oSegment.set_DataElementValue(5, 0, "RoutingFreeFormDescript");         // Routing (387) 
            oSegment.set_DataElementValue(10, 0, "SCAC");         // Standard Carrier Alpha Code (140) 

            int nAssignedNo = 0;
            while (nAssignedNo < 1)
            {
                // Assigned Number (LX) 
                oSegment = oTransactionSet.CreateDataSegment("LX\\LX");
                oSegment.set_DataElementValue(1, 0, "1");         // Assigned Number (554) 

                int nLineItem = 0;
                while (nLineItem < 1)
                {
                    // Line Item Detail - Warehouse (W01)
                    oSegment = oTransactionSet.CreateDataSegment("LX\\W01\\W01");
                    oSegment.set_DataElementValue(1, 0, "127.12");         // Quantity Ordered (330) 
                    oSegment.set_DataElementValue(2, 0, "BG");         // Unit or Basis for Measurement Code (355) 
                    oSegment.set_DataElementValue(3, 0, "UPC CaseCode  ");         // U.P.C. Case Code (438) 
                    oSegment.set_DataElementValue(4, 0, "SW");         // Product/Service ID Qualifier (235) 
                    oSegment.set_DataElementValue(5, 0, "ProductStockNo");         // Product/Service ID (234) 
                    oSegment.set_DataElementValue(6, 0, "LT");         // Product/Service ID Qualifier (235) 
                    oSegment.set_DataElementValue(7, 0, "ProductLotNo");         // Product/Service ID (234) 
                    oSegment.set_DataElementValue(14, 0, "01");         // Product/Service Condition Code (286) 
                    oSegment.set_DataElementValue(15, 0, "ZZ");         // Product/Service ID Qualifier (235) 
                    oSegment.set_DataElementValue(16, 0, "ProductNo");         // Product/Service ID (234) 

                    // Line Item Detail - Description (G69) 
                    ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("LX\\W01\\G69"));
                    oSegment.set_DataElementValue(1, 0, "FreeFormDescription");         // Free-form Description (369) 

                    // Reference Identification (N9) 
                    ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("LX\\W01\\N9"));
                    oSegment.set_DataElementValue(1, 0, "LI");         // Reference Identification Qualifier (128) 
                    oSegment.set_DataElementValue(2, 0, "LineItemID");         // Reference Identification (127) 
                    oSegment.set_DataElementValue(3, 0, "ItemDesc");         // Free-form Description (369) 

                    // Line-Item Detail - Miscellaneous (W20)
                    ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("LX\\W01\\W20"));
                    oSegment.set_DataElementValue(4, 0, "34567.12");         // Weight (81) 
                    oSegment.set_DataElementValue(5, 0, "G");         // Gross Weight Qualifier (187) 
                    oSegment.set_DataElementValue(6, 0, "L");         // Pounds Weight Unit Code (188) 

                    nLineItem = nLineItem + 1;
                }//nLineItem

                nAssignedNo = nAssignedNo + 1;
            }//nAssignedNo

            // Total Shipping Order (W76) 
            oSegment = oTransactionSet.CreateDataSegment("W76");
            oSegment.set_DataElementValue(1, 0, "7");         // Quantity Ordered (330) 
            oSegment.set_DataElementValue(2, 0, "93456.98");         // Weight (81) 
            oSegment.set_DataElementValue(3, 0, "LB");         // Unit or Basis for Measurement Code (355) 
            oSegment.set_DataElementValue(4, 0, "345.87");         // Volume (183) 
            oSegment.set_DataElementValue(5, 0, "CF");         // Unit or Basis for Measurement Code (355) 

            // Trailing segments are automatically created when Framework EDI commits 
            // (saves) the instance of "oEdiDoc" object into an EDI file. 
            oEdiDoc.Save(sEdiFileName, 0);

            MessageBox.Show(oEdiDoc.GetEdiString());

            oSegment.Dispose();
            oTransactionSet.Dispose();
            oGroup.Dispose();
            oInterchange.Dispose();
            oSchema.Dispose();
            oSchemas.Dispose();
            oEdiDoc.Dispose();

        }
    

    Click here to download a trial version of the Framework EDI