locked
ChangeTrackingType causes the failure of converting data type? RRS feed

  • Question

  •  Hi, Masters,

    I am using Sql Server 2008 and enabled "Change Tracking" in my database, however, when I use Sync services for ADO.net, I encoutered a curious problem, here is my code:

    public SampleServerSyncProvider()

    {

                string awNewAnchorVariable = "@" + SyncSession.SyncNewReceivedAnchor;

                SqlCommand selectNewAnchorCommand = new SqlCommand();

     

                selectNewAnchorCommand.CommandType = CommandType.Text;

                selectNewAnchorCommand.CommandText = "SELECT " + awNewAnchorVariable + "= @@DBTS"; // " = min_active_rowversion() - 1";

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

                selectNewAnchorCommand.Parameters[awNewAnchorVariable].Direction = ParameterDirection.Output;

                selectNewAnchorCommand.Connection = (SqlConnection)this.Connection;

                this.SelectNewAnchorCommand = selectNewAnchorCommand;

     

                builder.TableName = "Rule";

                //When I enable the below line, executing "syncEngine.Synchronize();"
             //will throw an exception: "Failed to convert parameter value from a Byte[] to a Int64."

                builder.ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking;

                builder.Connection = (SqlConnection)this.Connection;

                builder.SyncDirection = SyncDirection.DownloadOnly;

    }

     

     

    private void btnSync_Click(object sender, EventArgs e)

    {

                syncEngine.Synchronize();

    }


    if I comment the above bold line, all code runs well, who can tell me the reason and how to solve it?

    If I use SqlServerChangeTracking, does it mean that I no longer need selectNewAnchorCommand?

    Thank every master. my progress is from your supports.

    • Moved by Liam Cavanagh - MSFTMicrosoft employee Thursday, February 5, 2009 4:59 PM Wrong forum (Moved from [SyncFx] Feedback to [SyncFx] Microsoft Synchronization Services for ADO.NET)
    • Moved by Hengzhe Li Friday, April 22, 2011 5:44 AM (From:SyncFx - Microsoft Sync Framework Database Providers [ReadOnly])
    Thursday, February 5, 2009 6:43 AM

Answers

  • I think you need to change this line 
     selectNewAnchorCommand.Parameters.Add(awNewAnchorVariable, SqlDbType.Timestamp);

    TO this
     selectNewAnchorCommand.Parameters.Add(awNewAnchorVariable, SqlDbType.BigInt);

    change Tracking uses different anchors
    Friday, February 6, 2009 4:49 PM

All replies

  • I think you need to change this line 
     selectNewAnchorCommand.Parameters.Add(awNewAnchorVariable, SqlDbType.Timestamp);

    TO this
     selectNewAnchorCommand.Parameters.Add(awNewAnchorVariable, SqlDbType.BigInt);

    change Tracking uses different anchors
    Friday, February 6, 2009 4:49 PM
  •  G.Stoynev,

    Thank you for your solution, and solved my problem.

    But I got another problem now, my project uses Access mdb, and the sync direction is Downloadonly, at the first time, the server side db and the client side Access were synced successfully, however, when I added new rows in my server data, Sync can't get the changes, can you tell me how to solve the problem?

    The Server Side Code:

            public SampleServerSyncProvider()

            {

                 ….

                 // Maybe there  is an error? Maybe I don’t need the ClientID or should use SyncSession.SyncClientId

                // Because the sync direction is DownloadOnly              

                SqlCommand clientIdCmd = new SqlCommand();

                clientIdCmd.CommandType = CommandType.Text;

                clientIdCmd.CommandText = "SELECT @" + SyncSession.SyncOriginatorId + " = 1";

                clientIdCmd.Parameters.Add("@" + SyncSession.SyncOriginatorId, SqlDbType.Int).Direction = ParameterDirection.Output;

                this.SelectClientIdCommand = clientIdCmd;

     

                SqlSyncAdapterBuilder builder = new SqlSyncAdapterBuilder((SqlConnection)this.Connection);

                builder.ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking;

                builder.Connection = (SqlConnection)this.Connection;

                builder.SyncDirection = SyncDirection.DownloadOnly;

     

                builder.TableName = "Rule";

                builder.DataColumns.Add("RuleID");

                builder.DataColumns.Add("Title");

                builder.DataColumns.Add("Regex");

                builder.DataColumns.Add("Creator");

                …….

            }

     

     

    The Client Side Code:

            public Guid GetClientId()

            {

                if (_clientId == Guid.Empty)

                {

                    IDbCommand guidCom = null;

                    IDataReader reader = null;

                    try

                    {

                        BeginTransaction(null);

                        string queryStr = "SELECT Guid FROM " + GuidTableName;

                        ... // Read data from Access mdb

                        if (reader.Read())

                        {

                            _clientId = new Guid(reader[0].ToString());

                        }

                    }

                    catch

                    {

                        _clientId = Guid.Empty;

                        throw;

                    }

                    finally

                    {

                        ... //Dispose the resouces

                        EndTransaction(true, null);

                    }

                }

                return _clientId;

            }




    Greatly thank.
    • Edited by StarChenZhi Monday, February 9, 2009 7:03 AM Added my code
    Monday, February 9, 2009 6:41 AM