Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Troubles importing a csv file via commaIo object

Posted on by 1,085

Hi,

i'm importing fields from a csv file like this:

 

 

Robert White,Naples,0001

Eric Red,Washington,0002

Peter Green,Paris,0003

 

 

I can't understand why I need to put a carriage return on the first line to have all three rows imported.

And I can't understand why it also reads an "extra row" filled with zeros in the end.

 

So my output is basically:

 Robert White,Naples,0001

Eric Red,Washington,0002

Peter Green,Paris,0003

0,0,0

 

Is there a particular behaviour for this object? Documentation from Microsoft is quite poor about it...

 

This is my code:

 

static void CommaIO(Args _args)

{

    FileName fileName = 'C:\\Users\\note98\\Desktop\\testdata.csv';

    CommaIo csvFile;

    container readCon;

 

    str column1,column2,column3;

    ;

 

    csvFile = new CommaIo(filename, 'r');

    csvFile.inFieldDelimiter(','); // Delimiter...

 

    if (csvFile)

    {

        readCon = csvFile.read();

 

        while (csvFile.status() == IO_Status::OK)

        {

            readCon = csvFile.read();

 

            column1 = conPeek(readCon, 1);

            column2 = conPeek(readCon, 2);

            column3 = conPeek(readCon, 3);

 

            info(strFmt("%1 - %2 - %3", column1, column2, column3));

        }

    }

}

 

*This post is locked for comments

  • Khurshid Wali Profile Picture
    Khurshid Wali 922 on at
    RE: Troubles importing a csv file via commaIo object

    CommaIO class is still working, but the FileIO class is totally replaced.

    I am practicing with Microsoft DevIII-02 course and I found this example.

    Error: FileIO class Missing

    FileIoPermission permission;

    FileIO fileIO;

    str outputText;

    #File

    ;

    permission= new FileIoPermission(filename,#io_write);

    permission.assert();

    fileIO= new FileIO(filename, #io_write);

    if (fileIO)

    {

    outputText = "text that will go into the text file.";

    fileIO.write(outputText); //write the text to the

    file.

    fileIO.finalize(); //finish the file.

    }

  • Khurshid Wali Profile Picture
    Khurshid Wali 922 on at
    RE: Troubles importing a csv file via commaIo object

    I faced the same problem.  

    Here is how I solved this issue. I think it is not the proper way to use while statement

    readCon=commaIO.read();

           while (readCon)

           {

                   column1=conPeek(readCon,1);

                   column2=conPeek(readCon,2);

                   column3= conPeek(readCon,3);

                   info(strFmt('%1-%2-%3',column1,column2,column3));

               readCon = commaIO.read();

           }

  • vv1984 Profile Picture
    vv1984 1,085 on at
    Re: Troubles importing a csv file via commaIo object

    Thank you for your help.

    I finally fixed it!

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,056 Most Valuable Professional on at
    Re: Troubles importing a csv file via commaIo object

    Think about what your code does. It:

    1. Reads the first line
    2. If it's OK, it reads the second line (first command in while). That's why your first line is skipped.
    3. (...)
    4. Reads after the last line. Status is not OK anymore, but you first process this invalid data and only then you check status(). That's why you get those zeroes.

    By the way, CommaIo class was replaced by CommaTextIo.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,219 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans