Wednesday, 11 January 2012

How does Window execute a .net exe file?

Every .exe or .dll file apart from containing the metadadata table and the IL code also contain a PE32(32+) header thata deteremines what kind of machine or processor this exe is targetting for(32 bit,64 bit etc...)

After the windows has examined what processor is needed for this exe from the header of the exe it will try to load the corrosponding version(X86,X64 or IA64)mscoree.dll to the address space of the process.For a 32 bit system this dll is found in C:\Windows\System32 for a 64 bit system this is found in the same directory for backword compatibility.

The processor primary thread than call a method defined inside mscoree.dll.This method will initialize the CLR,executes the exe assembly and calls the main method.

At this point the managed application is up and running.

How to bind a drop down list with an xml with the items of the dropdown list being sorted?

Lets assume we have an XML file which looks like this
<Sates>
<State>
<ID>1</ID>
<Name>Orissa</Name>
  </State>

<State>
<ID>2</ID>
<Name>Karnataka</Name>
</State>

<State>
<ID>3</ID>
<Name>Kolkata</Name>
</State>

<State>
<ID>4</ID>
<Name>A.P.</Name>
</State>

<State>
<ID>5</ID>
<Name>Bihar</Name>
</State>
</Sates>

To bind this xml with a dropdown list(say "ddlXML") we have to bind this xml with a dataset.
DataSet dsXML = new DataSet();

dsXML.ReadXML(Server.MapPath("~/XMLFiles/State.xml"))(the xml file State.xml is present inside XMLFiles folder inside project directory)

Create a DataView to out of this DataSet that will provide the sorting option and than bind this DataView with the dropdown

DataView dvXML = dsXML.Tables[0].DefaultView();
dvXML.Sort = "Name";

Now bind this DataView with the dropdown list
ddlXML.DataSource = dvXML;

ddlXML.DataBind();

DataView can be accessed through a DefaultView property on DataTable. DataView class allows us to sort and filter data but not store it.We can also use the DataView class to sort filtered inputted data to the database.

Happy Coding :)

Sunday, 8 January 2012

Explain CLR

The heart to the .net frame work is its runtime execution environment that is called CLR or Common Language Runtime.As the name suggests it is a common runtime being used by variety of programming languages.The common features of CLR ie memory management,security,assembly loading,error handling is available to all languages that target the runtime.For example runtime uses "exception" to report errors.Thus all the languages that target runtime use exception to report errors. The code that runs under CLR is called managed code.

how ever before running under CLR any code that we write has to be compiled. And this compilation is a two step process in .net.
>Compilation of the source code to the microsoft intermediate language code MSIL(by the corresponding language compiler)
>compilation of il to machine specific code by the clr
This two stage compilation is very important because many of the .net key features is being provided by the intermediate language.IL is a low-level language that can be easily converted to the machine specific language.Having an universal syntax for the IL code gives the following advantage
Platform Independence
This means the same file containing the byte code instruction can be placed on any platform,the final stage of compilation can be easily accomplished so that it can run on that particular platform.Hence compiling the .net code to IL we achieve the same platform independence that compiling to java byte code,java has achieved.However complete platform independence for .net is theoretical.
Performance Improvement
IL is just in time compiled.Instead of compiling the entire application in one go JIT compiles each portion of the code as it is called.When a code is compiled the native executable is stored until the application exits so that it does not need to be recompiled the next time this portion of the code is executed.
Microsoft argues that this process is more efficient than compiling the entire code at the start of the application because a large portion of the code is not executed at any given run.This explains why the execution of managed IL is as fast as executing the native machine code.
But what Microsoft explains for the real performance improvement is:as the final stage of compilation takes place at runtime JIT is aware of what processor type the program will run on.that means it can optimize the final executable to take the advantage of any feature that the processor provides
Language Interoperability
We can compile to IL from one language and this code can be inter-operable with the code that has been compiled to IL from another language.