Import dataport and skipping the header line

krikikriki Member, Moderator Posts: 9,112
edited 2013-09-27 in NAV Tips & Tricks
Problem:you have a textfile with headers and you must import it with a dataport, but you want to skip the header line, because otherwise you get an error. How to do that?

Put some code in trigger "OnPreDataItem()":
(variable "cha" is CHAR)
IF CurrDataport.IMPORT THEN BEGIN
  REPEAT
    CurrFile.READ(cha);
  UNTIL cha = 10; // of course in case the RecordSeparator is "<<NewLine>>"
END;
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Comments

  • krikikriki Member, Moderator Posts: 9,112
    And for more than 1 line:
    IF CurrDataport.IMPORT THEN BEGIN
      LintLinesToRead := 2; // lines that must be read (this is an integer)
      REPEAT
        CurrFile.READ(cha);
        IF cha = 10 THEN
          LintLinesToRead -= 1;
      UNTIL LintLinesToRead = 0;
    END;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • gulamdastagirgulamdastagir Member Posts: 411
    I get an error "File is not open" at Currfile.Read(cha) part of the code.

    i dont want to hardocode the filepath but when the "user" slects the "csv"file from the options tab

    iam using 4.0(SP3)

    Thanks
    Regards,

    GD
  • SavatageSavatage Member Posts: 7,142
    are you sure the code is on PreDataItem not PreDataPort?

    (variable "cha" is TYPE CHAR)
  • gulamdastagirgulamdastagir Member Posts: 411
    thanks savatage #-o
    Regards,

    GD
  • colingbradleycolingbradley Member Posts: 162
    I am missing the logic for the Until cha = 10;
    Is 10 the expected number of records?
    Experience is what you get when you hoped to get money
  • krikikriki Member, Moderator Posts: 9,112
    No, the 10 is the ASCII value of the line feed.

    A newline in a textfile is defined by 2 chars : CR/LF = Carriage Return + Line Feed = ASCII 13 + ASCII 10.

    So I am reading char per char until I find ASCII value = 10. When I have found it, I know I am at the end-of-line.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • colingbradleycolingbradley Member Posts: 162
    Thanks, it is a long time since I had to think about ASCII.
    I have used your code and it works perfectly, many thanks.
    Experience is what you get when you hoped to get money
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Simple and Brilliant.

    Thanks. :thumbsup:
    David Singleton
  • markborgesmarkborges Member Posts: 170
    Worked like a charm! Thank you!

    \:D/
    Marcelo Borges
    D365 Business Central Solutions Architect
    BC AL/NAV C/AL Developer
    BC Repositories.com
  • eugeseuges Member Posts: 8
    Perfect! Thanks.
    B)
  • krikikriki Member, Moderator Posts: 9,112
    I like it when I see that an old reply is still useful to fix problems.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • gandhi_nklgandhi_nkl Member Posts: 44
    It works for me, thank you so much.
    Thanks & Regards,
    Muthu
Sign In or Register to comment.