Tuesday, 3 May 2011

Namespace in asp.net

  • Namespace  is a collection of classes  and  interfaces Namespace is a logical naming scheme for group related types.

  •  A namespace can span multiple assemblies, and an assembly can define multiple namespaces. When the compiler needs the definition for a class type, it tracks through each of the different imported namespaces to the type name and searches each referenced assembly until it is found.

  • Namespaces can be nested. This is very similar to packages in Java as far as scoping is concerned.

Garbage Collection in asp.net

Short  :
  1. Garbage collection is a CLR feature which automatically release the  memory from the application .
  2. Programmers forget to release the objects while coding. CLR automatically releases objects when they are no longer referenced and in use. CLR runs on non-deterministic to see the unused objects and cleans them.
  3. The garbage collector (GC) completely absolves the developer from tracking memory usage and knowing when to free memory.
 ------------------------------------------------------------------------------------
Breifly :
Every program uses resources of one sort or another -- memory buffers, network connections, database resources and so on. In fact, in an object-oriented environment, every type identifies some resource available for a program's use. To use any of these resources, memory must be allocated to represent the type.

The steps required to access a resource are as follows:
1. Allocate memory for the type that represents the resource.
2. Initialize the memory to set the initial state of the resource and to make the resource usable.
3. Use the resource by accessing the instance members of the type (repeat as necessary).
4. Tear down the state of the resource to clean up.
5. Free the memory.

What is MSIL?

Intermediate Language. Also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code (of any language) is compiled to MSIL.

When compiling the source code to managed code, the compiler translates the source into Microsoft intermediate language (MSIL). This is a CPU-independent set of instructions that can efficiently be converted to native code. Microsoft intermediate language (MSIL) is a translation used as the output of a number of compilers. It is the input to a just-in-time (JIT) compiler. The Common Language Runtime includes a JIT compiler for the conversion of MSIL to native code.

Before Microsoft Intermediate Language (MSIL) can be executed it, must be converted by the .NET Framework just-in-time (JIT) compiler to native code. This is CPU-specific code that runs on the same computer architecture as the JIT compiler. Rather than using time and memory to convert all of the MSIL in a portable executable (PE) file to native code. It converts the MSIL as needed whilst executing, then caches the resulting native code so its accessible for any subsequent calls.

What�s the difference between private and shared assembly?


Private assembly is used inside an application only and does not have to be identified by a strong name.


Shared assembly can be used by multiple applications and has to have a strong name.

Thursday, 28 April 2011

Sql DataReader in asp.net


private static void ReadOrderData()
{
    string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection( connectionString))
    {
        SqlCommand command = new SqlCommand( queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

SqlDataReader

private static void ReadOrderData()
{
    string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection( connectionString))
    {
        SqlCommand command = new SqlCommand( queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

Sql Server Transactions - ADO.NET 2.0 - Commit and Rollback

SqlConnection MySqlConnection = new SqlConnection("Connection String");
MySqlConnection.Open();
SqlTransaction MyTransaction = MySqlConnection.BeginTransaction("MyTrans");

SqlCommand MyCommand = new SqlCommand();
MyCommand.Transaction = MyTransaction;
MyCommand.Connection = MySqlConnection;

try
{
MyCommand.CommandText ="Delete From Employe where Empid = 100";
MyCommand.ExecuteNonQuery();
MyCommand.CommandText ="Delete From Salary where Empid = 100";
MyCommand.ExecuteNonQuery();
MyTransaction.Commit();
}
catch
{
MyTransaction.Rollback();
}
finally
{
MySqlConnection.Close();
}