Zordey
Zordey

Reputation: 656

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.

Upvotes: 1

Views: 3554

Answers (2)

Skaue
Skaue

Reputation: 783

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. :)

Upvotes: 1

10p
10p

Reputation: 6758

Can you try this code?

using (axRecord = ax.CreateAxaptaRecord("TempTable"))
{
    var xppDateTime = ax.CallStaticClassMethod("Global", "CLRSystemDateTime2UtcDateTime", DateTime.Now);

    axRecord.set_Field("DateField", xppDateTime);

    axRecord.Insert();

}

Upvotes: 1

Related Questions