Click here to Skip to main content
15,881,870 members
Articles / Programming Languages / C#

Integrate Microsoft Dynamics Axapta with Temperature Conversion C# Application

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
25 Sep 2010CPOL9 min read 55.4K   1.1K   15   10
This article is having a C# code DLL to convert a Farenhite temperature into Celsius and vice versa. Further I have used this DLL in Axapta to integrate the “Convert Temperature” UI with Axapta.

Introduction

This article is dedicated to Microsoft Dynamics Axapta and the C# community. Microsoft dynamics Axapta is an ERP for large scale industries. Every large organization has their own legacy system which they want to integrate with Microsoft dynamics Axapta so that without much effort they can leverage Microsoft Dynamics Axapta and their existing C# application. To understand it better, assume that the clients' C# application is having the UI with some functionality and we want to utilize that functionality in Microsoft dynamics Axapta without building that functionality from scratch but instead only passing the desire input for C# application from Microsoft Dynamics Axapta and in turn getting the output from C# application.

In order to understand the advantage of this article, please read the following scenario.

Suppose Client A is having one application B which is working perfectly fine to accomplish their small business goal. Now after some time, they decided to implement ERP Microsoft Dynamics Axapta to streamline and enhance their business process and to obtain profit. Suppose, the functionality B is not in ERP Microsoft Dynamics Axapta and client does want to implement that functionality in Axapta as already they are satisfied with that and application is having the DLL so that it can be used by other application. Hence to achieve this, we can integrate the application B of Client A to Axapta and thus by achieving this type of integration, client does not need to invest time and money in implementing the functionality to a larger extent. This article will also help any ISV partner for Microsoft Dynamics Axapta to have a POC of how to integrate the Axapta with C# application. Any ISV partner can develop their add-on and then expose their functionality as an DLL with API, which in turn Axapta will be utilized so that Axapta functionality is integrated to Add-on.

To develop a POC, I had developed a C# DLL “CvrtC2Frnt.dll” with project name as CvrtC2Frnt. After that “CvrtC2Frnt.dll” is utilized in Axapta to integrate the Convert Temperature application with Axapta table and form.

I am providing the source code for the following items:

  1. CvrtC2Frnt.dll
  2. Axapta code to integrate with Convert Temperature application

In addition to the above, I am also providing a sample application to test the CvrtC2Frnt.dll without using Axapta too.

There are many ways to integrate Axapta with .NET applications. In my upcoming articles, I will be covering some other aspects of integrating Axapta with .NET applications.

Let's begin...

Step 1: Creation of “CvrtC2Frnt.dll"

Create a C# project of type Class Library with the namespace CvrtC2Frnt. Let us assume that we have named the solution, "CvrtC2Frnt."

Step A

First, we will create the “CovertTemp” class. We will create the following members as follows:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CvrtC2Frnt
{
    public class ConvertTemp
    {
        public static double sourceTemp=0, destTemp=0;
        public static bool Faren = false;
        public static  double ConvertCelsiusToFahrenheit(double c)
        {
            destTemp = 0;
            destTemp = ((9.0 / 5.0) * c) + 32;
            return ((9.0 / 5.0) * c) + 32;
        }
 
        public static double ConvertFahrenheitToCelsius(double f)
        {
            destTemp = 0;
            destTemp = (5.0 / 9.0) * (f - 32);
            return (5.0 / 9.0) * (f - 32);
        } 
    }
}

The above code clearly specifies that two functions are created to convert temperature from Fahrenheit to Celsius and vice versa.

Step B

Now, we will add new windows form “ShowTemperature”, which will contain the UI for leveraging the functionality and interface to applications like Axapta.

Add the following controls on the form:

  1. Text Box of name txtSourceTmp
  2. Text Box of name txtDestTmp
  3. Label Box of name lblsrcTemp with blank value in Text property
  4. Label Box of name lbldesTemp with blank value in Text property
  5. One Command button with Text as Convert and name as btnConvert

Note: Text of labels will be displayed dynamically depending upon the type of conversion like if conversion is taking place from Fahrenheit to Celsius, then label lblsrcTemp will contain the Text as Fahrenheit and “lbldesTemp” as Celsius.

image001.jpg

Step C

Now, create the following functions of class ShowTemperature:

C#
public ShowTemperature(double Temperature, Boolean Fahrenheit)
        {
            InitializeComponent();
            txtSourceTmp.Text = Temperature.ToString();
            ConvertTemp.sourceTemp = Temperature;
            ConvertTemp.Faren = Fahrenheit;
           
        }
 
//Above code clearly specifies that we are initializing the label and text 
//box with vales coming from user input.
 
        private void btnConvert_Click(object sender, EventArgs e)
        {
 
            if (ConvertTemp.Faren == true)
                txtDestTmp.Text = Convert.ToString
		(ConvertTemp.ConvertFahrenheitToCelsius(ConvertTemp.sourceTemp));
 
            if (ConvertTemp.Faren == false)
                txtDestTmp.Text = Convert.ToString
		(ConvertTemp.ConvertCelsiusToFahrenheit(ConvertTemp.sourceTemp));
        }
 
