X12Reader error in InterchangeHeader

Post author
Ahmed Imran Khalid

Hi 

I am using below code to get the interchange Control number but getting Null Reference Exception error.

Any idea how to get Interchange Control Number.

I am using Demo version of .NET SDK

 

using (var ediReader = new X12Reader(x12Stream, LoadFactory, new X12ReaderSettings() { ContinueOnError = true }))
{
x12Items = ediReader.ReadToEnd().ToList();

var icn = ediReader.CurrentInterchangeHeader.InterchangeControlNumber_13;
Console.WriteLine("ICN:{0}", icn);

}

Comments

1 comment

  • Comment author
    Admin

    Hi,

    You can access the interchange control number like this:

    1. When using ReadToEnd() - this would have read ALL the data in the file so potentially multiple interchange control numbers. You need to tell which interchange control number exactly, that's why CurrentInterchangeHeader is NULL because there is no current:

    using (var ediReader = new X12Reader(ediStream, TemplateFactory.TrialTemplateFactory))
    x12Items = ediReader.ReadToEnd().ToList();

    var firstIcn = ediItems.OfType<ISA>().First().InterchangeControlNumber_13;
    Console.WriteLine("ICN:{0}", firstIcn );

    2. When using Read(), you can use the CurrentInterchangeHeader because it's still iterating through the items:

    using (var ediReader = new X12Reader(ediStream, LoadFactory))
    {
    while(ediReader.Read())
    {
    var icn = ediReader.CurrentInterchangeHeader.InterchangeControlNumber_13;
    Console.WriteLine("ICN:{0}", icn);
    }
    }
    0

Please sign in to leave a comment.