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)

How To Use a FileOpenDialog in a Form and then I Should be able to import data from .csv file to a Table .

(0) ShareShare
ReportReport
Posted on by

Hi All , 

I have a form in which I want to add a File Open Dialog so that I can select a csv file and then Import data from the (.CSV) file to a table .  

Please let me know . How Could I Do This .

With thanks & regards ,

Nishath Salman N,

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    You'll find an example in tutorial_Form_File form in AOT. Pay special attention to fileNameLookup*() methods there.

  • Community Member Profile Picture
    on at

    Hi Martin ,

    Thanks a lot for your Reply.  I am able to open a file dialog and after selecting a .CSV file when I hit ok then the Following Error Message Pops Up

    Error executing code: Dialog object does not have method 'batchDialogTabPage'.

    (C)\Classes\Dialog\batchDialogTabPage

    (C)\Classes\BatchInfo\dialog - line 22

    (C)\Classes\RunBaseBatch\dialogPost - line 6

    (C)\Classes\RunBase\dialogMake - line 16

    (C)\Classes\RunBase\promptPrim - line 24

    (C)\Classes\RunBase\prompt - line 12

    (C)\Classes\RunBaseBatch\prompt - line 32

    (C)\Classes\FormButtonControl\Clicked

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Unfortunately I don't know your code, therefore I can't comment on it. You'll have to show it to us.

    Also, you never mentioned that you're trying to add it to a RunBase dialog. Please don't forget that we depend on information from you and if you don't tell us enough, you have much lower chance to get a solution.

  • Community Member Profile Picture
    on at

    I am Following Class and Methods :

    class testImport extends RunBaseBatch

    {

    DialogField                 dialogFileName;

           FileName           fileName;

             FormStringControl fileNameControl;

           TextIO                      textIO;

           FileIOPermission          permission;

           Container                  c;

           Empl_Temp            importTable;

           #File

           #avifiles

          #define.CurrentVersion(1)

          #define.Version1(1)

          #localmacro.CurrentList

               fileName

          [tag:end]

    ____________________________________________________________________________________

    public Object dialog()

    {

           Dialog dialog;

    DialogField dialogField;

    Filename _fileName;

    ;

    dialog = new Dialog("FileOpen");

    dialogfield = dialog.addField(typeid(Filenameopen), "File Name");

    dialog.run();

    if (dialog.run())

    {

    _fileName = (dialogfield.value());

    }

    return dialog;

    }

    ________

    public boolean getFromDialog()

    {

           boolean ret;

                   ret = super();

           fileName =  dialogFileName.value();

           return ret;

    }

    __________________________________________________________________________________________

    void importFromTextFile()

    {

           SysOperationProgress    simpleProgress;

           ;

           permission      = new fileIOpermission(fileName,#io_read);

           permission.assert();

           textIO          = new TextIO(fileName,#io_read);

           textIO.inFieldDelimiter("|");

           textIO.inRecordDelimiter('\n');

           simpleProgress  = SysOperationProgress::newGeneral(#AviUpdate,"Import is in progress",100);

           startLengthyOperation();

           while(textIO.status() == IO_Status::Ok)

           {

                           c  = textIO.read();

                           if(conLen(c) > 1)

                           {

                                   setPrefix(this.caption());

                           importTable.clear();

                                   importTable. EmplId                = conpeek(c,1);

                                   importTable.New_EmplName                 = conpeek(c,2);

                                   importTable.New_FirstName   = conpeek(c,3);

                                   importTable.New_LastName                 = conpeek(c,4);

                                   importTable.New_Email = conpeek(c,5);

                                   importTable.New_Telephone = conpeek(c,6);

                                   importTable.New_Designation = conpeek(c,7);

                                   importTable.New_DOB = conpeek(c,8);

               }

                   }

           endLengthyOperation();

    }

    ------------------------------------------

    public container pack()

    {

       return [#CurrentVersion,#CurrentList];

    }

    ---------

    public void run()

    {

       #OCCRetryCount

       ;

       if (! this.validate())

             throw error("Import has been cancel");

           try

       {

                   this.importFromTextFile(); // Import records of txt file

           }

           catch(Exception::Deadlock)

           {

               retry;

           }

           catch(Exception::Error)

           {

                           info ('Import cancelled');

           }

           catch (Exception::UpdateConflict)

           {

                           if (appl.ttsLevel() == 0)

               {

                                if (xSession::currentRetryCount() >= #RetryNum)

                    {

                           throw Exception::UpdateConflictNotRecovered;

                    }

                    else

                    {

                           retry;

                    }

                }

                else

                {

                       throw Exception::UpdateConflict;

                }

           }

    }

    ------------------

    public boolean unpack(container packedClass)

    {

           Version version = RunBase::getVersion(packedClass);

           ;

           switch (version)

       {

                   case #CurrentVersion:

           [version,#CurrentList] = packedClass;

           break;

           default:

           return false;

      }

      return true;

    }

    ------------------

    public boolean validate()

    {

           boolean     ret = true;

       Container   checkType;

       TextIO     errorIo;

       if (! fileName)

       {

                   ret = checkFailed("Please specify file");

           }

           checkType =  Docu::splitFilename(fileNameControl.text());

       if(conpeek(checkType,2) != '.txt' || conpeek(checkType,2) != '.csv')

       {

             ret = checkFailed("file format is incorrect");

       }

       errorIo = new TextIO(fileName, 'r');

       if (! errorIo)

       {

                   return checkFailed(strfmt("@SYS18678", fileName));

       }

    return ret;

    }

    ---------------------

    static void main(Args args)

    {

       testImport             importClass;

       ;

           importClass = new testImport ();

     // if (Empl_Temp.prompt());

       //        Empl_Temp.run();

       importClass.run();

       importClass.prompt();

    }

  • Community Member Profile Picture
    on at

    And In the Form I have Added a button and in it's Clicked method I have Called two methods of the Class testImport ( testImport.Prompt()) , (testImport.run());

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    The reason for the error is that you ignore the RunBaseDialog instance necessary for RunBase framework and you create your own Dialog object. This is a bug. Instead of creating a new dialog, receive the existing one as an argument of dialog() and merely use it.

    Also, you don't have any code for the file name lookup. In case of a dialog, you pass a filter definition to dialog.filenameLookupFileName().

  • Verified answer
    Community Member Profile Picture
    on at

    Martin Please Let me know Where Should I have to change my code. Please Help

  • Community Member Profile Picture
    on at

    Please Help me with the Code Necessary.  It would be a great help to me .

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Like this:

    public Object dialog(DialogRunbase _dialog)
    {
        DialogField dialogField = _dialog.addField(...);
        ...
    
  • Community Member Profile Picture
    on at

    I am getting the following Error Message Martin :

    Error executing code: DialogRunbase object not initialized.

    (C)\Classes\DialogRunbase\addField

    (C)\Classes\testImport\dialog - line 10

    (C)\Classes\RunBase\dialogMake - line 15

    (C)\Classes\RunBase\promptPrim - line 24

    (C)\Classes\RunBase\prompt - line 12

    (C)\Classes\RunBaseBatch\prompt - line 32

    (C)\Classes\FormButtonControl\Clicked

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