How to Insert Data Using .NET Business Connector...???? Need Help.

This question has suggested answer(s)

 i am trying to insert data using .net business connector but i am some facing problem with exception message "Error encountered: Cannot access a disposed object.". I am unable to grasp that what's the matter. Can anybody help me please. Thanks in advance. here is the code i am using. it is to be mentioned that i read this code from Microsoft msdn online library for Dynamics Ax. I have also included reference Microsoft.Dynamics.BusinessConnectorNet.   here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.BusinessConnectorNet;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // 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();
                    ax.Logoff();
                    Console.WriteLine("Inserted Successfully");
                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error encountered: {0}", e.Message);
                Console.ReadLine();
                // Take other error action as needed.
            }
        }
    }
}

All Replies
  • Perhaps you should try to move

    ax.Logoff();

    to somewhere outside ths "using clause". I am not sure, but if you are logged off when disposing the record (happens when you exit using), this might occur.

    /Jonas

  • Thanks for reply. But its not working i.e.  ax.Logoff();

  • Ok, so this is the line that throws the exception:

    ax.Logoff();

    ??

  • Found the error. You have an extra ; in the code.

    using (axRecord = ax.CreateAxaptaRecord(tableName)) ;

    should be

    using (axRecord = ax.CreateAxaptaRecord(tableName))

    Now you create and dispose the object in the same line... :-)

  • Thanks Man!

    Its working Now.

    this was exactly, going wrong as you pointed