Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Microsoft Dynamic AX 2009 : Working with .NET Business Connector (part 2) - Exception Handling, Accessing Data

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/7/2013 9:22:52 AM

4. Exception Handling

.NET Business Connector has a large set of managed exceptions that can be raised at run time. This set of managed exceptions provides improved granularity, and therefore more flexibility, in handling those exceptions. Most notable are several remote procedure call (RPC)–related exceptions, which you can use to control error handling associated with the connectivity between .NET Business Connector and the AOS. As a general rule, .NET Business Connector does not catch unhandled exceptions (such as OutOfMemoryException). This type of exception is simply propagated to the calling application and prevents .NET Business Connector from masking or hiding such unhandled exceptions.

Refer to the Microsoft Dynamics AX 2009 SDK for more information on the data types, managed classes, and managed exceptions referenced in this section.

5. HelloWorld Example

How do you write C# code that uses .NET Business Connector? The simple example that follows (the .NET Business Connector equivalent of “Hello World”) demonstrates logging on to Dynamics AX. To use the following code, you must be able to log on successfully using the Dynamics AX client. Also, .NET Business Connector must be installed from wherever you execute the code. Create a new project in Microsoft Visual Studio. In the New Project dialog box, select Console Application under Visual C#. This creates the project file structure and files, and presents you with a program named Program.cs. Paste the code in the following example between the curly brackets associated with the Main method. In Solution Explorer, right-click References and choose Add Reference. In the Add Reference dialog box, click the Browse tab. Use the file controls to navigate to the Dynamics AX Client\Bin folder. Select Microsoft.Dynamics.BusinessConnectorNet.dll, and then click OK. This makes .NET Business Connector accessible to the C# application. Now you can build and run the solution.

using System;
using Microsoft.Dynamics.BusinessConnectorNet;
namespace Demo
{
    class HelloWorldFromBC
    {
        public static void Main(string[] args)
        {
            using (Axapta ax = new Axapta())
            {
                try
                {
                    ax.Logon(null, null, null, null);

                    Console.WriteLine("Hello World from Business Connector!");
                     Console.ReadKey();
                    // Logoff
                    ax.Logoff();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception occurred: " + e.Message);
                }
            }
        }
    }
}

					  


First, you must instantiate the Axapta class to authenticate, using one of the methods within Axapta. You use Logon method to provide authentication. If you don’t provide non-null parameter values, the following values, which you can override as needed, are used:

  • Current Windows user

  • Default Dynamics AX company for the user

  • Default language for the user

  • Default active configuration

A message appears on the console, and the Dynamics AX session is terminated using the Logoff method.

6. Accessing Data

To access data in Dynamics AX, you must use the AxaptaRecord class. The following example shows how to retrieve a list of inventory items that are classified as “raw material.”

using System;
using Microsoft.Dynamics.BusinessConnectorNet;

namespace Demo
{
    // ListInvItemRecords
    // Shows how to retrieve and iterate through a list of Dynamics AX records
    class ListInvItemRecords
    {
        public static void Main(string[] args)
        {
            String invItemNameField = "ItemName";
            Object invItemName;
            String invItemIdField = "ItemId";
            Object invItemId;

            using (Axapta ax = new Axapta())
            {
                try
                {
                    // Logon
                    ax.Logon(null, null, null, null);

                    Console.WriteLine("*** List inventory item records");

                    // Instantiate the Dynamics AX record.
                    AxaptaRecord axRecord = ax.CreateAxaptaRecord("InventTable");

                    // Execute a query.
                    axRecord.ExecuteStmt(
                        "select * from %1 where %1.ItemGroupId == 'RawMat'");

                    // Loop through matching Dynamics AX records.
                    while (axRecord.Found)
                    {
                        invItemName = axRecord.get_Field(invItemNameField);
                        invItemId = axRecord.get_Field(invItemIdField);

                        Console.WriteLine(invItemId + "\t" + invItemName);

                        axRecord.Next();
                    }
                    Console.ReadKey();
                    // Logoff
                    ax.Logoff();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception occurred: " + e.Message);
                }
            }
        }
    }
}

					  


Here are the important aspects of the code example:

  • Variables are declared to store the Dynamics AX record data that is retrieved and to hold the field names used.

  • Authentication is the same as in the HelloWorld example.

  • To begin working with a specific type of Dynamics AX record, you must first instantiate an AxaptaRecord object, and you must provide the name or ID of the record as an argument.

  • A query is executed against the Dynamics AX record using ExecuteStmt, which parses the query syntax and replaces the substitution variable (%1) with the name of the record. The query syntax is generic. Dynamics AX executes the query with the exact syntax appropriate for the database being used, whether Microsoft SQL Server or Oracle.

  • A while loop cycles through the records returned from Dynamics AX, which uses another method, Found, on AxaptaRecord to determine that matching records exist.

  • For each record, get_Field retrieves each field value and assigns a value to the appropriate variable declared earlier.

  • To proceed to the next record, the Next method is called.

  • Logoff is called to terminate the session.

Other -----------------
- Integrating Systems Management Server 2003 into Patch Management Processes (part 2) - Authorizing and Distributing Software Updates
- Integrating Systems Management Server 2003 into Patch Management Processes (part 1) - Extending SMS 2003 Functionality for Software Updates
- Microsoft Lync Server 2010 : Planning for Deploying External Services - Edge Server Preparation
- Microsoft Lync Server 2010 : Planning for Voice Deployment - Devices, Response Groups
- Sharepoint 2013 : Expanding My Tasks settings
- Sharepoint 2013 : Using SkyDrive Pro, Using the timeline feature for tasks, Mentioning a colleague feature
- Sharepoint 2013 : Adding a thumbnail to a video
- Exchange Server 2007 : Using OWA Mail Features (part 3)
- Exchange Server 2007 : Using OWA Mail Features (part 2)
- Exchange Server 2007 : Using OWA Mail Features (part 1)
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server