Microsoft Dynamics AX 2009
How to: Read Data Using .NET Business Connector

Using .NET Business Connector, you can access Microsoft Dynamics AX data or business logic from a .NET-connected application. The following example provides the code to read data in a Microsoft Dynamics AX table. For a complete working example, see Walkthrough: Integrate an Application with Microsoft Dynamics AX Using .NET Business Connector.

Adding a Reference to .NET Business Connector

This example assumes that you have a project in which you want to access Microsoft Dynamics AX data. You must create a connection using .NET Business Connector. In this section you will add a reference to the .NET Business Connector.

To add a reference to .NET Business Connector

  1. Open Visual Studio.

  2. In Solution Explorer, right-click References and click Add Reference.

  3. In the Add Reference window, click the Browse tab.

  4. Specify the location of Microsoft.Dynamics.BusinessConnectorNet.dll, and then click Add.

    NoteNote

    For a typical install, assemblies are located at C:\Program Files\Microsoft Dynamics AX\50\Client\Bin\.


  5. Click OK.

Reading a Record

In this section, you will execute a query to read records from the AddressState table. You will use the AxaptaRecord class to create a query by calling the Axapta.CreateAxaptaRecord method. This returns the data by using the AxaptaRecord.Field property. The AxaptaRecord.Next method is used to iterate through the list of returned records so that you can display the records. This example displays the output in a console window.

To read data

  • Add the following code to read and display records.

    C#
    // Create the .NET Business Connector objects.
    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = "AddressState";
    
    // The AddressState field names for calls to
    // the AxRecord.get_field method.
    string strNameField = "NAME";
    string strStateIdField = "STATEID";
    
    // The output variables for calls to the 
    // AxRecord.get_Field method.
    object fieldName, fieldStateId; 
    
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
    
        // Create a query using the AxaptaRecord class
        // for the StateAddress table.
        using (axRecord = ax.CreateAxaptaRecord(tableName));
        {
    
            // Execute the query on the table.
            axRecord.ExecuteStmt("select * from %1");
    
            // Create output with a title and column headings
            // for the returned records.
            Console.WriteLine("List of selected records from {0}",
                tableName);
            Console.WriteLine("{0}\t{1}", strNameField, strStateIdField);
    
            // Loop through the set of retrieved records.
            while (axRecord.Found)
            {
                // Retrieve the record data for the specified fields.
                fieldName = axRecord.get_Field(strNameField);
                fieldStateId = axRecord.get_Field(strStateIdField);
    
                // Display the retrieved data.
                Console.WriteLine(fieldName + "\t" + fieldStateId);
    
                // Advance to the next row.
                axRecord.Next();
            }
        }
    }
    
    catch (Exception e)
    {
        Console.WriteLine("Error encountered: {0}", e.Message);
        // Take other error action as needed.
    }

You will see the results in a console window when this code is executed. You can also, create, update, and delete Microsoft Dynamics AX data. For more information, see How to: Create Data Using .NET Business Connector, How to: Update Data Using .NET Business Connector, and How to: Delete Data Using .NET Business Connector.You may want to call X++ business logic in addition to accessing data. For more information, see How to: Call Business Logic Using .NET Business Connector.

See Also

Tags :


Page view tracker