Tuesday, June 25, 2013

What is delegate

q-1. What is delegate.
A delegate is a class whose object (delegate object) 
can store a set of references to methods. This delegate
object is used to invoke the methods


The Delegate class is the base class for delegate types. However, only the 

system and compilers can derive explicitly from the Delegate class or from the 

MulticastDelegate class. It is also not permissible to derive a new type from a

 delegate type. The Delegate class is not considered a delegate type; it is a 

class used to derive delegate types

q-2. How delegate Works internally .

whenever we declare delegate in c# initially it is
behaving as a keyword but when program gets compiled
the keyword called delegate converted into class and
compiler makes sealed class for delegate.

The sealed class implicitly inherited from multicast
delegate and multicastDelegate class implicitly inherited
from Delegate class.

multicastDelegate class and Delegate class both are marked
as a Abstract.

q-3. Why multicastDelegate class is needed?

A delegate is a class whose object (delegate object)
can store a set of references to methods means the
object of delegate class can hold the multiple method
references due to multicastDelegate.

q-4. Why Delegate class is needed?
The Delegate class plays a major role in case of
Asynchronous call back patteren.

Delegate class is carrying some major functions
like BeginInvoke() and EndInvoke() .

suppose we create a class for GUI Level like Windows Form,Web Form.
In this case we can't call a function of that class without Event.

Event can't created without delegate.There are several inbuilt
delegate class are(EventHandler,PageHandler,WebServiceHandler).

eg
using System;
using System.Windows.Forms;

class MyWin:Form
{

Button b1;

public MyWin()
{
b1=new Button();
b1.Text="Save";
this.Controls.Add(b1);

b1.Click+=new EventHandler(save);
}

protected void save(Object o,EventArgs e1)
{
MessageBox.Show("Ready to save the record in a table");
}
public static void Main()
{
MyWin m=new MyWin();
m.ShowDialog();
}
}

// In the above program,class MyWin inherited from
Form class and Button is added to the Form.
It shows if user wants to perform any action on
the he has to click on a Button.Means without
Click he can't perform any action.

Button class is provided the Event and for Event
delegate is needed.

IAsyncResult Interface

Represents the status of an asynchronous operation.
The IAsyncResult interface is implemented by classes containing methods that can operate asynchronously. It is the return type of methods that initiate an asynchronous operation, such as FileStream.BeginRead, and it is passed to methods that conclude an asynchronous operation, such as FileStream.EndReadIAsyncResult objects are also passed to methods invoked by AsyncCallback delegates when an asynchronous operation completes.

using System;
using System.Data.SqlClient; 

public class DBProcessing
{
static SqlConnection con = new SqlConnection("initial catalog=cac;data source=WINDOWS-PC\\SQLEXPRESS;integrated security=yes;async=true");
static SqlCommand cmd = new SqlCommand("insert into student values(101,'kaur')");
static SqlCommand cmd1 = new SqlCommand("select * from student");

public static void Perform_TASK_CMD()
{
cmd.Connection = con;
con.Open();
IAsyncResult res=cmd.BeginExecuteNonQuery(null,null); //At this point,New Thread will be allocated for execute the task
Console.WriteLine("Perfect example of Encapsulation...");

while (!res.IsCompleted)
{
Perform_TASK_CMD1(); //This Will run under the Main Thread

}
int t=cmd.EndExecuteNonQuery(res);

}

public static void Perform_TASK_CMD()
{
cmd.Connection = con;
con.Open();
IAsyncResult res=cmd.BeginExecuteNonQuery(null,null); //At this point,New Thread will be allocated for execute the task
Console.WriteLine("Perfect example of Encapsulation...");

while (!res.IsCompleted)
{
Perform_TASK_CMD1(); //This Will run under the Main Thread

}
int t=cmd.EndExecuteNonQuery(res);

}

public static void Perform_TASK_CMD1()
{
Console.WriteLine(" I am Performing");

}

}

class Froent_End
{
// DBProcessing class needs to access frequently

public static void Main() // Single Thread
{

DBProcessing.Perform_TASK_CMD();

}

}

Monday, June 24, 2013

How to Find 3th Highest Salary In Sql Server 2008

First create Table ----
--CREATE TABLE ED(ecode INT,ename VARCHAR(10),edept VARCHAR(10))
--CREATE TABLE ES(ecode INT,Esal INT ,eSaltype VARCHAR(10))

then Enter the Value 
--INSERT INTO ED( ecode, ename, edept)VALUES(1,'virendra','it')
--INSERT INTO ED( ecode, ename, edept)VALUES(2,'Ajit','mgt')
--INSERT INTO ED( ecode, ename, edept)VALUES(3,'pavan','it')
--INSERT INTO ED( ecode, ename, edept)VALUES(4,'kuldeep','mgt')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(1,3000,'basic')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(1,3000,'basic')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(1,1000,'insetive')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(2,5000,'basic')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(2,3000,'bounse')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(3,7000,'basic')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(3,3000,'bounse')
--INSERT INTO ES( ecode, Esal, eSaltype)VALUES(3,8000,'intsetive')

then After Create the Query to Find Any Highest Salary

SELECT a.[ecode] Code,a.[ename] Name,SUM(b.[Esal]) Salary FROM [MyDataDb].[dbo].[ED] AS a INNER JOIN [MyDataDb].[dbo].[ES] AS B ON b.ecode=a.ecode GROUP BY a.edept,a.ecode,a.ename

SELECT Salary,Name,ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RN FROM  
( SELECT a.[ecode] Code,a.[ename] Name,SUM(b.[Esal]) Salary FROM [MyDataDb].[dbo].[ED] AS a INNER JOIN [MyDataDb].[dbo].[ES] AS B ON b.ecode=a.ecode GROUP BY a.edept,a.ecode,a.ename)a

SELECT Salary,Name FROM (SELECT Salary,Name,ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RN FROM  

( SELECT a.[ecode] Code,a.[ename] Name,SUM(b.[Esal]) Salary FROM [MyDataDb].[dbo].[ED] AS a INNER JOIN [MyDataDb].[dbo].[ES] AS B ON b.ecode=a.ecode GROUP BY a.edept,a.ecode,a.ename)a )c WHERE c.RN=2 

Thursday, June 20, 2013

difference between SQL Server 2005 and SQL Server 2008

