web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Write Data in Text File

(0) ShareShare
ReportReport
Posted on by

Hi Sir,

Good day.  I have search how to write data to a text file and i have questions regarding this. where would i put the static server void WriteTextFile(Args _args) ? is it inside the datasource method? of control method?

static server void WriteTextFile(Args _args)
{
TextIo file;
FileName filename = @"c:\temp\cars.txt";
CarTable carTable;
container con;
FileIoPermission permission;
#File
;
try
{
permission = new FileIoPermission(filename, #io_write);
permission.assert();
file = new TextIo(filename, #io_write);
if (!file)
throw Exception::Error;
file.outRecordDelimiter(#delimiterCRLF);
file.outFieldDelimiter(";");
while select carTable
{
con = connull();
con = conins(con, 1, carTable.CarId);
con = conins(con, 2, carTable.CarBrand);
con = conins(con, 3, carTable.Mileage);
con = conins(con, 4, carTable.Model);
con = conins(con, 5, carTable.ModelYear);
file.writeExp(con);
}
}
catch(Exception::Error)
{
error("You do not have access to write the file to the selected folder");
}
CodeAccessPermission::revertAssert(); 
 }

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    From your code, my understanding is that you want to export data from carTable.
    If you need to simply export data from table, then instead of datasource/control then you need to create a class with main method and  static method WriteTextFile and include your code there.Create a Action menuItem for the same.

    If you want to export only specific data on the form, then you can create a button and in the clicked method you can iterate through the selected datasource records like below:
    Declare: CarTable carTable;

    for (carTable = (CarTable_ds.getFirst(true) ? CarTable_ds.getFirst(true) : CarTable_ds.cursor());
    carTable;
    carTable = CarTable_ds.getNext())
    {
    //Your code to export data
    }
  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Typically your code looks like below:

    i. Create a class (namely) ExportCarData

    ii. Create a main method, with a dialog field to select the file of type CSV or txt.

    public static void main(Args _args)
    {
        Dialog                      dialog;
        DialogField                 dfFileName;
        
        dialog = new Dialog("Pick the file");
        dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
        dialog.filenameLookupFilter(['csv','*.csv', '*.txt']);
        
        if (dialog.run())   
        {
            ExportCarData::WriteTextFile(dfFileName.value());     
        }
    }

    3. Create a static method WriteTextFile with parameter as FileName.

    static server void WriteTextFile(Filename _fileName)
    {
    TextIo file;
    FileName filename = _fileName;
    SampleTable carTable;
    container con;
    FileIoPermission permission;
    #File
    ;
    
    try
    {
        permission = new FileIoPermission(filename, #io_write);
        permission.assert();
        file = new TextIo(filename, #io_write);
        if (!file)
        throw Exception::Error;
        file.outRecordDelimiter(#delimiterCRLF);
        file.outFieldDelimiter(";");
        while select carTable
        {
            con = connull();
            con = conins(con, 1, carTable.CarId);
            con = conins(con, 2, carTable.CarBrand);
            con = conins(con, 3, carTable.Mileage);
            con = conins(con, 4, carTable.Model);
            con = conins(con, 5, carTable.ModelYear);
            file.writeExp(con);
        }
    }
        
    catch(Exception::Error)
    {
        error("You do not have access to write the file to the selected folder");
    }
    
        CodeAccessPermission::revertAssert(); 
     }


     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans