Click here to Skip to main content
12,622,032 members (32,442 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.2K views
31 downloads
3 bookmarked
Posted

Post rowversion or timestamp of MsSQL using jquery

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

Introduction

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);
alert(json);

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);
alert(json);

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);
alert(json);

Check the http://jsfiddle.net/DiponRoy/EUeq3/6/ 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.

License

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

Share

About the Author

DiponRoy
Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

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