Microsoft_MVP_banner

CRUD operation in Dynamics AX 2009 through .Net business connector

Friends, rather going into more details about “How to’s” of accessing Dynamics AX through .Net, my main objective is to share my experience of performing CRUD operations in Dynamics AX 2009 through .Net BC.

Here is a sample code;

    // CREATE OR INSERT SAMPLE
    // Create the .NET Business Connector objects.
    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = “AddressState”;
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
        // Create a new AddressState table record.
        using (axRecord = ax.CreateAxaptaRecord(tableName));
        {
            // Provide values for each of the AddressState record fields.
            axRecord.set_Field(“NAME”, “MyState”);
            axRecord.set_Field(“STATEID”, “MyState”);
            axRecord.set_Field(“COUNTRYREGIONID”, “US”);
            axRecord.set_Field(“INTRASTATCODE”, “”);
            // Commit the record to the database.
            axRecord.Insert();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(“Error encountered: {0}”, e.Message);
        // Take other error action as needed.
    }

    // READ SAMPLE
    // Create the .NET Business Connector objects.
    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = “AddressState”;
    // The AddressState field names for calls to
    // the AxRecord.get_field method.
    string strNameField = “NAME”;
    string strStateIdField = “STATEID”;
    // The output variables for calls to the
    // AxRecord.get_Field method.
    object fieldName, fieldStateId;
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
        // Create a query using the AxaptaRecord class
        // for the StateAddress table.
        using (axRecord = ax.CreateAxaptaRecord(tableName));
        {
            // Execute the query on the table.
            axRecord.ExecuteStmt(“select * from %1″);
            // Create output with a title and column headings
            // for the returned records.
            Console.WriteLine(“List of selected records from {0}”, tableName);
            Console.WriteLine(“{0}t{1}”, strNameField, strStateIdField);
            // Loop through the set of retrieved records.
            while (axRecord.Found)
            {
                // Retrieve the record data for the specified fields.
                fieldName = axRecord.get_Field(strNameField);
                fieldStateId = axRecord.get_Field(strStateIdField);
                // Display the retrieved data.
                Console.WriteLine(fieldName + “t” + fieldStateId);
                // Advance to the next row.
                axRecord.Next();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(“Error encountered: {0}”, e.Message);
        // Take other error action as needed.
    }

    // UPDATE SAMPLE
    // Create an Axapta record for the StateAddress table.
    axRecord = ax.CreateAxaptaRecord(tableName);
    // Execute a query to retrieve an editable record where the name is MyState.
    axRecord.ExecuteStmt(“select forupdate * from %1 where %1.Name == ‘MyState’”);
    // If the record is found then update the name.
    if (axRecord.Found)
    {
        // Start a transaction that can be committed.
        ax.TTSBegin();
        axRecord.set_Field(“Name”, “MyStateUpdated”);
        axRecord.Update();
        // Commit the transaction.
        ax.TTSCommit();
    }

    // DELETE SAMPLE
    // Execute a query to retrieve an editable record
    // where the name is MyStateUpdated.
    axRecord.ExecuteStmt(“select forupdate * from %1 where %1.Name == ‘MyStateUpdated’”);
    // If the record is found then delete the record.
    if (axRecord.Found)
    {
        // Start a transaction that can be committed.
        ax.TTSBegin();
        axRecord.Delete();
        // Commit the transaction.
        ax.TTSCommit();
    }

Share:

Related Posts

Microsoft Copilot
admin

Exploring Microsoft Copilot Architecture

Microsoft Copilot isn’t just another AI tool; it’s a comprehensive platform designed to be your indispensable companion, enhancing productivity, fostering creativity, and facilitating information comprehension all through a user-friendly chat interface. The concept of Copilot emerged two years ago when Microsoft introduced GitHub Copilot, aiming to assist developers in writing

Read More »
How to enable new Microsoft teams - Public Preview!
Microsoft Teams
Faisal Fareed

How to enable new Microsoft teams – Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Microsoft Teams admin center [Ask admin in your organization if you don’t have access] and follow path Teams > Teams update policies > Click on an existing

Read More »

Send Us A Message

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Post

Exploring Microsoft Copilot Architecture

Exploring Microsoft Copilot Architecture

Microsoft Copilot isn’t just another AI tool; it’s a comprehensive platform designed to be your indispensable companion, enhancing productivity, fostering creativity, and facilitating information comprehension all through a user-friendly chat interface. The concept of Copilot emerged two years ago when Microsoft introduced GitHub Copilot, aiming to assist developers in writing…

How to enable new Microsoft teams – Public Preview!

How to enable new Microsoft teams – Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Microsoft Teams admin center [Ask admin in your organization if you don’t have access] and follow path Teams > Teams update policies > Click on an existing…

Electronic Reporting: Send vendor payments to external azure storage via X++

Electronic Reporting: Send vendor payments to external azure storage via X++

Electronic Reporting module in Microsoft Dynamics 365 Finance Operation lets you archive file generated by ER at SharePoint location and in Azure Storage as per this link Archive ER destination type – Finance & Operations | Dynamics 365 | Microsoft Learn. APIs can be used to check message status and read…

FaisalFareed@2025. All rights reserved

Design by T3chDesigns