SQL SERVER 2005:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is introduced.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Cant encrypt
13.Can Compress tables and indexes.(Introduced in 2005 SP2)
14.Datetime is used for both date and time.
15.Varchar(max) and varbinary(max) is used.
16.No table datatype is included.
17.SSIS is started using.
18.CMS is not available.
19.PBM is not available.
20.PIVOT and UNPIVOT functions are used.
SQL SERVER 2008:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is used.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Can encrypt the entire database introduced in 2008.
 --check it(
http://technet.microsoft.com/en-us/library/cc278098(SQL.100).aspx)
   (
http://www.sqlservercentral.com/articles/Administration/implementing_efs/870/)
   (
http://www.kodyaz.com/articles/sql-server-2005-database-encryption-step-by-step.aspx)
   (
http://www.sql-server-performance.com/articles/dev/encryption_2005_1_p1.aspx)
   (
http://geekswithblogs.net/chrisfalter/archive/2008/05/08/encrypt-documents-with-sql-server.aspx)
13.Can compress tables and indexes.
 -http://www.mssqltips.com/tip.asp?tip=1582
14.Date and time are seperately used for date and time datatype,geospatial and timestamp with internal timezone
 is used.
15.Varchar(max) and varbinary(max) is used.
16.Table datatype introduced.
17.SSIS avails in this version.
18.Central Management Server(CMS) is Introduced.
 -http://msdn.microsoft.com/en-us/library/bb934126.aspx
 -http://www.sqlskills.com/BLOGS/KIMBERLY/post/SQL-Server-2008-Central-Management-Servers-have-you-seen-these.aspx
19.Policy based management(PBM) server is Introduced.
 -http://www.mssqltips.com/tip.asp?tip=1492
 -http://msdn.microsoft.com/en-us/library/bb510667.aspx
20.PIVOT and UNPIVOT functions are used.
 -http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

Sunday, June 16, 2013

WCF+LINQ(Integration)

WCF+LINQ(Integration)
//BL.CS

using System;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;

[DataContract]
[Table(Name="student")]
public class BL
{
[DataMember]
[Column(Name="seat_no",IsPrimaryKey=true)]
public int sno {get;set;}

[DataMember]
[Column(Name="name")]
public string sn { get; set; }
}
//DAL.CS

using System;
using System.Data.Linq;
using System.Collections.Generic;
using System.Linq;
public class DAL
{
Table<BL> b2 = null;

public List<BL> Show_Data()
{
List<BL> st = new List<BL>(); //For manipulation
// For insert,update,delete
using (DataContext dt = new DataContext("initial catalog=cac;integrated security=yes;data source=WINDOWS-PC\\SQLEXPRESS"))
{
st = dt.GetTable<BL>().ToList();
}
return st;

}

public int save(BL b)
{
using (DataContext dt = new DataContext("initial catalog=cac;integrated security=yes;data source=WINDOWS-PC\\SQLEXPRESS"))
{
b2 = dt.GetTable<BL>(); //Call By rererence
b2.InsertOnSubmit(b);
dt.SubmitChanges();
return 1;

}

}
}

//Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Runtime.InteropServices;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
public List<BL> Get_Student()
{

DAL d = new DAL();
return d.Show_Data();
}

public int Save_data(BL b)
{
DAL d2 = new DAL();
d2.save(b);
return 1;

}
}
/IService

//using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
List<BL> Get_Student();

[OperationContract]
int Save_data(BL b);
}

//AT CLIENT SIDE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
GridView1.DataSource = s.Get_Student();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
int r = Convert.ToInt32(TextBox1.Text);
string n = TextBox2.Text;

ServiceReference1.BL a = new ServiceReference1.BL();
a.sno = r;
a.sn = n;

ServiceReference1.ServiceClient s2 = new ServiceReference1.ServiceClient();
int f=s2.Save_data(a);
if (f == 1)
{
GridView1.DataSource = s2.Get_Student();
GridView1.DataBind();
}
}
}

Saturday, June 15, 2013

Error Page for 404 Page Not Found

>> Add below codes in <configuration> Tag in web.config

For Aspx Pages

<system.web>
    <customErrors defaultRedirect="404.aspx" mode="On">
      <error  redirect="404.aspx" statusCode="404" />
    </customErrors>
  </system.web>



For Non-Aspx pages

  <system.webServer>
    <httpErrors errorMode="Custom">
      <remove statusCode="404" subStatusCode="-1" />
      <error responseMode="Redirect" statusCode="404" prefixLanguageFilePath="" 
             path="404.aspx" />
    </httpErrors>
  </system.webServer>



you can design your 404 page using these links
http://www.smashingmagazine.com/2009/01/29/404-error-pages-one-more-time/

Friday, June 7, 2013

Reflection



Reflection contd.......


1. Reflection Provides a classes or Attributes for

1. Writing the Information to metadata.
Reason for Writing the information to Metadata.

1) To Solve the Dll-Hell(P)
assembly:AssemblyVersion(1.0.0.0)

2. To Create Shared Assembly
sn-k,assembly:AssemblyKeyFile("")
3. For Deployment of any given assembly,we need to provide
appropriate information
assembly:AssemblyDescription("")
assembly:AssemblyTrademark("")

2. Reading the Information from metadata.

Reason for Reading the information from Metadata.

1. We need to Find the type of an Assembly.

2. to Find the namespace name and its classes.

3. we must know the type of function or class(virtual,static,sealed,abstract)

** 4.By Reflection we can also access the base class information.

Sometimes he wants to check ur coding part:
1. Versioning
2. Shared Assembly
3. Type of an Assembly

4. Namespace information.
5. class and its base class and its related function.
using System; 
using System.Reflection; 

class vinod { 
static void Main(string[] args) {
Assembly ptr = Assembly.LoadFrom("d:\\kuldeep\\it.dll");
Type[] t = ptr.GetTypes(); 
foreach (Type t2 in t) { 
  if (t2.IsClass) { 
     Console.WriteLine(t2.FullName); 
     List(t2); } } }
static void List(Type t3) {
  MethodInfo[] m = t3.GetMethods(); 
    foreach (MethodInfo m2 in m) { 
         Console.WriteLine(m2.Name); }
}
}//End of Class