Search code examples
c#dynamics-ax-2009business-connector

Insert dateTime field into Dynamics AX database through business connector


I have been trying to get my C# program to insert records directly into a Dynamics AX 2009 database using the .NET business connector.

So far I can easly insert a string, int, int64, enum (NoYes), but it fails every time I try and insert a dateTime field (in AX the field is defined as UtcDateTime) with the error:

The supplied method arguments are not valid.

I'm sure this is something simple that I am just missing.

Snippet of code:

    using (axRecord = ax.CreateAxaptaRecord("TempTable"))
        {
            // Fails on this line with error: The supplied method arguments are not valid.
            axRecord.set_Field("DateField", DateTime.Now);

            axRecord.Insert();

        }

I have tried passing through as a string and using a dateTime.parseExact, etc., but it still does not seem to work.


Solution

  • It should work.

    I made a static helper class and included it in my project:

    public static class MyHelperExtensions 
    {    
            public static DateTime ParseDateAsString(this string value)
            {
                var culture = new CultureInfo("nb-NO");
                var formats = new[] {"ddMMyyyy", "dd.MM.yyyy"};
                DateTime date;
                return DateTime.TryParseExact(value, formats, culture, DateTimeStyles.None, out date) ? date : DateTime.MinValue;
            }
    }
    

    Then I could pass in values like this:

    record.set_Field("StartDate", subscription.StartDate.ParseDateAsString());
    

    Now this solution assumes Norwegian culture throughout the system. The value "record" is of type AxaptaRecord.

    StartDate is a field that extends (eventually) TransDate.

    I don't see why this shouldn't work for you. Here's some other tips:

    • Look for errors in the Event Viewer

    • Look for spelling errors in you variable contained in strings (like "StartDate" in my example).

    • Start adding breakpoints in both Visual Studio and your x++ code. :)