Monthly Archives: May 2010

Extension Methods

They are static methods that can be invoked like an instance method. Static methods can be declared only in Static classes. Let’s see how extension methods are declared.
public static void ExportToExcel(this DataSet ds)
{
//do something…
}

Extension methods are declared by specifying the “this” keyword as the modifier in the first parameter. The ExportToExcel method is declared as an extension method for the DataSet class.
Extension methods are invoked as soon the namespace of the containing static class is included.
In the below code snippet, the extension method is declared inside
namespace Orcas.Samples
{
public static class Extensions
{
public static void ExportToExcel(this DataSet ds)
{
//do something…
}
}
}

Above method is invoked in the client program as,
using Orcas.Samples;
DataSet ds = new DataSet();
ds.ExportToExcel();

Advertisements

Leave a comment

Filed under C#

Auto-Implemented Properties

Auto-Implemented Properties

In C# 2.0 properties are declared as follows:

private int iEmpId;
private string strEmpName;

public int EmpId
{
get { return iEmpId; }

set { iEmpId = value; }
}

Samething is redfined in C#3.0 using auto-implemented properties like:
public class AutoProperties
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public int Age { get; private set; } // read-only
}
Behind the scene, compiler automatically creates private, anonymous fields that can be accessed from the above auto-implemented properties.

Leave a comment

Filed under C#

HttpHandlers

Using http handlers we can add new functionalities to Web Server. Files with new extensions like .text for a text file can be handled by Web Server by using http handlers. The future of customization can lead to hosting .jsp pages in IIS by finding adequate ISAPI extensions.

To create customized http handler we need to Create a C# class library as “Examplehandler” and implement IHttpHandler interface.

eg: maps all HTTP requests for files with file name extension .New to the class MyHandler.New

<configuration>
   <system.web>
      <httpHandlers>
         <add verb=”*” path=”*.New” type=”MyHandler.New,MyHandler”/>
      </httpHandlers>
   <system.web>
</configuration>

eg: To configure Web services to enable calls from Ajax enabled client script, we need to register the ScriptHandlerFactory HTTP handler in web.config.

<httpHandlers>

   <remove verb=”*” path=”*.asmx”/>

      <add verb=”*” path=”*.asmx” validate=”false”     type=”System.Web.Script.Services.ScriptHandlerFactory,     System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/>

</httpHandlers>

Firstly you need to remove the default httphandler assigned to *.asmx

Leave a comment

Filed under .Net

Typed DataSet

Typed DataSet includes Type-safe into Dataset fields so as to protect it from runtime mismatch. These classes inherit directly from DataSet class.

eg: Runtime error using normal Dataset

1. Using DataSet:

 //Create DataAdapter

SqlDataAdapter daEmp = new SqlDataAdapter(“SELECT empno,empname,empaddress FROM EMPLOYEE”,conn);

//Create a DataSet Object

DataSet dsEmp = new DataSet();

//Fill the DataSet

daEmp.Fill(dsEmp,”EMPLOYEE”);

//Let us print first row and first column of the table

Console.Write(dsEmp.Tables[“EMPLOYEE”].Rows[0][0].ToString());

//Assign a value to the first column

dsEmp.Tables[“EMPLOYEE”].Rows[0][0] = “12345”;//This will generate runtime error as empno column is integer

  2. Using Typed DataSet:

 //Create DataAdapter

SqlDataAdapter daEmp = new SqlDataAdapter(“SELECT empno,empname,empaddress FROM EMPLOYEE”,conn);

//Create a DataSet Object

EmployeeDS dsEmp = new EmployeeDS ();

//Fill the DataSet

daEmp.Fill(dsEmp,”EMPLOYEE”);

//Let us print first row and first column of the table

Console.Write(dsEmp.EMPLOYEE[0].empno.ToString());

//Assign a value to the first column

dsEmp.EMPLOYEE[0].empno = “12345”;//This will generate compile time error.

 Typed dataset is very much similar to a normal dataset; the only difference is that the schema is already present for the same. And any mismatch in the column will generate compile time errors rather than runtime error as in the case of normal dataset. Also accessing the column value is much easier than the normal dataset as the column definition will be available in the schema.

  Typed DataSet can be generated in two ways,

 1) Using Visual Studio .NET IDE.

2) Using XSD.exe (Using VS.Net command prompt)

Open VS.Net command prompt and Type XSD /? For the help on this exe.

 Creating a Typed DataSet using Visual Studio .NET IDE

 Let me explain a step by step procedure to create a Typed DataSet,

 1. Open VS .Net IDE and Click on File -> New -> Project and Select Console Application.

2. Enter name for the project. Say TypedDataSetTest.

3. Right click on the solution and click on Add-> Add New Item will show a dialog box.

Select DataSet from templates pane, give the name (Say TypedDs.xsd) and click on Open. This will add file by name TypedDs.xsd to the solution.

4. Click on the Server Explorer browse to the database and drop the table on the TypedDs.xsd file. 

If we check the xml file for the same then we can see the schema for the table.

This dataset can be used in the same manner as the normal dataset to get the data.

Leave a comment

Filed under C#

Auto-Implemented Properties

In C# 2.0 properties are declared as follows:

 private int iEmpId;
        private string strEmpName;

        public int EmpId
        {
            get { return iEmpId; }

            set { iEmpId = value; }
        }

Samething is redfined in C#3.0 using auto-implemented properties like:
public class AutoProperties
{
       public int EmpID { get; set; }
       public string EmpName { get; set; }
       public int Age { get; private set; } // read-only
}
Behind the scene, compiler automatically creates private, anonymous fields that can be accessed from the above auto-implemented properties.

Leave a comment

Filed under C#

Temporary Tables VS Table Variables

Temporary Tables VS Table Variables

Temporary tables are like regular tables but stored in the tempdb database. These tables get dropped after they have been used. Temporary tables are of two types,

Local Temporary table – defined using a pound sign (#) and accessible only within the session that created it

Global Temporary table – defined using double pound signs (##) and visible to all users.

Table Variables is a data type that looks similar to a table but has a tighter lifetime scope (i.e within a function, stored procedure or batch that it is declared in) and should be used with small datasets.

Temporary Tables Table Variables
Created and stored in tempdb database Created in Memory (although it can write to tempdb)
They are bound to transactions i.e transaction logs are recorded. They are not bound to any transactions. So no effect of transaction rollbacks
Stored procedure containing temporary tables cannot be pre-compiled Stored procedures containing table variables can be pre-compiled
You can drop a Temporary Table You cannot manually drop a table variable
You can create indexes on them You cannot ‘explicitly’ create Indexes on them (exception is the index created while creating a Primary Key)

Leave a comment

Filed under SQL

WCF Transactions

 WCF Transactions

WCF provide the functionality of transactions based on WS specification which allow other language like JAVA to take advantage of this transaction that is the best part of WCF embracing other languages. Under transactions, every activity in the unit must either all succeed or all should fail.

This is achieved by using [TransactionFlow] attribute with different options like:

  • TransactionFlowOption.NotAllowed: This is a default option. Using this option no transaction is applied.
  • TransactionFlowOption.Allowed: This means client can use transaction but it is not compulsory.
  • TransactionFlowOption.Mandatory: This option specifies that client must call the WCF service in a transaction mode. If the WCF service is called without transaction, ‘FaultException’ will be raised. 

Leave a comment

Filed under WCF