Asiri Dissanayaka
Asiri Dissanayaka

Reputation: 510

Insert pdf file using Microsoft.Dynamics.BusinessConnectorNet

I need to insert pdf file to Ax database using Microsoft.Dynamics.BusinessConnectorNet.dll through a C# application.

I refer this article https://msdn.microsoft.com/EN-US/library/aa868997(v=ax.50).aspx.

AX column type is Container.

When I insert a binary array ArgumentException says:

The supplied method arguments are not valid.

What should be the datatype to insert file into AX Data base?

Upvotes: 3

Views: 462

Answers (2)

Maxim Lazarev
Maxim Lazarev

Reputation: 1254

I guess you have to use AxaptaContainer class to pass values in container.

AxaptaRecord axRecord;

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 container for record field.
        AxaptaContainer axContainer = ax.CreateAxaptaContainer();
        axContainer.Add("Some Data");

        axRecord.set_Field("ContainerField", axContainer);

        // Other fields

        // Commit the record to the database.
        axRecord.Insert();
    }
}
catch (Exception e)
{
    Console.WriteLine("Error encountered: {0}", e.Message);
    // Take other error action as needed.
}

I haven't tested it, so please provide some feedback so we could improve the solution.

Upvotes: 4

Asiri Dissanayaka
Asiri Dissanayaka

Reputation: 510

        Axapta DynAx = new Axapta();
        AxaptaRecord DynRec;
        string strUserName = "";
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential("", "");

        string tableName = "";

        DynAx.LogonAs(strUserName.Trim(), "", nc, dataAreaId, "en-us","", "");

        try
        {
            using (DynRec = DynAx.CreateAxaptaRecord(tableName))
            {
                var binData = DynAx.CreateAxaptaObject("Bindata");
                var loaded = binData.Call("loadFile", path);
                var data = binData.Call("getData");

                AxaptaContainer axc = DynAx.CreateAxaptaContainer();

                axc.Add(data);


                DynRec.set_Field("ATTACHMENT", axc.get_Item(1));

                // Commit the record to the database.
                DynRec.Insert();
            }
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            DynAx.Logoff();
        }

Authenticate to Ax, get the container from Ax Bindata class and save the record.

Upvotes: 2

Related Questions