// above code is triggered when Convert button is click to convert the temperature.

        private void ShowTemperature_Load(object sender, EventArgs e)
        {
            if (ConvertTemp.Faren == true)
            {
                lblsrcTemp.Text = "Farenheit";
                lbldesTemp.Text = "Celsius";
            }
            else
            {
                lblsrcTemp.Text = "Celsius";
                lbldesTemp.Text = "Farenheit";
            }
        }
// above  code specifies that form ShowTemperature is displayed dynamically 
// by label values depending
// upon the type of conversion is selected from Axapta or any other application.

Step D

Now, create the function ShowResult in class ConverTemp as follows:

C#
public void ShowResult(double Temperature, bool Farenhite)
        {
            ShowTemperature sT = new ShowTemperature(Temperature, Farenhite);
            sT.Show();
           
        }

The above function in created to first call the constructor of ShowTemperature class with Temperature and type of temperature (Fahrenheit or Celsius) values. Afterwards, ShowTemperature UI is called to display corresponding UI.

Step E

This step is optional as already our DLL is created but to test before we actually use it in Axapta, we can perform Step E.

Add a New project of type Windows Form Application to the existing solution. Put the name of project as ShowUI. Add a reference of CvrtC2Frnt.dll in references.

Now design the form as given below:

image002.jpg

The above form will work as follows:

If the value in Temperature Text box is as 50 and Fahrenheit check box is checked out, then it means that Temperature is given in Fahrenheit. When user will click on button Show Convert Temperature UI, then UI which was created in step B will be displayed.

Now the code behind Show Convert Temperature UI button is as follows:

C#
private void btnShowUI_Click(object sender, EventArgs e)
        {
            ConvertTemp ct = new ConvertTemp();
            if (chkFaren.Checked == true)
                ct.ShowResult(Convert.ToInt32(txtTemperature.Text), true);
            else
                ct.ShowResult(Convert.ToInt32(txtTemperature.Text), false);
        }

Now build the application and run it to test the result.

image004.jpg

Click on this to get the UI for converting Fahrenheit to Celsius.

image006.jpg

Click on Convert button to convert Fahrenheit to Celsius

image007.jpg

Step 2: Integrate the CvrtC2Frnt.dll with Axapta

To perform step 2, you need to have Microsoft Dynamics Axapta installed in your system. Assuming that this is installed and running, follow the steps given below:

Step A

  1. Copy CvrtC2Frnt.dll file into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.
  2. Open AOT (Application object tree). AOT is a development environment in Axapta. By using AOT, we can customize and develop new functionalities in Axapta.

    image009.jpg

    Click to Open AOT or Press CTRL+D

    image010.jpg

  3. Now under AOT, go to References and expand it. Then click over Add Reference. Browse the CvrtC2Frnt.dll file into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.

    image012.jpg

    Click on Browse and select file CvrtC2Frnt.dll
  4. Click on Open after selecting CvrtC2Frnt.dll file from into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.

    image014.jpg

    Click on Open
  5. Click on Ok to add the reference.

    image015.jpg

    Click on Ok to add reference.

image016.jpg

Step B

  1. Now create a New Project in Axapta name as ConverTemp. Press CTRL+SHIFT+P to open Project window and then right click on private node of Project window. Select New-> Project to create New Project.

    image018.jpg

    Click on Project to create New Project

    Rename the new project as ConverTemp.

  2. Create two groups and rename it to Form and Class.

    image020.jpg

    Click on Group two times to create new Groups and rename it to Form and Class
  3. Now right click on Class node under ConverTemp project and click on New-> Class to create new Class. Rename it to Temperature.

    image022.jpg

    Click on Class to create new Class in Axapta
  4. Now go to Class Declaration which is under and click Edit to open it.

    image024.jpg

    Click on Edit to edit class declaration of ConvertTemp Class
  5. Write the following code in class declaration:
    C#
    class Temperature
    {
    CvrtC2Frnt.ConvertTemp ct;
    }

    The above code is creating the object for ConvertTemp class which is defined in CvrtC2Frnt DLL.

    image025.jpg

  6. Add a new method ConvertTemperature to Temperature class in Axapta. Right click on temperature class and click on OverideMethod->New to add a new method. Rename the new method as ConvertTemperature.

    image027.jpg

    Click on New under OverideMethod to create new Method.
  7. Right click on ConvertTemperature method and select Edit to add code to it.

    image028.jpg

    Click on Edit to edit the class

    Add the following code to the editor:

    C#
    void ConvertTemperature(real Temperature, Boolean Fahrenheit)
    {
        ct = new CvrtC2Frnt.ConvertTemp();
        ct.ShowResult(Temperature, Fahrenheit);
    }

    image029.jpg

    The above function is taking two parameters as Temperature and Fahrenheit. Both parameters are passed to the ShowResult API defined in CvrtC2Frnt DLL.

  8. Right click Temperature class and select Save to save and compile the class.

    image030.jpg

    Click on Save to save and compile the Temperature Class

