4 min read

Add a reference to .NET Business Connector

In the next example, we create a new project in Visual Studio and add the reference to the project. You can skip this step if you already have a project where you would like to use the .NET Business Connector.

Open Visual Studio and create a new project as shown in the next screenshot:

 	 Microsoft Dynamics AX 2009 Programming: Getting Started

When you click on OK, you will have a new C# file called Program.cs. Before we start writing any code in the file, we have to add the .NET Business Connector as a reference.

In the Solution Explorer right-click on the References node and select Add Reference.

Microsoft Dynamics AX 2009 Programming: Getting Started

In the form that opens select Microsoft.Dynamics.BusinessConnectorNet.dll under the Browse tab.

The file is found in the ClientBin catalog of your AX installation. The default path is:

C:Program FilesMicrosoft Dynamics Ax50ClientBin

Microsoft Dynamics AX 2009 Programming: Getting Started

Click on OK and note that the reference has now been added under the References node in the Solution Explorer.

Using the .NET Business Connector in .NET classes

In the next sections of this article we will look at some examples that show how we can use methods in the .NET Business Connector to call AX methods, insert data into AX tables, and read data from AX tables.

Calling a static class method in AX

You now have to go back to AX, open the AOT and find the Global class. Add the following method to the Global class:

static str AxHelloWorld()
{
return "HelloWorld!";
}

In the Visual Studio project we open the Program.cs file again and enter the following code that will create a connection to AX through the .NET Business Connector. We then call the method we created in the Global class before it prints the result to the console:

using System;
using Microsoft.Dynamics.BusinessConnectorNet;
namespace GetAxInfo1
{
class Program
{
/// <summary>
/// This class connects to AX throught the
/// .NET Business Connector and call the static method
/// AxHelloWorld in the Global class in AX.
/// The result is sent to the console (command prompt).
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Axapta ax;
Object axObject;
try
{
// Create a new Axapta object.
ax = new Axapta();
// Logon using the default settings
// in the AX configuration for the .NET
// Business Connector
ax.Logon(null, null, null, null);
// Call the static method and return the
// result to the axObject variable
axObject =
ax.CallStaticClassMethod("Global", "AxHelloWorld");
// Print the result to the console
Console.WriteLine("The message from AX is {0}",
axObject.ToString());
ax.Logoff();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}

Execute the program by pressing Ctrl + F5. You should now see the following result in the console (command prompt) that opens up:

Microsoft Dynamics AX 2009 Programming: Getting Started

If you get a different result it might be because you haven’t set up the .NET Business Connector correctly. If so, please refer to the installation guide to set it up properly. Also, check the Windows Event Viewer to see if there are any error messages that can lead you in the right direction.

Insert data into an AX table

As mentioned earlier in this article, you can use the methods in some of the marshaled classes in the .NET Business Connector to manipulate data. However, it is better to avoid this and instead have AX handle all data selection and manipulation. It would be even better to have your .NET application call a method in AX that does the job and returns the results back to the .NET application again. However, this is not always feasible, for instance, when you would like to insert records from your .NET application to an AX table. The next section will guide you through the steps needed to achieve this.

First, you should create a new project in Visual Studio as we did earlier in this article. In this example, we will call the new project “InsertAxRecord”.

Then you have to add a reference to the .NET Business Connector.

In the Program.cs file (or whatever you call your file), you write something like this:

using System;
using Microsoft.Dynamics.BusinessConnectorNet;
namespace InsertAxRecord
{
class Program
{
static void Main(string[] args)
{
Axapta ax;
AxaptaRecord record;
try
{
// Create AX object and logon to AX
ax = new Axapta();
ax.Logon(null, null, null, null);
// Create a new AxaptaRecord object with
// the name of the table as input parameter
using (record = ax.CreateAxaptaRecord("CarTable"))
{
// Remember to clear the tablebuffer if
// you are inserting inside a loop
record.Clear();
record.InitValue();
// Set the fields in the table
record.set_Field("CARID", "XXX1");
record.set_Field("MODELYEAR", 1998);
record.set_Field("CARBRAND", "FORD");
record.set_Field("MODEL", "F1");
record.set_Field("MILEAGE", 89378);
// Insert the record
record.Insert();
}
// End the AX session
ax.Logoff();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here