locked
Failed to convert Parameter Value from a Byte[] to a Datetime? RRS feed

  • Question

  • Hi.

    I have an application with the codec down. When I execute the synchronization, the message is showed: Failed to convert Parameter Value from a Byte[] to a Datetime?

     

    public SyncStatistics SyncCestaProduto()  
            {  
                DbServerSyncProvider serverSyncProvider = new DbServerSyncProvider();  
                serverSyncProvider.Connection = conn;  
     
                SyncAgent syncAgent = new SyncAgent();  
                syncAgent.RemoteProvider = serverSyncProvider;  
                syncAgent.LocalProvider = clientSyncProvider;  
     
                SyncGroup CestaSyncGroup = new SyncGroup("CESTAPRODUTOS");  
     
                SyncTable SyncTableCesta = new SyncTable("CESTAPRODUTOS");  
                SyncTableCesta.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;  
                SyncTableCesta.SyncDirection = SyncDirection.DownloadOnly;  
                SyncTableCesta.SyncGroup = CestaSyncGroup;  
                syncAgent.Configuration.SyncTables.Add(SyncTableCesta);  
     
                syncAgent.Configuration.SyncParameters.Add(new SyncParameter("@CodigoAreaAtuacao", Consts.Acesso.Usuario.AreaAtuacao));  
     
                SqlCommand selectNewAnchorCommand = new SqlCommand();  
                string newAnchorVariable = "@" + SyncSession.SyncNewReceivedAnchor;  
                selectNewAnchorCommand.CommandText = "SELECT " + newAnchorVariable + " = min_active_rowversion() - 1";  
                selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.Binary, 1000);  
                selectNewAnchorCommand.Parameters[newAnchorVariable].Direction = ParameterDirection.Output;  
                selectNewAnchorCommand.Connection = conn;  
                serverSyncProvider.SelectNewAnchorCommand = selectNewAnchorCommand;  
     
                SqlParameter filterParameter = new SqlParameter("@CodigoAreaAtuacao", SqlDbType.NVarChar);  
     
     
                SqlSyncAdapterBuilder builder = new SqlSyncAdapterBuilder(conn);           
     
                builder.TableName = "CESTAPRODUTOS";  
                builderbuilder.TombstoneTableName = builder.TableName + "_Tombstone";  
                builder.SyncDirection = SyncDirection.DownloadOnly;  
                builder.CreationTrackingColumn = "CreationDate";  
                builder.UpdateTrackingColumn = "LastEditDate";  
                builder.DeletionTrackingColumn = "DeletionDate";  
     
                builder.DataColumns.Add("CODIGOPRODUTO");  
                builder.DataColumns.Add("CODIGOAREAATUACAO");  
                builder.DataColumns.Add("DATAATUALIZACAO");  
                builder.DataColumns.Add("USUARIOATUALIZACAO");  
                builder.DataColumns.Add("UPLOAD");  
                //builder.DataColumns.Add("LastEditDate");  
                //builder.DataColumns.Add("CreationDate");  
     
                builder.TombstoneDataColumns.Add("CODIGOPRODUTO");  
                builder.TombstoneDataColumns.Add("CODIGOAREAATUACAO");  
                //builder.TombstoneDataColumns.Add("DeletionDate");  
     
                string FilterClause = "CODIGOAREAATUACAO=@CodigoAreaAtuacao";  
     
                builder.FilterClause = FilterClause;  
                builder.FilterParameters.Add(filterParameter);  
                builder.TombstoneFilterClause = FilterClause;  
                builder.TombstoneFilterParameters.Add(filterParameter);  
     
                SyncAdapter customerSyncAdapter = builder.ToSyncAdapter();  
                customerSyncAdapter.TableName = "CESTAPRODUTOS";  
                serverSyncProvider.SyncAdapters.Add(customerSyncAdapter);  
     
                return syncAgent.Synchronize();  
            } 

    What am I doing wrong?

    Help me, please.

    Thanks.

    Marcos Antonio Aguiar Jr
    • Moved by Hengzhe Li Friday, April 22, 2011 5:44 AM (From:SyncFx - Microsoft Sync Framework Database Providers [ReadOnly])
    Thursday, February 5, 2009 5:50 PM

