
{"id":103,"date":"2017-04-07T13:21:45","date_gmt":"2017-04-07T03:21:45","guid":{"rendered":"http:\/\/bakke.online\/?p=103"},"modified":"2017-04-07T13:21:45","modified_gmt":"2017-04-07T03:21:45","slug":"retrieving-datedatetime-from-ado-net-into-a-record-field","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/04\/07\/retrieving-datedatetime-from-ado-net-into-a-record-field\/","title":{"rendered":"Retrieving date\/datetime from ADO.NET into a record field"},"content":{"rendered":"<p>In Dynamics AX, you can use ADO.NET to retrieve data from external data sources. \u00a0This is quite easy to do for text and numeric data types, but not for Date or DateTime data types.<\/p>\n<p>If you try a simple assign, you&#8217;ll end up throwing an exception:<\/p>\n<pre>blocks.Field1 = reader.get_Item( <span style=\"color: #993300;\">\"REGISTRATIONDATE\"<\/span> );<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-104\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/wrongtype.png\" alt=\"\" width=\"419\" height=\"87\" \/><\/p>\n<blockquote><p>Error executing code: Wrong argument types in variable assignment.<\/p><\/blockquote>\n<p>Using SqlDataReader.GetDataTypeName() to check the data type, it shows DateTime2. \u00a0The destination field is a datetime field, but this problem as well as the solution happens even for plain date fields.<\/p>\n<p>So, we might try to use SqlDataReader.GetDateTime() instead, but that results in exactly the same error.<\/p>\n<p><!--more--><\/p>\n<p>The problem is happening because what is returned by the get_Item() and GetDateTime() is actually a CLR object, not a native X++ object. \u00a0As is documented for the CLR Interop layer in AX, AX can&#8217;t chain operations on CLR objects, and what is happening above is actually an implicit conversion feeding into an assignment operation.<\/p>\n<p>To work around this, we need to stuff the return value from get_Item() into a local variable, and then assign that variable into the record field instead. \u00a0This is called a bounce variable.<\/p>\n<pre><strong><span style=\"color: #0000ff;\">utcDateTime<\/span> <\/strong>dateTimeTmp;\n\n...\n\ndateTimeTmp = reader.get_Item( <span style=\"color: #993300;\">\"REGISTRATIONDATE\"<\/span> );\nblocks.Field1 = dateTimeTmp;<\/pre>\n<p>A bit non-intuitive, because we&#8217;re still dealing with an implicit conversion feeding into an assignment operation, it just happens to work with a local variable.<\/p>\n<p>This same problem is also happening for decimal\/numeric fields and int-values to be loaded into an enum field. \u00a0Again, the solution is the same: Create a local bounce for the value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Dynamics AX, you can use ADO.NET to retrieve data from external data sources. \u00a0This is quite easy to do for text and numeric data types, but not for Date or DateTime data types. If you try a simple assign, you&#8217;ll end up throwing an exception: blocks.Field1 = reader.get_Item( &#8220;REGISTRATIONDATE&#8221; ); Error executing code: Wrong &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/07\/retrieving-datedatetime-from-ado-net-into-a-record-field\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Retrieving date\/datetime from ADO.NET into a record field&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[2,3,6],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-dynamics-ax","category-uncategorized","tag-ax2012r3","tag-dynamics-ax","tag-error-messages"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":0,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}