The given value of type String from the data source cannot be converted to type nvarchar of the specified target column when syncing Retail Data to Channel Database

One of our customer are going to use the AX 2012 R3 Retail solution and the setup of the Async Server – Client in the TEST environment was setup and tested. The application consultants started importing the product data to AX and suddenly the sync stopped with this error:

Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.ProcessSourceRequestHeaderException: ProcessTargetRequestHeader failed due to an InvalidOperationException.connectionString: Data Source=XXXXXX;Initial Catalog=ClientChannelDB;Integrated Security=True;Connect Timeout=60;Application Name=”Commerce Data Exchange Async Client” —> System.InvalidOperationException: The given value of type String from the data source cannot be converted to type nvarchar of the specified target column. —> System.InvalidOperationException: String or binary data would be truncated.

After having a look online I found that there is no sync of the schema between the AX database and the Channel Database (!!!). This meant that when we imported data to AX everything worked but when we did the sync jobs it failed. The reason being that some field types did not match.

After talking to MS support I got a script to try to figure out which fields were wrong

select t1.table_name, t1.column_name, t1.character_maximum_length, t2.character_maximum_length
from MicrosoftDynamicsAX.INFORMATION_SCHEMA.columns t1
inner join RetailHoustonStore.INFORMATION_SCHEMA.columns t2
on t1.table_name = t2.table_name
and t1.column_name = t2.column_name
and t1.character_maximum_length != t2.character_maximum_length order by TABLE_NAME

this query finds all of the fields which are not matched between the AX database and the Channel Database. With some help we found that the issue probably was the field called RETAILVARIANTID which was present in INVENTDIMCOMBINATION and INVENTITEMBARCODE

We ran these SQL queries:

ALTER TABLE [ClientChannelDB].ax.INVENTDIMCOMBINATION ALTER COLUMN RETAILVARIANTID nvarchar(25) NOT NULL;
ALTER TABLE [ClientChannelDB].ax.INVENTITEMBARCODE ALTER COLUMN RETAILVARIANTID nvarchar(25) NOT NULL;

And there was also a view called INVENTDIMCOMBINATION which we scripted out to a query windows, deleted and recreated again. This solved the issue.

System.InvalidOperationException: The given value of type String from the data source cannot be converted to type datetime of the specified target column.

I had an issue today with the sync between Async Server and Client. Some of the jobs worked and some threw an error

Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.ProcessSourceRequestHeaderException: ProcessTargetRequestHeader failed due to an InvalidOperationException.
connectionString: Data Source=XXXXXXXX;Initial Catalog=ClientChannelDB;Integrated Security=True;Connect Timeout=60;Application Name=”Commerce Data Exchange Async Client” —> System.InvalidOperationException: The given value of type String from the data source cannot be converted to type datetime of the specified target column. —> System.FormatException: Failed to convert parameter value from a String to a DateTime. —> System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   — End of inner exception stack trace —
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
   — End of inner exception stack trace —
   at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
   at System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
   at System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
   at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader)
   at Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.SCSqlWriteRequestRunner.BulkCopyData(SqlConnection connection, SqlTransaction transaction)
   at Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.SCSqlWriteRequestRunner.Run(SqlConnection connection, SqlTransaction transaction)
   at Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.SCSqlTargetRequestHandler.ProcessWriteRequest(SqlConnection connection, SqlTransaction transaction)
   at Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.SCSqlTargetRequestHandler.ProcessTargetRequestHeader(ISCTargetRequestHeader targetRequestHeader)
   — End of inner exception stack trace —
   at Microsoft.Dynamics.Retail.StoreConnect.Request.SQLHandler.SCSqlTargetRequestHandler.ProcessTargetRequestHeader(ISCTargetRequestHeader targetRequestHeader)
   at Microsoft.Dynamics.Retail.SynchClient.Core.DownloadAgent.ApplySessionFileToClientDatabase(SessionManager sessionMgr, String fileName)

The error turned out to be a mismatch in the configuration of the Channel Data Group where I had set the force locale to EN-UK instead of EN-US and apprently there is a difference in the date format. After changeing this parameter everything worked again.