Thursday, June 02, 2011

How to use .Net Business Connector for Dynamics Ax 4.0 within .Net

Requirement :
1) Ax 4.0 with .Net Business Connector
2) Visual Studio 2005 (preferred)

Business Connector enables third-party applications to interface with Microsoft Dynamics AX as though they were a native Microsoft Dynamics AX client. Applications built using the Microsoft .NET Framework or ASP.NET can now interface with Microsoft Dynamics AX using the .NET Business Connector. The .NET Business Connector is integrated with Enterprise Portal and Web services. You can leverage the benefits of these other applications and services by installing the .NET Business Connector.


Open your visual studio.
Click on File>New>Project. New Project window will open. Select Visual C# from Project types and Windows Application from Templates. Give a name in the Name field and click on OK button.














By default Form1 will be added to your project.
From Toolbox add a button and a textbox to the form as shown below:



















Double click on the button.
Now see the default code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DotNet2Ax //your project name
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

}
}
}

Click on menu Project >Add Reference…
It will give you the dialog as below:

















Click on Browse tab and browse the Ax 4.0 Client folder then Bin and locate Microsoft.Dynamics.BusinessConnectorNet.dll as shown below:


















Click OK button.

Now add the below lines in the button1_Click method.

Microsoft.Dynamics.BusinessConnectorNet.Axapta DAX = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();
Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord DAXRec;
DAX.Logon(null, null, null, null);
DAXRec = DAX.CreateAxaptaRecord("CustTable");
DAXRec.ExecuteStmt("select firstonly * from %1");
textBox1.Text = (string) DAXRec.get_Field("name");

After adding the lines it will look like:
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Dynamics.BusinessConnectorNet.Axapta DAX = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();
Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord DAXRec;
DAX.Logon(null, null, null, null);
DAXRec = DAX.CreateAxaptaRecord("CustTable");
DAXRec.ExecuteStmt("select firstonly * from %1");
textBox1.Text = (string)DAXRec.get_Field("name");

}

Now click on Build>Build Solution.
Click on Debug>Start Debugging. Your Form1 will run. Click on the button and see the value of CustTable field ‘Name’.



















Code Description:
DAX.Logon(null, null, null, null);
The above method takes logon criteriaon/parameters
Company, Language, ObjectServer, Configuration all are in string

DAXRec = DAX.CreateAxaptaRecord("CustTable");
In the above line table name has been specified.

DAXRec.ExecuteStmt("select firstonly * from %1");
In the above line select statement has been specified.

textBox1.Text = (string)DAXRec.get_Field("name");
In the above line selected record of field name is being assigned to the textBox control.

2 comments:

  1. I have two tables
    1)ParentTable(PTableId, Name)
    2)ChildTable(CTableId,PTableId,Name)

    How can i work with inner join with above two tables to get list of records, i dont want to execute multiple executesmpt with axaprecord

    Can you please help me, I need solution ASAP.

    Regards,
    Sachin Kotak

    ReplyDelete
    Replies
    1. Sachin, you can create a view in AOT and call this view with the same way than a table. But also you must know: views in AX may be a hell for some joins. In any case may be a solution!

      Delete

Enable UAT database over OneBox DevTest environment using JIT

 Dear Friends, In this Article I will show you how to enable UAT database access for Development machine using just-in-time (JIT). Many time...