Ajax Shortcuts for Element Positioning

getBounds Method
Gets the x and y coordinates, width and height of a element.
getBounds Method is in Sys.UI.DomElement namespace.
Returns a json formatted object with x,y,width,height as its members.

// Example
// Get the bounds of the element
var elementRef = $get("elementID");
var elementBounds = Sys.UI.DomElement.getBounds(elementRef);
var xPosition = elementBounds.x;
var yPosition = elementBounds.y;
var width = elementBounds.width;
var height = elementBounds.height;

getLocation Method
Gets the x and y coordinates.
getLocation Method is in Sys.UI.DomElement namespace.
Returns a json formatted object with x,y,width,height as its members.
getLocation may fail to return a correct value if the positing is fixed mode.(style="position:fixed").

// Example
// Get the bounds of the element
var elementRef = $get("elementID");
var elementBounds = Sys.UI.DomElement.getLocation(elementRef);
var xPosition = elementBounds.x;
var yPosition = elementBounds.y;

setLocation Method
Sets the x and y coordinates.
setLocation Method is in Sys.UI.DomElement namespace.

// Get the location of the element
var elementRef = $get("elementID");
var elementLoc = Sys.UI.DomElement.getLocation(elementRef);
// Move the element's x position
Sys.UI.DomElement.setLocation(elementRef, 100, elementLoc.y);

Ajax StringBuilder for Javascript

String builder is used to concatenate string. the mechanism is created in such a way that the memory is not wasted for concatenating a sequence of strings. adding a string using the '+' operator creates a new instance of the string variable each time we add. String builder does the same job with a single instance. this reduces the memory and increses performance.

StringBuilder belongs to namespace Sys.
it can be instantiated like this
var sb = new Sys.StringBuilder(string);
Methods of StringBuilder Class

append Method
Appends a string to the end of the StringBuilder instance.

appendLine Method
Appends a new string with a line terminator to the end of the StringBuilder instance.

clear Method
Clears the contents of the StringBuilder instance.

isEmpty Method
Determines whether the StringBuilder instance has any content.

toString Method
Creates a string from the contents of a StringBuilder instance.

var sb = new Sys.StringBuilder("this");
sb.append("string ");
sb.append("is ");
sb.append("build by ");
sb.append("String Builder class");
// Displays: "The result string : this string is build by String BUilder class"
alert("The result" + sb.toString());

Ajax Shortcuts

The Ajax ScriptManager has a set of client side reference function, which can be used with javascript.

1. $get("elementID")

Method that gets the reference to the specified Control id.This is equivalent to the method document.getElementById("elementId"), it returns the control object.

$get("elementid").value = someValue;
var someVariable = $get("elementid").value;
//Direct Method
Sys.UI.DomEvent.get("elementid").value = someValue;
var someVariable = Sys.UI.DomEvent.get("elementid").value;

2. $find("elementID",parent)

Finds the control within the page or within the specified parent. This is more similar to server side Find Control method, the parent parameter is optional, if provided, the search is specified to that parent control only. it returns control object if found else returns null.

var o = $find("elementID", mydiv);
//Direct Method
var obj = Sys.UI.DomEvent.find("elementID", mydiv);

3. $addHandler(element, eventName, handler);

Method to assign a handler function to event of a specified control. The eventName parameter should not include the "on" prefix. For example, specify "click" instead of "onclick".

//Direct Method

4. $removeHandler(element, eventName, handler);
Method to remove a existing handler function to event of a specified control.

//Direct Method

Row_Number() in SQL Server

The row number function returns the sequitial number of the row within the partition of the result set. this is one of the Ranking function in SQL Server.

The Row_Number() function sorts the result set as per the column specified in the ORDER BY clause and starts numbering the row with a intial value of 1. The PARTITION BY is a optional parameter. if it is used the result is partioned as per the column or expression specified and starts numbering the row with intial value of 1 for each partition. partioning is similar to group functionality. it groups the result set and arranges in order and serialize the row numbers.

SELECT orderid, customerid,
FROM orders
WHERE orderid < 10400
AND customerid <= 'BN'

WITH OrderedOrders AS
(SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (partition by OrderDate order by OrderDate)as RowNumber
FROM Sales.SalesOrderHeader )
FROM OrderedOrders

WITH OrderedOrders AS
(SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (partition by Datepart(yy,OrderDate), Datepart(mm,OrderDate), Datepart(dd,OrderDate) order by OrderDate)as RowNumber
FROM Sales.SalesOrderHeader )
FROM OrderedOrders

