private void button1_Click(object sender, System.EventArgs e)
{
//This is just an example program to demonstrate how to generate a UN/EDIFACT INVOIC file in C#
//using the Framework EDI .NET hybrid component
ediDocument oEdiDoc = null;
ediSchema oSchema = null;
ediSchemas oSchemas = null;
ediInterchange oInterchange = null;
ediTransactionSet oTransactionset = null;
ediDataSegment oSegment = null;
string sPath = AppDomain.CurrentDomain.BaseDirectory;
string sEdiFile = "INVOIC.TXT";
string sSefFile = "INVOIC_S93A.SEF";
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);
//LOADS THE SEF FILE
ediSchema.Set(ref oSchema, (ediSchema) oEdiDoc.LoadSchema(sPath + sSefFile, 0));
//SET TERMINATORS
oEdiDoc.SegmentTerminator = "'";
oEdiDoc.ElementTerminator = "+";
oEdiDoc.CompositeTerminator = ":";
oEdiDoc.ReleaseIndicator = "?";
//Create UNA segment
oEdiDoc.set_Option(Edidev.FrameworkEDI.DocumentOptionIDConstants.OptDocument_ServiceSegment,1);
//CREATES THE UNB SEGMENT
ediInterchange.Set(ref oInterchange, (ediInterchange) oEdiDoc.CreateInterchange("UN","S93A"));
ediDataSegment.Set(ref oSegment, (ediDataSegment) oInterchange.GetDataSegmentHeader());
oSegment.set_DataElementValue(1,1, "UNOB"); //Syntax identifier
oSegment.set_DataElementValue(1,2, "1"); //Syntax version number
oSegment.set_DataElementValue(2,1, "PARTNER ID"); //Interchange sender identification
oSegment.set_DataElementValue(2,2, "ZZZ"); //Partner identification code qualifier
oSegment.set_DataElementValue(3,1, "0038977332"); //Recipient identification
oSegment.set_DataElementValue(3,2, "1"); //Partner identification code qualifier
oSegment.set_DataElementValue(3,3, "MFGB"); //Routing address
oSegment.set_DataElementValue(4,1, "020331"); //Date
oSegment.set_DataElementValue(4,2, "1230"); //Time
oSegment.set_DataElementValue(5,0,"00000000000001"); //Interchange control reference
oSegment.set_DataElementValue(7,0,"INVOIC"); //Application reference
oSegment.set_DataElementValue(11,0,"1"); //Test indicator
//CREATES THE UNH SEGMENT
ediTransactionSet.Set(ref oTransactionset, (ediTransactionSet) oInterchange.CreateTransactionSet("INVOIC"));
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.GetDataSegmentHeader());
oSegment.set_DataElementValue(1,0,"0001"); //Message reference number
oSegment.set_DataElementValue(2,1,"INVOIC"); //Message type identifier
oSegment.set_DataElementValue(2,2,"S"); //Message version number
oSegment.set_DataElementValue(2,3,"93A"); //Message release number
oSegment.set_DataElementValue(2,4,"UN"); //Controlling agency
//BGM - BEGINNING OF MESSAGE
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("BGM"));
oSegment.set_DataElementValue(1,1,"380"); //Document/message name, coded
oSegment.set_DataElementValue(2,0,"INVOICE-NBR"); //Document/message number
oSegment.set_DataElementValue(3,0,"9"); //Message function, coded
//DTM - DATE/TIME/PERIOD
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("DTM"));
oSegment.set_DataElementValue(1,1,"137"); //Date/time/period qualifier
oSegment.set_DataElementValue(1,2,"20000101"); //Date/time/period
oSegment.set_DataElementValue(1,3,"102"); //Date/time/period format qualifier
//create the RFF segment in the RFF group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("RFF\\RFF"));
oSegment.set_DataElementValue(1,1,"ON"); //Reference qualifier
oSegment.set_DataElementValue(1,2,"CUST_ORDER_NO"); //Reference number
//create the NAD segment in the NAD group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD\\NAD"));
oSegment.set_DataElementValue(1,0,"RE"); //Party qualifier
oSegment.set_DataElementValue(2,3,"92"); //Code list responsible agency, coded
oSegment.set_DataElementValue(4,1,"MANUFACTURER NAME"); //Party name
//create the RFF segment in the RFF group nested in the NAD group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD\\RFF\\RFF"));
oSegment.set_DataElementValue(1,1,"VA"); //Reference qualifier
oSegment.set_DataElementValue(1,2,"DE12931720 6"); //Reference number
//create the COM segment in the CTA group nested in the NAD group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD\\CTA\\CTA"));
oSegment.set_DataElementValue(1,0,"AR"); //Contact function, coded
oSegment.set_DataElementValue(2,2,"JANE DOE"); //Department or employee
//COM - COMMUNICATION CONTACT
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD\\CTA\\COM"));
oSegment.set_DataElementValue(1,1,"00 49 89 9933-2543"); //Communication number
oSegment.set_DataElementValue(1,2,"TE"); //Communication channel qualifier
//create the NAD segment in the second instance of the NAD group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD(2)\\NAD"));
oSegment.set_DataElementValue(1,0,"ST"); //Party qualifier
oSegment.set_DataElementValue(2,3,"92"); //Code list responsible agency, coded
oSegment.set_DataElementValue(4,1,"COMPAQ COMPUTER CORP."); //Party name
//create the NAD segment in the third instance of the NAD group
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("NAD(3)\\NAD"));
oSegment.set_DataElementValue(1,0,"BY"); //Party qualifier
oSegment.set_DataElementValue(2,3,"92"); //Code list responsible agency, coded
oSegment.set_DataElementValue(4,1,"COMPAQ COMPUTER CORP."); //Party name
//CUX - CURRENCIES
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("CUX\\CUX"));
oSegment.set_DataElementValue(1,1,"2"); //Currency details qualifier
oSegment.set_DataElementValue(1,2,"USD"); //Currency, coded
oSegment.set_DataElementValue(1,3,"4"); //Currency qualifier
//ALC - ALLOWANCE OR CHARGE
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("ALC\\ALC"));
oSegment.set_DataElementValue(1,0,"C"); //Allowance or charge qualifier
oSegment.set_DataElementValue(3,0,"6"); //Settlement, coded
oSegment.set_DataElementValue(5,1,"ABG"); //Special services, coded
//PCD - PERCENTAGE DETAILS
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("ALC\\PCD\\PCD"));
oSegment.set_DataElementValue(1,1,"1"); //Percentage qualifier
oSegment.set_DataElementValue(1,2,"2.5"); //Percentage
//MOA - MONETARY AMOUNT
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("ALC\\MOA\\MOA"));
oSegment.set_DataElementValue(1,1,"204"); //Monetary amount type qualifier
oSegment.set_DataElementValue(1,2,"200.00"); //Monetary amount
//LIN - LINE ITEM
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("LIN\\LIN"));
oSegment.set_DataElementValue(1,0,"1"); //Line item number
oSegment.set_DataElementValue(2,0,"10"); //Action request/notification, coded
oSegment.set_DataElementValue(3,1,"240152"); //Item number
oSegment.set_DataElementValue(3,2,"AB"); //Item number type, coded
//QTY - QUANTITY
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("LIN\\QTY"));
oSegment.set_DataElementValue(1,1,"47"); //Quantity qualifier
oSegment.set_DataElementValue(1,2,"3.00"); //Quantity
oSegment.set_DataElementValue(1,3,"EA"); //Measure unit qualifier
//PRI - PRICE DETAILS
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("LIN\\PRI\\PRI"));
oSegment.set_DataElementValue(1,1,"AAA"); //Price qualifier
oSegment.set_DataElementValue(1,2,"1310.00"); //Price
oSegment.set_DataElementValue(1,3,"CT"); //Price type, coded
//UNS - SECTION CONTROL
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("UNS"));
oSegment.set_DataElementValue(1,0,"S"); //Section identification
//create the MOA segment in the MOA group located in area (table) 3 of the message
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("MOA#3\\MOA"));
oSegment.set_DataElementValue(1,1,"77"); //Monetary amount type qualifier
oSegment.set_DataElementValue(1,2,"4378.28"); //Monetary amount
oSegment.set_DataElementValue(1,3,"USD"); //Currency, coded
//create the TAX segment in the TAX group located in area (table) 3 of the message
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("TAX#3\\TAX"));
oSegment.set_DataElementValue(1,0,"7"); //Duty/tax/fee function qualifier
oSegment.set_DataElementValue(2,1,"VAT"); //Duty/tax/fee type, coded
oSegment.set_DataElementValue(5,4,"15"); //Duty/tax/fee rate
oSegment.set_DataElementValue(6,0,"S"); //Duty/tax/fee category, coded
//create the MOA segment in the TAX group located in area (table) 3 of the message
ediDataSegment.Set(ref oSegment, (ediDataSegment) oTransactionset.CreateDataSegment("TAX#3\\MOA"));
oSegment.set_DataElementValue(1,1,"176"); //Monetary amount type qualifier
oSegment.set_DataElementValue(1,2,"248.28"); //Monetary amount
oSegment.set_DataElementValue(1,3,"USD"); //Currency, coded
//TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES) THE EDIDOC OBJECT INTO A FILE.
oEdiDoc.Save(sPath + sEdiFile);
Cursor = Cursors.Default ;
MessageBox.Show("Done. Output = " + sPath + sEdiFile);
}
}