namespace cSharp_Tran834 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ediDocument oEdiDoc = null; string sSegmentID; int nArea; string sLoopSection; bool bErrorFound = false; bool bSevereErrorFound = false; long nErrCode; string sErrDesc; string sSubscriberNumber = ""; string sPolicyNumber = ""; string sEligibilityBeginDate = ""; string sSubscriberLastName = ""; string sSubscriberFirstName = ""; private void btnTranslate_Click(object sender, EventArgs e) { ediSchemas oSchemas = null; ediDataSegment oSegment = null; string sValue = ""; string sN1Code = ""; string sInsNm1Code = ""; string sCodeValue = ""; string sInsRec = ""; string sEdiFileName = "834_5010X220A1.X12"; string sSefFileName = "834_005010X220A1.SemRef.EVAL0.SEF"; //evaluation SEF file string sPath = AppDomain.CurrentDomain.BaseDirectory; // Create the top-level application object "ediDocument". oEdiDoc = new ediDocument(); oEdiDoc.EventNotify += new EventNotifyDelegate(oEdiDoc_EventNotify); // 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 translate // the EDI document oSchemas = (ediSchemas) oEdiDoc.GetSchemas(); oSchemas.EnableStandardReference = false; // The FORWARD-ONLY cursor increases the performance of processing the EDI // document (see Technical Note 3 below). oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly; // Specify SEF file to load. oEdiDoc.LoadSchema(sPath + sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format); // Specify EDI document to read. oEdiDoc.LoadEdi(sEdiFileName); // Gets the first data segment in the EDI document. ediDataSegment.Set(ref oSegment, oEdiDoc.FirstDataSegment); // Loop that will traverse through the EDI document from top to bottom. This // is required for FORWARD-ONLY cursor. while (oSegment != null) { // Data segments are uniquely identified by their segment Identifier (ID). // The loop section and the area (or table) properties of the data segment // are read for each iteration of the loop. sSegmentID = oSegment.ID; sLoopSection = oSegment.LoopSection; nArea = oSegment.Area; if (nArea == 0) { // Data segments that are not designated an area (i.e. area = 0) // are control segments that are part of the Interchange or // Functional Group envelopes, and are not part of the Transaction Set. if (sSegmentID == "ISA") { sValue = oSegment.get_DataElementValue(13, 0); // Interchange Control Number (I12) } else if (sSegmentID == "GS") { sValue = oSegment.get_DataElementValue(6, 0); // Group Control Number (28) } } else if (nArea == 1) { // Data segments in area 1 are processed here. if (sLoopSection == "") { if (sSegmentID == "ST") // Transaction Set Header { sValue = oSegment.get_DataElementValue(1, 0); // Transaction Set Identifier Code (143) sValue = oSegment.get_DataElementValue(2, 0); // Transaction Set Control Number (329) sValue = oSegment.get_DataElementValue(3, 0); // Implementation Convention Reference (1705) sInsRec = ""; } else if (sSegmentID == "BGN") // Beginning Segment { sValue = oSegment.get_DataElementValue(1, 0); // Transaction Set Purpose Code (353) sValue = oSegment.get_DataElementValue(2, 0); // Reference Identification (127) sValue = oSegment.get_DataElementValue(3, 0); // Date (373) sValue = oSegment.get_DataElementValue(4, 0); // Time (337) sValue = oSegment.get_DataElementValue(5, 0); // Time Code (623) sValue = oSegment.get_DataElementValue(6, 0); // Reference Identification (127) sValue = oSegment.get_DataElementValue(7, 0); // Transaction Type Code (640) sValue = oSegment.get_DataElementValue(8, 0); // Action Code (306) } } else if (sLoopSection.StartsWith("N1")) { if (sLoopSection == "N1" && sSegmentID == "N1") { sN1Code = oSegment.get_DataElementValue(1, 0); } // sLoopSection == "N1" && sSegmentID == "N1" if (sN1Code == "P5") { if (sLoopSection == "N1") { if (sSegmentID == "N1") // Name { sValue = oSegment.get_DataElementValue(1, 0); // Entity Identifier Code (98) sValue = oSegment.get_DataElementValue(2, 0); // Name (93) sValue = oSegment.get_DataElementValue(3, 0); // Identification Code Qualifier (66) sValue = oSegment.get_DataElementValue(4, 0); // Identification Code (67) sValue = oSegment.get_DataElementValue(5, 0); // Entity Relationship Code (706) sValue = oSegment.get_DataElementValue(6, 0); // Entity Identifier Code (98) } // sSegmentID == "N1" } // sLoopSection == "N1" } else if (sN1Code == "IN") { if (sLoopSection == "N1") { // Read the loop information containing information about "INSURER" as indicated // by the code qualifier "IN". // if (sSegmentID == "N1") // Name { sValue = oSegment.get_DataElementValue(1, 0); // Entity Identifier Code (98) sValue = oSegment.get_DataElementValue(2, 0); // Name (93) sValue = oSegment.get_DataElementValue(3, 0); // Identification Code Qualifier (66) sValue = oSegment.get_DataElementValue(4, 0); // Identification Code (67) sValue = oSegment.get_DataElementValue(5, 0); // Entity Relationship Code (706) sValue = oSegment.get_DataElementValue(6, 0); // Entity Identifier Code (98) } // sSegmentID == "N1" } // sLoopSection == "N1" } else if ((sN1Code == "BO" || sN1Code == "TV")) { if (sLoopSection == "N1") { // Read the loop information containing information about one of the following: // // "BROKER OR SALES OFFICE" as indicated by the code qualifier "BO". // "THIRD PARTY ADMINISTRATOR (TPA)" as indicated by the code qualifier "TV". // // if (sSegmentID == "N1") // Name { sValue = oSegment.get_DataElementValue(1, 0); // Entity Identifier Code (98) sValue = oSegment.get_DataElementValue(2, 0); // Name (93) sValue = oSegment.get_DataElementValue(3, 0); // Identification Code Qualifier (66) sValue = oSegment.get_DataElementValue(4, 0); // Identification Code (67) sValue = oSegment.get_DataElementValue(5, 0); // Entity Relationship Code (706) sValue = oSegment.get_DataElementValue(6, 0); // Entity Identifier Code (98) } // sSegmentID == "N1" } else if (sLoopSection == "N1;ACT") { if (sSegmentID == "ACT") // Account Identification { sValue = oSegment.get_DataElementValue(1, 0); // Account Number (508) sValue = oSegment.get_DataElementValue(2, 0); // Name (93) sValue = oSegment.get_DataElementValue(3, 0); // Identification Code Qualifier (66) sValue = oSegment.get_DataElementValue(4, 0); // Identification Code (67) sValue = oSegment.get_DataElementValue(5, 0); // Account Number Qualifier (569) sValue = oSegment.get_DataElementValue(6, 0); // Account Number (508) sValue = oSegment.get_DataElementValue(7, 0); // Description (352) sValue = oSegment.get_DataElementValue(8, 0); // Payment Method Code (107) sValue = oSegment.get_DataElementValue(9, 0); // Benefit Status Code (1216) } // sSegmentID == "ACT" } // sLoopSection == "N1;ACT" } // (sN1Code == "BO" || sN1Code == "TV") } // sLoopSection.StartsWith("N1") } else if (nArea == 2) { // Data segments in area 2 are processed here. if (sLoopSection == "") { if (sSegmentID == "SE") { if (bErrorFound) { if (sInsRec != "") { ProcessRejectedRec(); sInsRec = ""; } bErrorFound = false; } else { if (sInsRec != "") { ProcessAcceptedRec(); sInsRec = ""; } } } //sSegmentID } else if (sLoopSection == "INS") { if (sSegmentID == "INS") // Insured Benefit { if (bErrorFound) { if (sInsRec != "") { ProcessRejectedRec(); sInsRec = ""; } bErrorFound = false; } else { if (sInsRec != "") { ProcessAcceptedRec(); sInsRec = ""; } } sValue = oSegment.get_DataElementValue(1, 0); // Yes/No Condition or Response Code (1073) sInsRec = sInsRec + sValue; sValue = oSegment.get_DataElementValue(2, 0); // Individual Relationship Code (1069) sValue = oSegment.get_DataElementValue(3, 0); // Maintenance Type Code (875) sValue = oSegment.get_DataElementValue(4, 0); // Maintenance Reason Code (1203) sValue = oSegment.get_DataElementValue(5, 0); // Benefit Status Code (1216) sValue = oSegment.get_DataElementValue(6, 1); // Medicare Plan Code (1218) sValue = oSegment.get_DataElementValue(6, 2); // Eligibility Reason Code (1701) sValue = oSegment.get_DataElementValue(6, 3); // Eligibility Reason Code (1701) sValue = oSegment.get_DataElementValue(6, 4); // Eligibility Reason Code (1701) sValue = oSegment.get_DataElementValue(7, 0); // Consolidated Omnibus Budget Reconciliation Act (COBRA) Qualify (1219) sValue = oSegment.get_DataElementValue(8, 0); // Employment Status Code (584) } else if (sSegmentID == "REF") // Reference Identification { sCodeValue = oSegment.get_DataElementValue(1, 0); if (sCodeValue == "0F") // SUBSCRIBER NUMBER { sValue = oSegment.get_DataElementValue(1, 0); // Reference Identification Qualifier (128) sSubscriberNumber = oSegment.get_DataElementValue(2, 0); // Reference Identification (127) } else if (sCodeValue == "1L") // POLICY NUMBER { sValue = oSegment.get_DataElementValue(1, 0); // Reference Identification Qualifier (128) sPolicyNumber = oSegment.get_DataElementValue(2, 0); // Reference Identification (127) sValue = oSegment.get_DataElementValue(3, 0); // Description (352) } } else if (sSegmentID == "DTP") // Date or Time or Period { sValue = oSegment.get_DataElementValue(1, 0); // Date/Time Qualifier (374) sValue = oSegment.get_DataElementValue(2, 0); // Date Time Period Format Qualifier (1250) sEligibilityBeginDate = oSegment.get_DataElementValue(3, 0); // Date Time Period (1251) } // sSegmentID } else if (sLoopSection == "INS;HD") { if (sSegmentID == "HD") // Health Coverage { sValue = oSegment.get_DataElementValue(1, 0); // Maintenance Type Code (875) sValue = oSegment.get_DataElementValue(2, 0); // Maintenance Reason Code (1203) sValue = oSegment.get_DataElementValue(3, 0); // Insurance Line Code (1205) sValue = oSegment.get_DataElementValue(4, 0); // Plan Coverage Description (1204) sValue = oSegment.get_DataElementValue(5, 0); // Coverage Level Code (1207) sValue = oSegment.get_DataElementValue(6, 0); // Count (609) sValue = oSegment.get_DataElementValue(7, 0); // Count (609) sValue = oSegment.get_DataElementValue(8, 0); // Underwriting Decision Code (1209) sValue = oSegment.get_DataElementValue(9, 0); // Yes/No Condition or Response Code (1073) sValue = oSegment.get_DataElementValue(10, 0); // Drug House Code (1211) sValue = oSegment.get_DataElementValue(11, 0); // Yes/No Condition or Response Code (1073) } else if (sSegmentID == "DTP") // Date or Time or Period { sValue = oSegment.get_DataElementValue(1, 0); // Date/Time Qualifier (374) sValue = oSegment.get_DataElementValue(2, 0); // Date Time Period Format Qualifier (1250) sValue = oSegment.get_DataElementValue(3, 0); // Date Time Period (1251) } // sSegmentID } else if (sLoopSection == "INS;HD;COB") { if (sSegmentID == "COB") // Coordination of Benefits { sValue = oSegment.get_DataElementValue(1, 0); // Payer Responsibility Sequence Number Code (1138) sValue = oSegment.get_DataElementValue(2, 0); // Reference Identification (127) sValue = oSegment.get_DataElementValue(3, 0); // Coordination of Benefits Code (1143) sValue = oSegment.get_DataElementValue(4, 0); // Service Type Code (1365) } // sSegmentID } else if (sLoopSection.StartsWith("INS;NM1")) { if (sLoopSection == "INS;NM1" && sSegmentID == "NM1") { sInsNm1Code = oSegment.get_DataElementValue(1, 0); } // sLoopSection == "INS;NM1" && sSegmentID == "NM1" if ((sInsNm1Code == "74" || sInsNm1Code == "IL")) { if (sLoopSection == "INS;NM1") { if (sSegmentID == "NM1") // Individual or Organizational Name { sValue = oSegment.get_DataElementValue(1, 0); // Entity Identifier Code (98) sValue = oSegment.get_DataElementValue(2, 0); // Entity Type Qualifier (1065) sSubscriberLastName = oSegment.get_DataElementValue(3, 0); // Name Last or Organization Name (1035) sInsRec = sInsRec + sSubscriberLastName; sSubscriberFirstName = oSegment.get_DataElementValue(4, 0); // Name First (1036) sValue = oSegment.get_DataElementValue(5, 0); // Name Middle (1037) sValue = oSegment.get_DataElementValue(6, 0); // Name Prefix (1038) sValue = oSegment.get_DataElementValue(7, 0); // Name Suffix (1039) sValue = oSegment.get_DataElementValue(8, 0); // Identification Code Qualifier (66) sValue = oSegment.get_DataElementValue(9, 0); // Identification Code (67) } // sSegmentID } // sLoopSection == "INS;NM1" } // sInsNm1Code } // sLoopSection.StartsWith("INS;NM1") } // nArea == 2 // Get the next data segment in the document. ediDataSegment.Set(ref oSegment, oSegment.Next()); } // oSegment != null } private void ProcessAcceptedRec() { listBox1.Items.Add("Subscriber number: " + sSubscriberNumber); listBox1.Items.Add("Policy Number: " + sPolicyNumber); listBox1.Items.Add("Eligibility Begin Date: " + sEligibilityBeginDate); listBox1.Items.Add("Subscriber LastName: " + sSubscriberLastName); listBox1.Items.Add("Subscriber FirstName: " + sSubscriberFirstName); listBox1.Items.Add(" "); } private void ProcessRejectedRec() { listBox2.Items.Add("Record rejected because error was found. Error Code = " + nErrCode.ToString() + " " + sErrDesc); listBox2.Items.Add("Subscriber number: " + sSubscriberNumber); listBox2.Items.Add("Policy Number: " + sPolicyNumber); listBox2.Items.Add("Eligibility Begin Date: " + sEligibilityBeginDate); listBox2.Items.Add("Subscriber LastName: " + sSubscriberLastName); listBox2.Items.Add("Subscriber FirstName: " + sSubscriberFirstName); listBox2.Items.Add(" "); } void oEdiDoc_EventNotify(ediEventNotify EventNotify) { if (EventNotify.Severity == EventIDConstants.Event_Warning) { nErrCode = EventNotify.ErrorCode; sErrDesc = EventNotify.ErrorDescription; if (sLoopSection.StartsWith("INS")) { // Only interested with errors inside INS loop bErrorFound = true; } else { bErrorFound = false; } } else if (EventNotify.Severity == EventIDConstants.Event_Severe) { nErrCode = EventNotify.ErrorCode; sErrDesc = EventNotify.ErrorDescription; bSevereErrorFound = true; } } } }