Step C

  1. We will create two new fields in SalesTable of Axapta:
    1. Temperature
    2. Farenhite
  2. Open AOT and go to data dictionary. Click on Tables and search for SalesTable.

    image031.jpg

    Select SalesTable.

    image032.jpg

    Select SalesTable

    image032.jpg

    Go to fields and right click over it and select new field of type real.

    image033.jpg

    Rename it Temperature.

    Add a new field as define of type as enum.

    image035.jpg

    Click Enum

    Rename it to Farenhite.

    image036.jpg

    Select the value in extended type property as Temp Type which is again a user enum of type NoYes. Hence before creating Farenhite field, first create the User Enum TempType.

    To create the TempType Enum, go to Extended Data Types in Data dictionary and right click select new->Enum.

    image038.jpg

    Click ExtendedData Type -> Click Enum

    Rename it to TempType and go to property. Select EnumType property value as NoYes.

    image040.jpg

    Select NoYes

    image040.jpg

    Now save it and go to SalesTable under DataDictionary. Search the Farenhite field created earlier and then go to ExtendedData Type property and select TempType as value.

    Select TempType

    image042.jpg

    Now save it.

  3. Now we will add two fields created in the above step to SalesTable Form.

    Go to SalesTable form under Form section in AOT. Select Designs->Design->Group:Table->Tab:TabHeader->TabPage:TabHeaderOverview->Grid:GridHeader and drop Temperature, Farenhite field from Datasource of SalesTableForm in it.

    image043.jpg

    Now Save it.

  4. Now Drag and drop the SalesTable Form from AOT to Form group under ConverTemp project in Axapta. Please note that Form group is created under point 2 of Step B.

    image044.jpg

  5. Now, we will create the Convert Temperature button on SalesTable Form.

    Go to SalesTable->Designs->Design->ButtonGroup:ButtonsHeader and right click to select new CommandButton control on SalesTable form.

    image045.jpg

    Click on CommandButton to create new Command button
  6. Right click on new command button and click properties to open property window. Go to Text attribute and enter value as ConverTemperature.

    image047.jpg

    Enter Convert Temperature -> Click on properties.
  7. Now we will create a new method clicked associated to ConvertTemperature button. This method we will create by right clicking on Methods under ConvertTemperature button and then selecting Overide method and then selecting Clicked.

    Methods->Overide Method->Clicked

    image048.jpg

    Select Clicked to override click method
  8. In the editor window, copy the following code:
    C#
    void clicked()
    {
        Temperature tem = new Temperature();
        ;
        tem.ConvertTemperature( element.salesTable().Temperature, 
    	element.salesTable().Farenhite);
        super();
    }

The above code is creating the object of class Temperature created in Axapta. Then we are calling ConvertTemperature function defined in Temperature class of Axapta with parameters as Temperature and Farenhite from SalesTable in Axapta. Earlier in point 1 of Step C, we had created two new fields in SalesTable of Axapta, i.e.:

  • Temperature (Data type: real)
  • Farenhite (Data Type: Enum with boolean)
  • Save the Code and run the SalesTable Form. To run go to Account Receivable module of Axapta and then click Sales Order.

    image049.jpg

Step D

Click on Account receivable

Double click on Sales Orders under Account Receivable module of Axapta. Under Sales Order Grid, select any record and double click and go overview tab of Sales Order form. Scroll to find out the Temperature and Farenheit field.

image051.jpg

First click on Sales Order

image056.jpg

Enter Temperature as 59 and check the Farenhite Check. (If Farenhite is checked, then the temperature in Temperature field is in Fahrenheit and upon clicking Convert Temperature, command button will convert it to Celsius. If the Farenhite checkbox is not checked, then the temperature in Temperature field is in Celsius and upon clicking Convert Temperature command button will convert it to Fahrenheit).

  • Click on Convert Temperature Command button on sales order form.

    image056.jpg

    Click on ConvertTemperature Button
  • Click on Convert Button of Convert Temperature Form.

    image057.jpg

    You got the temperature in Celsius!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kanasz Robert1-Dec-11 2:51
professionalKanasz Robert1-Dec-11 2:51 
QuestionAX CLR Interop Question Pin
stationhill19-Oct-10 12:20
stationhill19-Oct-10 12:20 
AnswerRe: AX CLR Interop Question Pin
abhishek pareek200922-Oct-10 9:09
abhishek pareek200922-Oct-10 9:09 
GeneralRe: AX CLR Interop Question Pin
stationhill22-Oct-10 9:37
stationhill22-Oct-10 9:37 
GeneralRe: AX CLR Interop Question Pin
abhishek pareek200925-Oct-10 7:59
abhishek pareek200925-Oct-10 7:59 
GeneralGood Job! Pin
danielAus22-Sep-10 0:43
danielAus22-Sep-10 0:43 
GeneralMy vote of 5 Pin
danielAus22-Sep-10 0:38
danielAus22-Sep-10 0:38 
GeneralMy vote of 5 Pin
dyamicsworld18-Sep-10 6:31
dyamicsworld18-Sep-10 6:31 
GeneralAwesome Pin
andysymon16-Sep-10 7:45
andysymon16-Sep-10 7:45 
GeneralMy vote of 5 Pin
jom0115-Sep-10 9:01
jom0115-Sep-10 9:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.