Sunday 30 March 2014

Delegate In C#

Introduction
Delegate is like a buzz word in C#.NET programming. In this article, I explain delegates, multicast delegates and their usage with the help of simple C# programs.
What is a Delegate?
Delegate is a type which  holds the method(s) reference in an object. It is also referred to as a type safe function pointer.
Advantages
·         Encapsulating the method's call from caller
·         Effective use of delegate improves the performance of application
·         Used to call a method asynchronously
Declaration
public delegate type_of_delegate delegate_name()
Example:
public delegate int mydelegate(int delvar1,int delvar2)
Note
·         You can use delegates without parameters or with parameter list
·         You should follow the same syntax as in the method
(If you are referring to the method with two 
int parameters and int return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)
Sample Program using Delegate

public delegate double Delegate_Prod(int a,int b);
class Class1
{
    static double fn_Prodvalues(int val1,int val2)
    {
        return val1*val2;
    }
    static void Main(string[] args)
    {
        //Creating the Delegate Instance
        Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
        Console.Write("Please Enter Values");
        int v1 = Int32.Parse(Console.ReadLine());
        int v2 = Int32.Parse(Console.ReadLine());
        //use a delegate for processing
        double res = delObj(v1,v2);
        Console.WriteLine ("Result :"+res);
        Console.ReadLine();
    }
}

Explanation
Here I have used a small program which demonstrates the use of delegate.
The delegate "Delegate_Prod" is declared with double return type and accepts only two integer parameters.
Inside the class, the method named fn_Prodvalues is defined with double return type and two integer parameters. (The delegate and method have the same signature and parameter type.)
Inside the Main method, the delegate instance is created and the function name is passed to the delegate instance as follows:
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
After this, we are accepting the two values from the user and passing those values to the delegate as we do using method:
delObj(v1,v2);
Here delegate object encapsulates the method functionalities and returns the result as we specified in the method.
Multicast Delegate
What is Multicast Delegate?

It is a delegate which holds the reference of more than one method.
Multicast delegates must contain only methods that return void, else there is a run-time exception.
Simple Program using Multicast Delegate
delegate void Delegate_Multicast(int x, int y);
Class Class2
{
    static void Method1(int x, int y)
    {
        Console.WriteLine("You r in Method 1");
    }

    static void Method2(int x, int y)
    {
        Console.WriteLine("You r in Method 2");
    }

    public static void <place w:st="on" />Main</place />()
    {
        Delegate_Multicast func = new Delegate_Multicast(Method1);
        func += new Delegate_Multicast(Method2);
        func(1,2);             // Method1 and Method2 are called
        func -= new Delegate_Multicast(Method1);
        func(2,3);             // Only Method2 is called
    }
}

Explanation
In the above example, you can see that two methods are defined named method1 and method2 which take two integer parameters and return type as void.
In the main method, the Delegate object is created using the following statement:
Delegate_Multicast func = new Delegate_Multicast(Method1);
Then the Delegate is added using the += operator and removed using the -= operator.


Sunday 23 March 2014

SQL String or Binary Data Would be Truncated. The Statement has been Terminated

Introduction: 

Here I will explain how to solve the problem of “string or binary data would be truncated. The statement has been terminated.” in sql server. 

Description: 

One day I am trying to insert data into one table using SQL queries at that time I got error like “String or binary data would be truncated. The statement has been terminated.” Actually this problem because of I declared column datatype
 varchar(25) but I am inserting data more than 25 characters in that column. To solve this problem I modified column datatype varcha (25) to varchar(50)

I will explain with one example I have query like this

DECLARE @UserDetails TABLE(UserId INT, UserName VARCHAR(25),Designation VARCHAR(10))
INSERT INTO @UserDetails(UserId,UserName,Designation)
VALUES(1,'Jitendra gangwar','Senior Software Engineer')
SELECT * FROM @UserDetails
If you observe above query I declared Designation field with VARCHAR(10) and inserting more than 10 characters because of that I got error like

Msg 8152, Level 16, State 14, Line 2
String or binary data would be truncated.
The statement has been terminated.

(0 row(s) affected)
To solve this problem I changed Designation datatype VARCHAR(10) to VARCHAR(50) and run the below query  
DECLARE @UserDetails TABLE(UserId INT, UserName VARCHAR(25),Designation VARCHAR(50))
INSERT INTO @UserDetails(UserId,UserName,Designation)
VALUES(1,'Jitendra gangwar','Senior Software Engineer')
SELECT * FROM @UserDetails