Tuesday, November 29, 2011

How to read a CSV , txt file in AX 2009

Hi Folks,

In this post, I am going to explain you on how to read a CSV file in AX 2009.

1. First of all, make sure that the file is accessible and has sufficient permissions to read the file.

2. Now follow the below code which reads the file, reads all the lines in the file and inserts into the table.

CommaIo                 aSCIIFile;
Container                  recordsCon;
FileIoPermission perm;
StudentTable studentTable;

perm = new FileIoPermission(#ExampleFile, #io_read);

if (perm == null)
// Grants permission to execute the CommaIo.new method.
    // CommaIo.new runs under code access security.
// BP deviation documented.

aSCIIFile = new CommaIo(#ExampleFile,#io_read);
while (aSCIIFile.status()== IO_Status::Ok) //Reading the ASCII file if records are there
               recordsCon     = aSCIIFile.read();
                               studentTable.(fieldName2Id(tableNum(studentTable), "Id")) = conpeek(recordsCon,j);

No comments: