Friday, May 29, 2009

How to convert from rows to columns format in the table

I have the table and the values are defined as below:
Id PuringInterval Description
1 10 UTM
2 20 Hourly
3 25 Daily

And I would like to have the select or the view statement to return the
following
UTM Hourly Daily
10 20 25
So I want to convert from rows to columns

In SQL Server 2005 you can use the PIVOT operator, and using CASE will work
for both SQL Server 2000 & 2005:

SQL Server 2005
SELECT MAX(UTM) AS 'UTM',
MAX(Hourly) AS 'Hourly',
MAX(Daily) AS 'Daily'
FROM Foobar
PIVOT (MAX(PuringInterval)
FOR [Description] IN ([UTM], [Hourly], [Daily])) AS P;

SQL Server 2000 & 2005


SELECT MAX(CASE WHEN [Description] = 'UTM'
THEN PuringInterval END) AS 'UTM',
MAX(CASE WHEN [Description] = 'Hourly'
THEN PuringInterval END) AS 'Hourly',
MAX(CASE WHEN [Description] = 'Daily'
THEN PuringInterval END) AS 'Daily'
FROM Foobar;

Some more information refer link
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx


Tuesday, May 5, 2009

What is the difference between Server.Transfer and Response.Redirect


Server.Transfer() : client is shown as it is on the requesting page only, but the all the content is of the requested page. Data can be persist accros the pages using Context.Item collection, which is one of the best way to transfer data from one page to another keeping the page state alive. 

Response.Dedirect() :client know the physical loation (page name and query string as well). Context.Items loses the persisitance when nevigate to destination page. In earlier versions of IIS, if we wanted to send a user to a new Web page, the only option we had was Response.Redirect. While this method does accomplish our goal, it has several important drawbacks. The biggest problem is that this method causes each page to be treated as a separate transaction. Besides making it difficult to maintain your transactional integrity, Response.Redirect introduces some additional headaches. First, it prevents good encapsulation of code. Second, you lose access to all of the properties in the Request object. Sure, there are workarounds, but they’re difficult. Finally, Response.Redirect necessitates a round trip to the client, which, on high-volume sites, causes scalability problems. 

As you might suspect, Server.Transfer fixes all of these problems. It does this by performing the transfer on the server without requiring a roundtrip to the client.

Examples:

Server.Transfer

Server.Transfer("Webform2.aspx")

Response.Redirect

Response.redirect("Webform2.aspx

AppDomain concept in ASP.Net

Asp.Net introduces the concept of an Application Domain which is shortly known as AppDomain. It can be considered as a Lightweight process which is both a container and boundary. The .NET runtime uses an AppDomain as a container for code and data, just like the operating system uses a process as a container for code and data. As the operating system uses a process to isolate misbehaving code, the .NET runtime uses an AppDomain to isolate code inside of a secure boundary.

The CLR can allow the multiple .Net applications to be run in a single AppDomain.

The CLR isolates each application domain from all other application domains and prevents the configuration, security, or stability of a running .NET applications from affecting other applications.An AppDomain can be destroyed without effecting the other Appdomains in the process.
 
Mulitple Appdomains can exist in Win32 process. As we discussed the main aim of AppDomain is to isolate applications from each other and the process is same as the working of operating system process. This isolation is achieved by making sure than any given unique virtual address space runs exactly one application and scopes the resources for the process or application domain using that address space.

Win32 processes provide isolation by having distinct memory addresses. The .Net runtime enforces AppDomain isolation by keeping control over the use of memory. All memory in the App domain is managed by the run time so the runtime can ensure that AppDomains Do not access each others memory.

How to create AppDomain

AppDomains are generally created by Hosts for example Internet Explorer and Asp.net. The following is an example to create instance of an object inside it and then executes one of the objects methods. This is the explicit way of creating AppDomain by .Net Applications
 
AppDomains are created using the CreateDomain method. AppDomain instances are used to load and execute assemblies (Assembly). When an AppDomain is no longer in use, it can be unloaded.

public class MyAppDomain : MarshalByRefObject
{
    public string GetInfo()
    {
        return AppDomain.CurrentDomain.FriendlyName;
    }
} 

public class MyApp

{

    public static void Main()

    {

        AppDomain apd = AppDomain.CreateDomain("Rajendrs Domain");

        MyAppDomain apdinfo = (MyAppDomain)apd.CreateInstanceAndUnwrap (Assembly.GetCallingAssembly().GetName().Name, "MyAppDomain");

        Console.WriteLine("Application Name = " + apdinfo.GetInfo());

    }

}

 

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown. 

Advantages

A single CLR operating system process can contain multiple application domains. There are advantages to having application domains within a single process.

  1. Lower system cost - many application domains can be contained within a single system process.
  2. Each application domain can have different security access levels assigned to them, all within a single process.
  3. Code in one AppDomain cannot directly access code in another AppDomain.
  4. The application in an AppDomain can be stopped without affecting the state of another AppDomain running in the same process.
  5. An  Exception in on AppDomain will not affect other AppDomains or crash the entire process that hosts the AppDomains.


Friday, May 1, 2009

Intermediate Language Disassembler(ILDASM)

You can get IL disassemble tool as ILDasm.exe in directory C:\Program Files\Microsoft.NET\FrameworkSDK\bin

So what does this tool do?

The answer to this question is found in the tutorial supplied with .NET SDK as "The ILDSAM tool parses any .NET Framework EXE/DLL module and shows the information in a human-readable format. It allows user to see the pseudo assembly language for .NET". IL disassmeber tool shows not only namespace but also types including their interfaces. As its name suggests, it is an intermediate language, so it has its own specification. Users can also write programs using this intermediate language, its very similar to assembly language of the old days.

I will use a simple example and use ILDASM.exe

//Hello World Program HelloWorld.cs using System;   class HelloWorld  {       static void Main()       {          Console.WriteLine("Hello, world!");           } } 
Complier it on command line by using csc HelloWorld.cs

Helloworld.exe file will be generated

Now use the command ildasm HelloWorld.exe

You will see a screen like this.

Here you can see all of the Symbols. The table below explains what each graphic symbol means. Some of them you can find in HelloWorld's members.

The tree in this window shows that manifest information contained inside HelloWorld.exe. By double-clicking on any of the types in the tree, you can see more information about the type.

Double-clicking the ".class public auto ansi" entry shows the following information:

Users can see that the HelloWorld type is derived from the System.Object type.

The first method, .ctor, is a constructor. This particular type has just one constructor but other types may have several constructors each with a different signature. If you double-click on the constructor method, a new window appears showing the IL (intermediate language) contained within the method:

The Common Language Runtime is stack based. So, in order to perform any operations, the operands are first pushed onto a virtual stack and then the operator executes. The operator grabs the operands off the stack, performs the desired operation and places the result back on the stack. At any one time, this method will have no more than 8 operands pushed onto the virtual stack. We can see thby looking at the ".maxstack" attribute ( Maximum Stack size ) that appears just before the IL code. In the above code maxstack is shown as 8.

Lets examine the IL code :

IL_0000:  ldarg.0 : Load Object this pointer in stack IL_0001:  call       instance void [mscorlib]System.Object::.ctor() IL_0006:  return the value loaded in stack 
If user make a double click on main: void()
It will look like this:

If we will examine IL Code:

IL_0000:  ldstr      "Hello, world!" IL_0005:  call       void [mscorlib]System.Console::WriteLine(class System.String) IL_000a:  ret 
LDSTR: Load String.
First line indicates load String in stack.
Second Line indicates call method System.Console:: WriteLine as the fetch the value from stack put in this method and again put the result in stack.
Third line shows fetch the final value from stack and return it.

There are some advance option also available. The extra options are enabled by running ILDASM with the /ADV ("ADVanced") command-line switch. When /ADV is specified, ILDASM enables additional command-line switches. For the user convenience I will summarize some basic instructions here below.

InstructionMeaning
LDCThis instruction pushes a hard coded number on the stack.
LDARG and LDARGALoad argument and load argument address, respectively
LDLOC and LDLOCALoad local variable and load local variable address, respectively
LDFLD and LDSFLDLoad Object Field and Load Static Field of a Class, respectively
LDELEMLoad an element of an array
LDLENLoad the length of an array
STARGStore a value in an argument slot
STELEMStore an element of an array
STFLDStore into a field of an object
CEQCompare equal
CGTCompare greater than
CLTCompare less than
BRUnconditional branch
BRFALSE and BRTRUEBranch on false and branch on true, respectively
CONVData conversion
NEWARRCreate a zero-based, one-dimensional array
NEWOBJCreate a new object
BOXConvert value type to object reference
UNBOXConvert boxed value type to its raw form
CALL and CALLVIRTCall a method and call a method associated at runtime with an object, respectively

Intermediate Language Disassembler(ILDASM)

You can get IL disassemble tool as ILDasm.exe in directory C:\Program Files\Microsoft.NET\FrameworkSDK\bin

So what does this tool do?

The answer to this question is found in the tutorial supplied with .NET SDK as "The ILDSAM tool parses any .NET Framework EXE/DLL module and shows the information in a human-readable format. It allows user to see the pseudo assembly language for .NET". IL disassmeber tool shows not only namespace but also types including their interfaces. As its name suggests, it is an intermediate language, so it has its own specification. Users can also write programs using this intermediate language, its very similar to assembly language of the old days.

I will use a simple example and use ILDASM.exe

//Hello World Program HelloWorld.cs using System;   class HelloWorld  {       static void Main()       {          Console.WriteLine("Hello, world!");           } } 
Complier it on command line by using csc HelloWorld.cs

Helloworld.exe file will be generated

Now use the command ildasm HelloWorld.exe

You will see a screen like this.

Here you can see all of the Symbols. The table below explains what each graphic symbol means. Some of them you can find in HelloWorld's members.

The tree in this window shows that manifest information contained inside HelloWorld.exe. By double-clicking on any of the types in the tree, you can see more information about the type.

Double-clicking the ".class public auto ansi" entry shows the following information:

Users can see that the HelloWorld type is derived from the System.Object type.

The first method, .ctor, is a constructor. This particular type has just one constructor but other types may have several constructors each with a different signature. If you double-click on the constructor method, a new window appears showing the IL (intermediate language) contained within the method:

The Common Language Runtime is stack based. So, in order to perform any operations, the operands are first pushed onto a virtual stack and then the operator executes. The operator grabs the operands off the stack, performs the desired operation and places the result back on the stack. At any one time, this method will have no more than 8 operands pushed onto the virtual stack. We can see thby looking at the ".maxstack" attribute ( Maximum Stack size ) that appears just before the IL code. In the above code maxstack is shown as 8.

Lets examine the IL code :

IL_0000:  ldarg.0 : Load Object this pointer in stack IL_0001:  call       instance void [mscorlib]System.Object::.ctor() IL_0006:  return the value loaded in stack 
If user make a double click on main: void()
It will look like this:

If we will examine IL Code:

IL_0000:  ldstr      "Hello, world!" IL_0005:  call       void [mscorlib]System.Console::WriteLine(class System.String) IL_000a:  ret 
LDSTR: Load String.
First line indicates load String in stack.
Second Line indicates call method System.Console:: WriteLine as the fetch the value from stack put in this method and again put the result in stack.
Third line shows fetch the final value from stack and return it.

There are some advance option also available. The extra options are enabled by running ILDASM with the /ADV ("ADVanced") command-line switch. When /ADV is specified, ILDASM enables additional command-line switches. For the user convenience I will summarize some basic instructions here below.

InstructionMeaning
LDCThis instruction pushes a hard coded number on the stack.
LDARG and LDARGALoad argument and load argument address, respectively
LDLOC and LDLOCALoad local variable and load local variable address, respectively
LDFLD and LDSFLDLoad Object Field and Load Static Field of a Class, respectively
LDELEMLoad an element of an array
LDLENLoad the length of an array
STARGStore a value in an argument slot
STELEMStore an element of an array
STFLDStore into a field of an object
CEQCompare equal
CGTCompare greater than
CLTCompare less than
BRUnconditional branch
BRFALSE and BRTRUEBranch on false and branch on true, respectively
CONVData conversion
NEWARRCreate a zero-based, one-dimensional array
NEWOBJCreate a new object
BOXConvert value type to object reference
UNBOXConvert boxed value type to its raw form
CALL and CALLVIRTCall a method and call a method associated at runtime with an object, respectively