Answers

  • Hi Yunwen.

    The problem was:

    selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.Binary, 1000);


    modified for

    selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.DateTime);

    Now work correct.

    Thanks for help me.

    Marcos Antonio Aguiar Jr
    Monday, February 9, 2009 3:44 PM

All replies

  • Hi,

    Showing the definition of your meta data table on the client and the stack trace when error occurs may help identify the issue.

    I have seen such errors mentioned in the forums.

    I even recently dealt with a similar issue. I used the SQLExpressSyncProvider example with custom tracking (custom tracking relies on timestamp/binary(8) anchors), so my metadata table had binary(2048) as the type of anchor columns.

    Then I switched to SQL 2008 Change Tracking, which uses int64 as anchors, forgeting to update my meta data table, I was getting conversion error from byte[] to int64 or vice versa.

    Another possible issue may be (depending on the client adapter implementation) the serialization of your anchors. Check this thread for clues and even some code that improves/fixes the issue: SQLExpressSyncProvider Example

    I am new to the framework, so pelase read my words with caution

    Thursday, February 5, 2009 7:55 PM
  •  Maybe I've understood that you said, but I'm going to explain what's my probleman.

    I have a table with the following definition:

    IDProduct    NUMERIC(10),
    IDRegion     NUMERIC(10)

    I want to synchronize the client data into the server. I was using the code above, but when the instructions below are not commented, the error is showed.

    builder.CreationTrackingColumn = "CreationDate";  
    builder.UpdateTrackingColumn = "LastEditDate";  
    builder.DeletionTrackingColumn = "DeletionDate";  

    when they are comented I don't get any error but no data is synchronized.

    Do you know what can I do?

    Thank you in advanced.
    Marcos Antonio Aguiar Jr
    Thursday, February 5, 2009 8:20 PM
  • What client provider do you use?

    What is the table structure of your meta data table on the client?

    Can you paste here the stack trace when you get the error.

    Thursday, February 5, 2009 8:47 PM
  • I'm using de SqlCeClientSyncProvider with client provider.

    I change that row and de message error didn't showed again:

    selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.Timestamp);

    Now a I have a other probleman. The filter don't work.

    Do you know why?

    Thanks.


    Marcos Antonio Aguiar Jr
    Friday, February 6, 2009 10:54 AM
  • Yes, I do.
    Friday, February 6, 2009 3:26 PM
  • So, please, What am I doing wrong? Where's the error? If possible, show an example.

    Thanks.
    Marcos Antonio Aguiar Jr
    Friday, February 6, 2009 3:46 PM
  • Hi Marcos, I am sorry I am being smart - I thought it would be funny to give you that answer.

    Again - could you show the relevant code and also show the detailed error description and stack trace when error occurs.

    It is really hard to guess from my location what your code is trying to do.
    Friday, February 6, 2009 5:52 PM
  • two issues were discussed here:

    1. the byte[] to datetime conversion exception: this is like due to the tracking column type and the anchor type ( in the get new anchor command ) mismatch. getnewAnchor command returnd bytep[] and likley the tracking columns are with datetime ( as the columnName indicates ).

    2. the filter won't work. we need more info here.  what is the symptoms ? can you check the selectIncremental insert/update commands on the syncadatper ?

     

    thanks

    Yunwen


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, February 7, 2009 3:03 AM
    Moderator
  • Hi Yunwen.

    The problem was:

    selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.Binary, 1000);


    modified for

    selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.DateTime);

    Now work correct.

    Thanks for help me.

    Marcos Antonio Aguiar Jr
    Monday, February 9, 2009 3:44 PM