Tuesday, September 16, 2008

Delegates Demystified

Delegates are function pointers in C# that are managed and type safe and can refer to one or more methods that have identical signatures. Delegates in C# are reference types. They are type safe, managed function pointers in C# that can be used to invoke a method that the delegate refers to. The signature of the delegate should be the same as the signature of the method to which it refers. According to MSDN, "A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure."
C# provides support for Delegates through the class called Delegate in the System namespace. Delegates are of two types.
· Single-cast delegates
· Multi-cast delegates
A Single-cast delegate is one that can refer to a single method whereas a Multi-cast delegate can refer to and eventually fire off multiple methods that have the same signature.
The signature of a delegate type comprises are the following.
· The name of the delegate
· The arguments that the delegate would accept as parameters
· The return type of the delegate
A delegate is either public or internal if no specifier is included in its signature. Further, you should instantiate a delegate prior to using the same.

Single Cast Delegate:

class MPS
{
delegate void MPSDel();
delegate void MPSCallDel (int i);
public void fun()
{
MPSDel d1 = new MPSDel(MPS);
d1();
MPSCallDel d2 = new MPSCallDel (HMC);
d2 ( 5 ) ;
}
public void MPS()
{
Response.Write(“WelCome to MPS”) ;
}
public void HMC(int i)
{
Response.Write(i) ;
}
}



Multicast Delegate:


delegate void del3(int i);

public void function2(int i)
{
Response.Write(i);
Response.Write("
");
}
public void function3(int s)
{
s = 900;
Response.Write(s);
Response.Write("
");
}





protected void Button1_Click(object sender, EventArgs e)
{
del3 d3 = new del3(function2);
d3 += new del3(function3);
d3(10);
}



Output:

10
900

Monday, September 15, 2008

An Example of Using DAAB

Accessing data with Data Access Application Block:

using Microsoft.ApplicationBlocks.Data;

SqlHelper.ExecuteNonQuery(connection,"INSERT_PERSON",new
SqlParameter("@Name",txtName.Text) ,new SqlParameter("@Age",txtAge.Text) );




Retrieving Multiple Rows using SqlDataReader and Data Access Application Block:


using Microsoft.ApplicationBlocks.Data; SqlDataReader dr = SqlHelper.ExecuteReader(connection,CommandType.StoredProcedure,"SELECT_PERSON");
STORED PROCEDURE SELECT_PERSON:
SELECT * FROM tblPerson;



Retrieving Multiple Rows using DataSet:

// Selects all the rows from the database
DataSet ds = SqlHelper.ExecuteDataset(connection,CommandType.StoredProcedure,"SELECT_DATA");
// Sends the parameters and returns the dataset
DataSet ds = SqlHelper.ExecuteDataset(connection,CommandType.StoredProcedure,new SqlParameter("@Name",txtName.Text), new SqlParameter("@Age",txtAge.Text));



Retrieving a Single Row


First Create a Stored Proc:

CREATE PROCEDURE getPersonDetails @PersonID int, @Name nvarchar(40) OUTPUT,@Age nvarchar(40) OUTPUTASSELECT @Name = Name,@Age = AgeFROM tblPersonWHERE PersonID = @PersonID


As you see in the stored procedure that we have marked the parameters with OUTPUT which means that when the execution of stored procedure is completed those parameters with OUTPUT keyword will be returned to the caller



SqlParameter [] arParms = new SqlParameter[3];
// @PersonID Input Parameter
arParms[0] = new SqlParameter("@PersonID", SqlDbType.Int );
arParms[0].Value = personID;
// @ProductName Output Parameter
arParms[1] = new SqlParameter("@Name", SqlDbType.NVarChar, 40);
arParms[1].Direction = ParameterDirection.Output;
// @UnitPrice Output Parameter
arParms[2] = new SqlParameter("@Age", SqlDbType.NVarChar,40);
arParms[2].Direction = ParameterDirection.Output;
// Execute the stored procedure
SqlHelper.ExecuteNonQuery( connectionString, CommandType.StoredProcedure, "getPersonDetails", arParms);
// create a string array of return values and assign values returned from stored procedurestring [] arReturnParms = new string[2];
arReturnParms[0] = arParms[1].Value.ToString();
arReturnParms[1] = arParms[2].Value.ToString();



Retrieving XML Data:
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
// Call ExecuteXmlReader static method of SqlHelper class that returns an XmlReader
// We pass in an open database connection object, command type, and command text
XmlReader xreader = SqlHelper.ExecuteXmlReader(conn, CommandType.Text, "SELECT * FROM Products FOR XML AUTO" );
return xreader;