Error Handling in SQL Server 2005

Error handling in previous versions of SQL Server are Tedious. we find the error occured by frequently checking the @@ERROR Variable after each statement and raise the error accordingly.
In SQL Server 2005, Try Catch Block was added to extend error handling.

-- Sample code to Try Catch
SELECT * FROM authors

Things to be remembered while using Try Catch
1. Try Block should be follwed by a Catch Block
2. Try and Catch should be assoiciated to single batch only
3. Try and Catch Block can be nested.
4. Muliple Catch Block should not be used for a single Try Block

TRY…CATCH uses error functions to capture error information.
1. ERROR_NUMBER() returns the error number.
2. ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters such as lengths, object names, or times.
3. ERROR_SEVERITY() returns the error severity.
4. ERROR_STATE() returns the error state number.
5. ERROR_LINE() returns the line number inside the routine that caused the error.
6. ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.

Try Catch in Trasactions
The function XACT_STATE() return the state of transaction.
-1 - failed transaction
1 - success transaction

-- catch errors in a transaction
SELECT * FROM authors
-- Test tx state
IF (XACT_STATE()) = -1

Building Hierarchical Xml Data using DataSet

Building a xml string in Hierarchical manner using the sql query. The BuildMap method of SiteMapBuilder class accepts dataset, identity column and refering colum and key value as a parameters and retuns the xml. the dataset contains the datatable will two columns, with one column refering the another column. no relation should be created explicitly. the data in the table should have a relation.

the class is listed below.

Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Data

Public Class HierarchicalXMLDataBuilder

Private ParentColumn As String
Private ChildColumn As String
Private NoOfColumns As Integer
Private Data As DataSet
Private vuData As DataView

Public Function BuildMap(ByVal data As DataSet, ByVal IdentityColumn As String, ByVal ReferenceColumn As String, ByVal KeyValue As String) As String
Me.ParentColumn = IdentityColumn
Me.ChildColumn = ReferenceColumn
Me.Data = data
vuData = New DataView(data.Tables(0))
NoOfColumns = data.Tables(0).Columns.Count
Dim xml As String
xml = BuildChildNodes(vuData, ParentColumn & " = " & KeyValue)
Return xml
End Function

Private Function BuildChildNodes(ByVal vu As DataView, ByVal filter As String) As String
Dim nodeBuilder As New StringBuilder(String.Empty)
vu.RowFilter = filter
Dim RowCounter As Integer = 0
For RowCounter = 0 To vu.Count - 1
Dim ColCounter As Integer = 0
For ColCounter = 0 To NoOfColumns - 1
nodeBuilder.Append(" ")
nodeBuilder.Append(" = """)
nodeBuilder.Append(""" ")
nodeBuilder.Append(" >")
Dim vuData2 As New DataView(data.Tables(0))
If vu.Count > RowCounter Then
nodeBuilder.Append(BuildChildNodes(vuData2, ChildColumn & " = " & vu.Item(RowCounter)(ParentColumn)))
End If
Return nodeBuilder.ToString()
End Function
End Class

the BUilder class can be used this way, and can be binded with treeview or to a menu control.

Dim XmlData As New SiteMapBuilder
Dim XmlString As String = XmlData.BuildMap(ds, "RowId", "ParentID", 1)
Dim doc As New System.Xml.XmlDocument()

Dispose Vs Finalizer

The Dispose and Finalizer are methods used to clear the resources used for the object.
The Dispose is a method of IDisposable interface. we have to inherit this interface for our object and implement the method Dispose, to clear our objects used in our application. suppose if we use collection and dictionary or any other huge resource for a specific object, we have to clear all the memory used by the object when it is destroyed. to make this happen internallly for every object we have to implement Dispose method and it takes care of the clearing the memory.
The Finalizer method is also similar type but this will be called when GC gets invoked. this GC.Collect call will invoke Finalizer method for each and every object. this will do a emergency clean, it may lead to data loss some times.

we can also implement finalizer method as a special case. the call of finalizer method after a dipose is a waste of process time, we can bypass this action by setting the property SupressFinalizer to true. this will help to avoid calling the finalizer method when GC gets activated.

New operator ?? in c#

The operator '??' is in c#, which is used check the value is null.
Previously, we used to check the null value by the following methods..

if (a!=null) { c=a; } or
c = ((a!=null)?a:0);

we can do the same checking with the new operator ?? like this..
c = a??0;

By this statement the value a will be assigned to c if it is not null else the zero will be assigned.