Click here to Skip to main content
13,349,032 members (67,853 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 21 Jul 2014

Post rowversion or timestamp of MsSQL using jquery

, 22 Jul 2014
Rate this:
Please Sign up or sign in to vote.


TIMESTAMP/ROWVERSION provides a row-versioning feature in the table rows. This helps us to manage the synchronization (row update) for multiuse access. But the problem comes, when we try to serialize a particular object (which cares a row version value) to json string using JSON.stringify(). And post that string to a server using jquery ajax.

What is the problem?

Let's say we have a Rowversion at variable rowVersion.

/*rowVersion from server*/
var rowVersion = [0, 0, 0, 0, 0, 0, 7, 209];
var json = '';

And we want to post json using JSON.stringify() of a perticaular object which holds this rowVersion using juery ajax.

/*this json post would faill, at server RowVersion is null*/
var normalPostObj = {
    RowVersion: rowVersion
json = JSON.stringify(normalPostObj);

But at server posted rowVersion is null. The result is same even if post the object itself rather than the json string.

What can we do?

extension to handle row versions

/*rowVersion hepler*/
$.timeStampToBase64String = function(data) {
    var str = String.fromCharCode.apply(null, data);
    return btoa(str);

Use this extension inside post object to format the roversion like,

/*this json post would work just fine, at server rowVersion is as..*/
var modifiedPostObj = {
    RowVersion: $.timeStampToBase64String(rowVersion)
json = JSON.stringify(modifiedPostObj);

Can we use it in Knockout mapper ?

To do the same thing for Knockout mapper, we have to use this extension inside a replacer function like,

/*Use for knockout =>*/
var timestampReplacer = function(key, value) {
    /*if any property with name RowVersion at the object convert its value*/
    if (key === 'RowVersion' && Array.isArray(value)) {
        return $.timeStampToBase64String(value);
    return value;

and use this replacer function in knockout like,

/*important: using normalPostObj rether than the modifiedPostObj*/
json = ko.toJSON(normalPostObj, timestampReplacer);

Check the for quick preview.

Find Visual Studio 2012 solution for an MVC project at the attachment. In the project I have used entity framework code first. So If you run the project.

1. A database with name DbRowVersion will be created. (Change it from web.config)

2. And table Student with some seed data’s.

Or follow the example we just talked about. It would work just fine.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bangladesh Bangladesh
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 22 Jul 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid