private void btnTranslate_Click(object sender, System.EventArgs e) { //This is an example program to demonstrate how to translate and validate an //EDI 810 file in C# using Framework EDI .NET hybrid ediDocument oEdiDoc = null; ediSchemas oSchemas = null; ediDataSegment oSegment = null; ediWarnings oWarnings = null; ediWarning oWarning = null; int nErrors; string sSegmentID; string sLoopSection; int nArea; string sNm1Entity = ""; string sValue = ""; string sPath = ""; string sEdiFile; string sSefFile; sPath = AppDomain.CurrentDomain.BaseDirectory; sSefFile = "810_X12-4010.SEF"; sEdiFile = "810_4010.X12"; //CREATES EDIDOC 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 TRANSLATE THE EDI FILE. ediSchemas.Set(ref oSchemas,(ediSchemas) oEdiDoc.GetSchemas()); //oSchemas = (ediSchemas) oEdiDoc.GetSchemas(); oSchemas.EnableStandardReference = false; //THIS OPTIONS STOPS FREDI FROM KEEPING ALL THE SEGMENTS IN MEMORY oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly; //LOADS THE SEF FILE oEdiDoc.ImportSchema(sPath + sSefFile, 0); //LOADS THE EDI FILE oEdiDoc.LoadEdi(sPath + sEdiFile); //GETS THE FIRST DATA SEGMENT ediDataSegment.Set(ref oSegment, (ediDataSegment) oEdiDoc.FirstDataSegment); //oSegment = (ediDataSegment) oEdiDoc.FirstDataSegment //LOOP THAT WILL TRAVERSE THRU EDI FILE FROM TOP TO BOTTOM while ( oSegment != null ) { //DATA SEGMENTS WILL BE IDENTIFIED BY THEIR ID, THE LOOP SECTION AND AREA //(OR TABLE) NUMBER THAT THEY ARE IN. sSegmentID = oSegment.ID; sLoopSection = oSegment.LoopSection; nArea = oSegment.Area; if (nArea == 0) { if (sLoopSection == "") { if (sSegmentID == "ISA") { sValue = oSegment.get_DataElementValue(1,0); //Authorization Information Qualifier sValue = oSegment.get_DataElementValue(2,0); //Authorization Information sValue = oSegment.get_DataElementValue(3,0); //Security Information Qualifier sValue = oSegment.get_DataElementValue(4,0); //Security Information sValue = oSegment.get_DataElementValue(5,0); //Interchange ID Qualifier sValue = oSegment.get_DataElementValue(6,0); //Interchange Sender ID sValue = oSegment.get_DataElementValue(7,0); //Interchange ID Qualifier sValue = oSegment.get_DataElementValue(8,0); //Interchange Receiver ID sValue = oSegment.get_DataElementValue(9,0); //Interchange Date sValue = oSegment.get_DataElementValue(10,0); //Interchange Time sValue = oSegment.get_DataElementValue(11,0); //Interchange Control Standards Identifier sValue = oSegment.get_DataElementValue(12,0); //Interchange Control Version Number listBox1.Items.Add(oSegment.get_DataElement(13).Description + " = " + oSegment.get_DataElementValue(13,0)); //Interchange Control Number sValue = oSegment.get_DataElementValue(14,0); //Acknowledgment Requested sValue = oSegment.get_DataElementValue(15,0); //Usage Indicator sValue = oSegment.get_DataElementValue(16,0); //Component Element Separator } else if (sSegmentID == "GS") { sValue = oSegment.get_DataElementValue(1,0); //Functional Identifier Code sValue = oSegment.get_DataElementValue(2,0); //Application Sender's Code sValue = oSegment.get_DataElementValue(3,0); //Application Receiver's Code sValue = oSegment.get_DataElementValue(4,0); //Date sValue = oSegment.get_DataElementValue(5,0); //Time listBox1.Items.Add(oSegment.get_DataElement(6).Description + " = " + oSegment.get_DataElementValue(6,0)); //Group Control Number sValue = oSegment.get_DataElementValue(7,0); //Responsible Agency Code sValue = oSegment.get_DataElementValue(8,0); //Version / Release / Industry Identifier Code } //sSegmentID } //sLoopSection } else if (nArea == 1) { if (sLoopSection == "") { if (sSegmentID == "ST") { sValue = oSegment.get_DataElementValue(1,0); //Transaction Set Identifier Code sValue = oSegment.get_DataElementValue(2,0); //Transaction Set Control Number } else if (sSegmentID == "BIG") { sValue = oSegment.get_DataElementValue(1,0); //Date listBox1.Items.Add(oSegment.get_DataElement(2).Description + " = " + oSegment.get_DataElementValue(2,0)); //Invoice Number sValue = oSegment.get_DataElementValue(3,0); //Date sValue = oSegment.get_DataElementValue(4,0); //Purchase Order Number } //Segment ID } else if (sLoopSection == "N1") { if (sSegmentID == "N1") { sNm1Entity = oSegment.get_DataElementValue(1,0); //In most cases the loop qualifier is the first element of the first segment in the loop, but not necessarily } if (sNm1Entity == "ST") //SHIP-TO INFORMATION { if (sSegmentID == "N1") { sValue = oSegment.get_DataElementValue(1,0); //Entity Identifier Code listBox1.Items.Add("Ship-To Name = " + oSegment.get_DataElementValue(2,0)); //Name sValue = oSegment.get_DataElementValue(3,0); //Identification Code Qualifier sValue = oSegment.get_DataElementValue(4,0); //Identification Code } else if (sSegmentID == "N3") { sValue = oSegment.get_DataElementValue(1,0); //Address Information } else if (sSegmentID == "N4") { sValue = oSegment.get_DataElementValue(1,0); //City Name sValue = oSegment.get_DataElementValue(2,0); //State or Province Code sValue = oSegment.get_DataElementValue(3,0); //Postal Code } //sSegmentID } else if (sNm1Entity == "BT") //BILL-TO INFORMATION { if (sSegmentID == "N1") { sValue = oSegment.get_DataElementValue(1,0); //Entity Identifier Code listBox1.Items.Add("Bill-To Name = " + oSegment.get_DataElementValue(2,0)); //Name sValue = oSegment.get_DataElementValue(3,0); //Identification Code Qualifier sValue = oSegment.get_DataElementValue(4,0); //Identification Code } else if (sSegmentID == "N3") { sValue = oSegment.get_DataElementValue(1,0); //Address Information } else if (sSegmentID == "N4") { sValue = oSegment.get_DataElementValue(1,0); //City Name sValue = oSegment.get_DataElementValue(2,0); //State or Province Code sValue = oSegment.get_DataElementValue(3,0); //Postal Code } //sSegmentID } else if (sNm1Entity == "RE") //REMIT-TO INFORMATION { if (sSegmentID == "N1") { sValue = oSegment.get_DataElementValue(1,0); //Entity Identifier Code listBox1.Items.Add("Remit-To Name = " + oSegment.get_DataElementValue(2,0)); //Name sValue = oSegment.get_DataElementValue(3,0); //Identification Code Qualifier sValue = oSegment.get_DataElementValue(4,0); //Identification Code } else if (sSegmentID == "N3") { sValue = oSegment.get_DataElementValue(1,0); //Address Information } else if (sSegmentID == "N4") { sValue = oSegment.get_DataElementValue(1,0); //City Name sValue = oSegment.get_DataElementValue(2,0); //State or Province Code sValue = oSegment.get_DataElementValue(3,0); //Postal Code } //sSegmentID } } //sLoopSection } else if (nArea == 2) { if (sLoopSection == "IT1") { if (sSegmentID == "IT1") { listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,0)); //Assigned Identification sValue = oSegment.get_DataElementValue(2,0); //Quantity Invoiced sValue = oSegment.get_DataElementValue(3,0); //Unit or Basis for Measurement Code listBox1.Items.Add(oSegment.get_DataElement(4).Description + " = " + oSegment.get_DataElementValue(4,0)); //Unit Price sValue = oSegment.get_DataElementValue(5,0); //Basis of Unit Price Code sValue = oSegment.get_DataElementValue(6,0); //Product/Service ID Qualifier sValue = oSegment.get_DataElementValue(7,0); //Product/Service ID } //Segment ID } else if (sLoopSection == "IT1;PID") { if (sSegmentID == "PID") { sValue = oSegment.get_DataElementValue(1,0); //Item Description Type sValue = oSegment.get_DataElementValue(2,0); //Product/Process Characteristic Code sValue = oSegment.get_DataElementValue(3,0); //Agency Qualifier Code sValue = oSegment.get_DataElementValue(4,0); //Product Description Code listBox1.Items.Add(oSegment.get_DataElement(5).Description + " = " + oSegment.get_DataElementValue(5,0)); //Description } //sSegmentID } //sLoopSection } else if (nArea == 3) { if (sLoopSection == "") { if (sSegmentID == "TDS") { sValue = oSegment.get_DataElementValue(1,0); //Amount } else if (sSegmentID == "CAD") { sValue = oSegment.get_DataElementValue(1,0); //Transportation Method/Type Code sValue = oSegment.get_DataElementValue(2,0); //Equipment Initial sValue = oSegment.get_DataElementValue(3,0); //Equipment Number sValue = oSegment.get_DataElementValue(4,0); //Standard Carrier Alpha Code sValue = oSegment.get_DataElementValue(5,0); //Routing } //Segment ID } else if (sLoopSection == "ISS") { if (sSegmentID == "ISS") { listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,0)); //Number of Units Shipped sValue = oSegment.get_DataElementValue(2,0); //Unit or Basis for Measurement Code } //sSegmentID } //sLoopSection } //nArea //GETS THE NEXT DATA SEGMENT ediDataSegment.Set(ref oSegment, (ediDataSegment) oSegment.Next()); //oSegment = (ediDataSegment) oSegment.Next(); } //while //check to see if any errors were found in the EDI file ediWarnings.Set(ref oWarnings, (ediWarnings) oEdiDoc.GetWarnings()); nErrors = oWarnings.Count; for (int i=1; i <= nErrors; i++) { ediWarning.Set(ref oWarning, (ediWarning) oWarnings.get_Warning(i)); listBoxErrors.Items.Add(oWarning.Description); } MessageBox.Show("Done!"); }