Wednesday, August 10, 2011

Insert and Update with OpenXML in SQL

This is pretty easy to do. Below is an example that uses a table named 'test' that has an ID column called xmlID and a data column called xmlData.
 

declare @i int
 
exec sp_xml_preparedocument @i output,
'<mydata>
<test xmlID="3" xmlData="blah blah blah"/>
<test xmlID="1" xmlData="blah"/>
</mydata>'
 
insert into test
select xmlID, xmlData
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30))
where xmlID not in (select xmlID from test)
 
update test
set test.xmlData = ox.xmlData
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30)) ox
where test.xmlID = ox.xmlID
 
exec sp_xml_removedocument @i
 

Tuesday, June 7, 2011

How to check an array item exist or not in the json array based on the item property value in javascript

In my current project i am working more with jquery and json.In this project i had a requirement to find the json object exist or not in the json array based on the item property value.I had googled a bit and did not find the answer.

Then i wrote a prototype method to find the json object exist or not.

Array.prototype.containsJsonObj = function(name,value) {
var isExists=false;
$.each(this,function(){
if(this[name]==value){
isExists=true;
return false;
}});
return isExists;
};


Below is the json array

var jsonArray=[{"firstName":"John","lastName":"Doe","age": 23 },
{ "firstName":"Mary","lastName" :"Smith","age": 32 }]

we can check whether the array item exist or not as shown below

if(jsonArray.containsJsonObj("firstName","John"))
{
//if it exists enter into this loop
}
else
{
//if it does not exist enter into this loop
}

How to get checkboxlist selected item values on client side using javascript

Sometimes, you may need to find out server side checkboxlist selected item values on client side.By default checkboxlist does not send item value to the client.This problem can be solved by assigning values to each item of checkboxlist in page load event as shown below

protected void Page_Load(object sender, EventArgs e)
{
foreach (ListItem li in cblListItems.Items)
li.Attributes.Add("mainValue", li.Value);
}

Then call the below javascript function on click of checkboxlist.

function GetCheckBoxListValues(chkBoxID)
{
var chkBox = document.getElementById(chkBoxID);
var options = chkBox.getElementsByTagName('input');
var listOfSpans = chkBox.getElementsByTagName('span');
for (var i = 0; i < options.length; i++)
{
if(options[i].checked)
{
alert(listOfSpans[i].attributes["mainvalue"].value);
}
}
}

How To Sort ListItems In JavaScript

First Declare ListItemArray Varaible:-

var myOptions = [];

//Fill ListItem Array With Some Data

// copy options into an array
function SortListItems(optionText,optionValue) //where optionText and optionValue are array of texts and values
{ myOptions.clear();//clear array if any data present

// copy options into an array
for (var i=0; i
{ myOptions[i] ={ optText:optionText[i], optValue:optionValue[i]}; }

// sort array myOptions.sort(SortFuncAsc);}

// sort functionfunction SortFuncAsc(record1, record2){ var value1 = record1.optText.toLowerCase(); var value2 = record2.optText.toLowerCase();; if (value1 > value2) return(1); if (value1 < value2) return(-1); return(0);
}

Bandwidth detection with javascript

In one of my current projects, I came across a scenario to play a video based on the users downloading bandwidth. The detection should be done with javascript. There is a technique, to load an image of known size and calculating the bandwidth on basis of time taken to load that image. Though this technique is not 100% reliable, this will give an approximate estimate of bandwidth.

Here is the javascript code to find users bandwidth:

var userBandwidth = 0;
var startTime;
var endTime;
var imgSize = 39842;
var loadTimeInSec;

function GetUserBandwidth() {
var testImage = new Image();
testImage.src = "bwtest.jpg";
startTime = (new Date()).getTime();
testImage.onload = CreateDelegate(testImage, DoneWithTest);
}

function DoneWithTest() {
endTime = (new Date()).getTime();
loadTimeInSec = (endTime - startTime) / 1000;
userBandwidth = (imgSize / loadTimeInSec) / 1024;
}

Here we are loading an image of size 38Kb and added a delegate on image loaded event. In the call back function, we calculate end time, with that we can calculate the bandwidth of the user.

Check my previous post to add delegate in javascript.

Delegate in Javascript

Before going to the topic, lets know brief about “Delegate”:
A delegate is like a function pointer, which stores reference of a method. It specifies a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners.

We can create and define delegates in Javascript to perform some specific tasks on an object. The following is the code to define delegate.


function CreateDelegate(contextObject, delegateMethod) {
return function() {
return delegateMethod.apply(contextObject, arguments);
}
}


Let us take a simple example and use the delegate in that. Here is the scenario, there is a large image to be loaded on a web page and we need to display the height and width of that image after loading. Delegate method will be very handy in this situation.


var image = new Image();
image.src = "img.jpg";
image.name = image.id = "img1";
image.onload = CreateDelegate(image, ImageLoaded);

function ImageLoaded() {
alert(this.width + " X " + this.height);
}


We are creating a delegate on image loaded event, which will call method ‘ImageLoaded’. This delegate automatically pass the object on which the delegate is defined, to the method. We can refer the actual image object using ‘this’ in the method.