Monday, September 26, 2005

C# look at IL - .NET disassembler

While googling came across Lutz Roeder's .NET Reflector - truely .NET disassembler and thought to share it with you. Have a look at the below link for all tools by Lutz.


Using SQL Cache Dependency in ASP.NET

ASP.NET Cache is used to store static data across web sessions. But what to do when you want to update the Cache as and when SQL/Data changes. The below two articles focus on the same, have a look.


Effective way to expand data tree in SQL Server

There are possibly many ways to expand nodes which are in parent - child relationship. This post focus on the most effective way to do it along with other possible solutions.

# Multiple round trip to server (worst case)
- Make an SP which returns all parent/child (whatever required) IDs
- From code call SP recursively untill you reach the end.

# Single round trip to server (thats what a better option - again not the best one)
- Make an SP which returns everything from the table.
- Just make your recurssion process in Business logic (.NET/Java) rather than T-SQL

# Without recursion (hopefully, the best)
Let's dig it out, Here is the story. I tried to solve the problem in most effective way using many ways including the above two. But no way could end up into success. I was looking for some logical way at SP end. Below is the table structure and the SP. SP comments would help you to understand it well. Just go through it, it's very easy. Also I would appreciate if you have the solution in some other way.


Sample Table
CREATE TABLE [_Test_Hierarchy] (
[ID] [numeric](10, 0) IDENTITY (1, 1) NOT NULL ,
[ParentID] [numeric](10, 0) NOT NULL ,
[Name] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

Stored Procedure
CREATE PROCEDURE Test_Expand -- Creates an stored procedure
@current NUMERIC -- The ID of which all childs are required
DECLARE @LinesUpdated INT, @Level INT


SET @LinesUpdated = 1 -- It holds the value indicating number of rows inserted into temp table.
SET @Level = 1 -- used to identify the nodes for which we have already added childs.

CREATE TABLE #stack -- temporary table, you can add more columns as per your comfort.

INSERT INTO #stack -- Insert the current node and set it's level to 1.
VALUES (@current, @Level)

WHILE @LinesUpdated > 0
SET @Level = @Level + 1 -- Every loop iteratation increast one level.

-- Here we need to focus on two criteria while shortlisting.
-- 1. Check all child nodes of the parents already stored into temp table - #stack
-- 2. A node already in the list, must not be repetated. That is why we kept Lvl = @Level - 1 condition. It always search for fresh nodes.
SELECT t1.ID, @Level
_Test_Hierarchy t1,
(SELECT * FROM #stack WHERE Lvl = @Level - 1) s1
WHERE 1 = 1
AND s1.ID = t1.ParentID

SET @LinesUpdated = @@ROWCOUNT -- @@ROWCOUNT always holds last count of selection. Terminate the loop when no row added.

-- Its very simpe, you must be knowing...
SELECT t1.ID, t1.Name
#stack s1,
_Test_Hierarchy t1
WHERE 1 = 1
AND s1.ID = t1.ID


Wednesday, September 21, 2005

Handeling server control event from page itself in .NET

Let me define the problem statement. Say, we have address control having address1, address2, city, state and some other fields. This post answers to handle "save" button click event from page itself. You already have the event handler on control code behind, but to handle it from main page (where your control resides) is a bit tricky. Here is a step by step guide for a newbie who want to create server control and would like to wire events.

> Declare an public event in to your control, like...
public event CommandEventHandler SaveCommand;

> Add btnSave click event handler, and also the defination...
btnSave.Click += new EventHandler(btnSave_Click);
private void btnSave_Click(object sender, EventArgs e)
Button button = sender as Button;
if (button != null)
CommandEventArgs args = new CommandEventArgs(button.CommandName, button.CommandArgument);


> Add OnSaveCommand function...
protected virtual void OnSaveCommand(CommandEventArgs e)
if (SaveCommand != null)
SaveCommand(this, e);

> Now you can have the event handler in your page. Just click on control from page, add your code...
private void AddressControl1_SaveCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)
// my code..


Friday, September 16, 2005

Refrencing the Root Path/URL in .NET

A common question from newbies is "How can I reference the root path of my application from controls?". It is common for many server side controls to have properties such as src (source) and hyperlinks/URL etc. ASP.NET has a neat syntax that you can use to reference the root of the applications directory in any server-side controls URL property. This syntax will relief you of having to place relative pathis in your apps directory hierachy.

Simply prefix the path with the ~ (tilda) character. During the compilation process of the server-side control, ASP.NET will translate that ~ into Request.ApplicationPath.

An example would look like img src="~/images/logo.gif" runat="server". Keep in mind that this will work for SERVER-SIDE controls only!