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 sSql = "";
            string sPath;
            string sConnection;
            int nitem = 0;

            sPath = AppDomain.CurrentDomain.BaseDirectory;
            sConnection = "persist security info=False; integrated security=true; initial catalog=TestDb; server=(local)";

            SqlConnection oConnection = new SqlConnection(sConnection);
            
            oEdiDoc = new ediDocument();

            oSchemas = (ediSchemas)oEdiDoc.GetSchemas();
            oSchemas.EnableStandardReference = false;

            oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite;

            oEdiDoc.SegmentTerminator = "~{13:10}";
            oEdiDoc.ElementTerminator = "*";
            oEdiDoc.CompositeTerminator = "!";

            sEdiFileName = sPath + "850_004010.X12";
            sSefFileName = sPath + "850_004010.EVAL0.SEF";  //evaluation SEF file

            oSchema = (ediSchema)oEdiDoc.LoadSchema(sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format);

            sSql = "select * from Interchange";
            SqlDataAdapter oInterchangeAdapter = new SqlDataAdapter(sSql, oConnection);
            DataSet oInterchangeDs = new DataSet("dsInterchange");
            oInterchangeAdapter.Fill(oInterchangeDs, "dsInterchange");

            foreach (DataRow oInterchangeRow in oInterchangeDs.Tables["dsInterchange"].Rows)
            {
                oInterchange = (ediInterchange)oEdiDoc.CreateInterchange("X", "004010");
                oSegment = (ediDataSegment)oInterchange.GetDataSegmentHeader();
                oSegment.set_DataElementValue(1, 0, oInterchangeRow["ISA01_AuthorizationInfoQlfr"].ToString());
                oSegment.set_DataElementValue(2, 0, oInterchangeRow["ISA02_AuthorizationInfo"].ToString());
                oSegment.set_DataElementValue(3, 0, oInterchangeRow["ISA03_SecurityInfoQlfr"].ToString());
                oSegment.set_DataElementValue(4, 0, oInterchangeRow["ISA04_SecurityInfo"].ToString());
                oSegment.set_DataElementValue(5, 0, oInterchangeRow["ISA05_SenderIdQlfr"].ToString());
                oSegment.set_DataElementValue(6, 0, oInterchangeRow["ISA06_SenderId"].ToString());
                oSegment.set_DataElementValue(7, 0, oInterchangeRow["ISA07_ReceiverIdQlfr"].ToString());
                oSegment.set_DataElementValue(8, 0, oInterchangeRow["ISA08_ReceiverId"].ToString());
                oSegment.set_DataElementValue(9, 0, oInterchangeRow["ISA09_Date"].ToString());
                oSegment.set_DataElementValue(10, 0, oInterchangeRow["ISA10_Time"].ToString());
                oSegment.set_DataElementValue(11, 0, oInterchangeRow["ISA11_RepetitionSeparator"].ToString());
                oSegment.set_DataElementValue(12, 0, oInterchangeRow["ISA12_VersionNumber"].ToString());
                oSegment.set_DataElementValue(13, 0, oInterchangeRow["ISA13_ControlNumber"].ToString());
                oSegment.set_DataElementValue(14, 0, oInterchangeRow["ISA14_AckRequested"].ToString());
                oSegment.set_DataElementValue(15, 0, oInterchangeRow["ISA15_UsageIndicator"].ToString());
                oSegment.set_DataElementValue(16, 0, oInterchangeRow["ISA16_ComponentSeparator"].ToString());

                sSql = "select * from FunctionalGroup where Interkey = " + oInterchangeRow["Interkey"].ToString();
                SqlDataAdapter oGroupAdapter = new SqlDataAdapter(sSql, oConnection);
                DataSet oGroupDs = new DataSet("dsGroup");
                oGroupAdapter.Fill(oGroupDs, "dsGroup");

                foreach (DataRow oGroupRow in oGroupDs.Tables["dsGroup"].Rows)
                {

                    oGroup = (ediGroup)oInterchange.CreateGroup("004010");
                    ediDataSegment.Set(ref oSegment, (ediDataSegment)oGroup.GetDataSegmentHeader());
                    oSegment.set_DataElementValue(1, 0, oGroupRow["GS01_FunctionalIdfrCode"].ToString());
                    oSegment.set_DataElementValue(2, 0, oGroupRow["GS02_SendersCode"].ToString());
                    oSegment.set_DataElementValue(3, 0, oGroupRow["GS03_ReceiversCode"].ToString());
                    oSegment.set_DataElementValue(4, 0, oGroupRow["GS04_Date"].ToString());
                    oSegment.set_DataElementValue(5, 0, oGroupRow["GS05_Time"].ToString());
                    oSegment.set_DataElementValue(6, 0, oGroupRow["GS06_Control_Number"].ToString());
                    oSegment.set_DataElementValue(7, 0, oGroupRow["GS07_Agency_Code"].ToString());
                    oSegment.set_DataElementValue(8, 0, oGroupRow["GS08_VersionCode"].ToString());

                    sSql = "select * from [Header] where Groupkey = " + oGroupRow["Groupkey"].ToString();
                    SqlDataAdapter oHeaderAdapter = new SqlDataAdapter(sSql, oConnection);
                    DataSet oHeaderDs = new DataSet("dsHeader");
                    oHeaderAdapter.Fill(oHeaderDs, "dsHeader");

                    foreach (DataRow oHeaderRow in oHeaderDs.Tables["dsHeader"].Rows)
                    {
                        oTransactionSet = (ediTransactionSet)oGroup.CreateTransactionSet("850");
                        ediDataSegment.Set(ref oSegment, (ediDataSegment)oTransactionSet.GetDataSegmentHeader());
                        oSegment.set_DataElementValue(1, 0, "850");
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["ST02_TS_ControlNumber"].ToString());

                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("BEG"));
                        oSegment.set_DataElementValue(1, 0, "00");
                        oSegment.set_DataElementValue(2, 0, "SA");
                        oSegment.set_DataElementValue(3, 0, oHeaderRow["BEG03_PurchaseOrderNumber"].ToString());
                        oSegment.set_DataElementValue(5, 0, oHeaderRow["BEG05_PODate"].ToString());

                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("REF"));
                        oSegment.set_DataElementValue(1, 0, "VR");
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["REF02_VendorIDNo"].ToString());
                        
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("ITD"));
                        oSegment.set_DataElementValue(1, 0, "01");
                        oSegment.set_DataElementValue(2, 0, "3");
                        oSegment.set_DataElementValue(3, 0, oHeaderRow["ITD03_TermsDiscountPercent"].ToString());
                        oSegment.set_DataElementValue(5, 0, oHeaderRow["ITD05_TermsDiscountDaysDue"].ToString());
                        oSegment.set_DataElementValue(7, 0, oHeaderRow["ITD07_TermsNetDays"].ToString());
                        
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment("DTM"));
                        oSegment.set_DataElementValue(1, 0, "002");
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["DTM02_DeliveryDate"].ToString());
                        
                        //BILL TO
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N1"));
                        oSegment.set_DataElementValue(1, 0, "BT");
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["N102_BillToName"].ToString());
                        oSegment.set_DataElementValue(3, 0, "9");
                        oSegment.set_DataElementValue(4, 0, oHeaderRow["N104_BillToIdCode"].ToString());

                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N3"));
                        oSegment.set_DataElementValue(1, 0, oHeaderRow["N301_BillToAddress"].ToString());

                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N4"));
                        oSegment.set_DataElementValue(1, 0, oHeaderRow["N401_BillToCity"].ToString());
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["N402_BillToState"].ToString());
                        oSegment.set_DataElementValue(3, 0, oHeaderRow["N403_BillToZip"].ToString());

                        //SHIP TO
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N1"));
                        oSegment.set_DataElementValue(1, 0, "ST");
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["N102_ShipToName"].ToString());
                        oSegment.set_DataElementValue(3, 0, "9");
                        oSegment.set_DataElementValue(4, 0, oHeaderRow["N104_ShipToIdCode"].ToString());
                        
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N3"));
                        oSegment.set_DataElementValue(1, 0, oHeaderRow["N301_ShipToAddress"].ToString());
                        
                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"N1\N4"));
                        oSegment.set_DataElementValue(1, 0, oHeaderRow["N401_ShipToCity"].ToString());
                        oSegment.set_DataElementValue(2, 0, oHeaderRow["N402_ShipToState"].ToString());
                        oSegment.set_DataElementValue(3, 0, oHeaderRow["N403_ShipToZip"].ToString());
                        

                        
                        
                        sSql = "select * from [Detail] where Headerkey = " + oHeaderRow["Headerkey"].ToString();
                        SqlDataAdapter oDetailAdapter = new SqlDataAdapter(sSql, oConnection);
                        DataSet oDetailDs = new DataSet("dsDetail");
                        oDetailAdapter.Fill(oDetailDs, "dsDetail");

                        foreach (DataRow DetailRow in oDetailDs.Tables["dsDetail"].Rows)
                        {
                            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"PO1\PO1"));
                            oSegment.set_DataElementValue(1, 0, DetailRow["PO101_AssignedId"].ToString());
                            oSegment.set_DataElementValue(2, 0, DetailRow["PO102_QtyOrdered"].ToString());
                            oSegment.set_DataElementValue(3, 0, DetailRow["PO103_UnitMeasure"].ToString());
                            oSegment.set_DataElementValue(4, 0, DetailRow["PO104_UnitPrice"].ToString());
                            oSegment.set_DataElementValue(5, 0, "");
                            oSegment.set_DataElementValue(6, 0, "CB");
                            oSegment.set_DataElementValue(7, 0, DetailRow["PO107_CatalogNo"].ToString());
                            oSegment.set_DataElementValue(8, 0, "UA");
                            oSegment.set_DataElementValue(9, 0, DetailRow["PO109_EAN"].ToString());

                            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"PO1\PID\PID"));
                            oSegment.set_DataElementValue(1, 0, "F");
                            oSegment.set_DataElementValue(5, 0, DetailRow["PID05_Description"].ToString());

                            ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"PO1\PO4"));
                            oSegment.set_DataElementValue(1, 0, DetailRow["PO401_Pack"].ToString());
                            oSegment.set_DataElementValue(2, 0, DetailRow["PO402_Size"].ToString());
                            oSegment.set_DataElementValue(3, 0, "LB");

                            nitem = nitem + 1;

                        }//foreach oDetailRow

                        ediDataSegment.Set(ref oSegment, oTransactionSet.CreateDataSegment(@"CTT\CTT"));
                        oSegment.set_DataElementValue(1, 0, Convert.ToString(nitem));

                    }//foreach oHeaderRow

                }//foreach oGroupRow

            }  //foreach oInterchangeRow


            oEdiDoc.Save(sEdiFileName, 0);

            MessageBox.Show(oEdiDoc.GetEdiString());

        }

    Click here to download a trial version of the Framework EDI