The Microsoft Dynamics AX development environment enables you to integrate a .NET-connected application with Microsoft Dynamics AX. This feature gives developers a simplified way to develop integrations with Microsoft Dynamics AX using standard Microsoft developer tools such as Visual Studio .NET and the .NET Framework.

This walkthrough illustrates the following tasks:

・ Create a C# Windows project

・ Add a reference to .NET Business Connector

・ Create the layout of a form

・ Add code behind the form

・ Retrieve a record using a where clause

Prerequisites

To complete this walkthrough, you will need:

・ Microsoft Dynamics AX with data in the AddressState table

・ Visual Studio 2005 or Visual Studio 2008

・ 2.0 .NET Framework or Windows Server SDK

Creating a C# Windows Project

The first step is to create a Windows application that will access data in Microsoft Dynamics AX.

To create a C# Windows project

1. Open Visual Studio.

2. From the File menu, click New, and then click Project to display the New Project dialog box.

3. In the Project types pane, click Visual C#. In the Templates pane, click Windows Application.

4. Set the name to DisplayAddressState.

5. To change the location of the solution directory, click Browse, and specify a new location.

6. Click OK.

Adding a Reference to .NET Business Connector

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 assembly.

To add a reference to .NET Business Connector

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

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

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



Note

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



4. Click OK.

Creating the Layout of a Form

After you have created DisplayAddressState, you can add controls to the form by dragging them from the toolbox.

To create the layout a form

1. In the toolbox, click the Label control, and then add it to the form.

2. Click the TextBox control and add it to the form next to the Label.

3. Click Button and add it to the form next to the TextBox.

Adding Code Behind the Form

In this section, you will add code behind the form to extract the data from Microsoft Dynamics AX. You will use the AxaptaRecord class to execute a query. This query will extract data from Microsoft Dynamics AX. You will retrieve and display customer data on the controls that you have added to the Windows form.

To add code behind the form

1. On the form, double-click button1 to access the DisplayAxCustomer.Form1 code.

2. Add the following code to the button1_Click method.



C#

  Microsoft.Dynamics.BusinessConnectorNet.Axapta DynAx = new

Microsoft.Dynamics.BusinessConnectorNet.Axapta();

Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord DynRec;

// Authenticate the user and establish a session.

DynAx.Logon(null, null, null, null);

// Define the record as the AddressState table.

DynRec = DynAx.CreateAxaptaRecord("AddressState");

// Define the query that will run on the AddressState records.

// This will return all the data in the AddressState table with

// the cursor positioned at the first record in the table.

DynRec.ExecuteStmt("select * from %1");

// Check if the query returned any data.

if(DynRec.Found)

{

// Display the record on the form that was retrieved from the query.

textBox1.Text = (string) DynRec.get_Field("Name");

}



3. Press F5 to run the application.

4. On the form, click button1. The text box displays the first record in your AddressState table.

5. Close the form.

Retrieving a Record Using a Where Clause

In this section, you will modify the code to look for a specific record. You will use a where clause to find a record in your AddressState table. In the following example you will replace the 'your_search_criteria_here' with the name of a record in your AddressState table, for example Michigan.

To retrieve a record using a where clause

1. On the form, double-click button1 to access the DisplayAxCustomer.Form1 code.

2. Declare the following two variables.



C#

  string fieldName = ("Name");

string fieldValue = ("your_search_criteria_here");



3. Change the execute statement to include a where clause.



C#

  DynRec.ExecuteStmt(string.Format("select * from %1 where %1.{0} ==

'{1}'", fieldName, fieldValue));



4. Press F5 to run the application.

5. On the form, click button1. The text box displays the record for the criteria you supplied, like Michigan.




Leave a Reply.