Tuesday, 16 December 2014

Constructor in C#

Constructor :

1.Constructor is a special method which will invoke automatically whenever an  object of class is cretaed.
2.constructor does not have return type and name of the constructor should be same as class Name.
3.if you create class with out constructor,the complier  will create a default constructor for that class.
4.Every class can contain atleast one constructor.
5.constructor is responsible for initialization of object and allocating memory to class.(initialization of fields)


Note:
A class contain any number of constructors.
A constructor doesn’t have any return type even void.
A static constructor can not be a parameterized constructor.
Within a class you can create only one static constructor.

Types of Constructors:

1.Default Constructor

   A Constructor with out any parameters is called default constructor.

2.Parameterized constructor

   A constructor with atleast one parameter is called paramerterised constructor.

3.Copy constructor

  A Parameterized constructor that contains a parameter of same class type is called as copy .
Constructor.

4.Static constructor

     Static constructor will not accept any parameters becaues it is automatically called by CLR.
     Static constructor will not have any access modifiers
     Only one  Static constructor will allowed.
      Static constructor will execute automatically whenever we create first instance of class .

5.private constructor

    Private constructor is used to restrict the class from being instantiated when it contains every      member as static.
    One use of private construct is when we have only static member.
     Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
     If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor.

Example :

 public class Sample
    {
        public string Param1 { get; set; }
        public string Param2 { get; set; }
        public static string test { get; set; }
        //default constructor
        public Sample()
        {
            Param1 = "Hello";
            Param2 = "Constructor";
        }
        //parameterized constructor
        public Sample(string str1, string str2)
        {
            Param1 = str1;
            Param2 = str2;
        }
        //copy constructor
        public Sample(Sample obj)
        {
            Param1 = obj.Param1;
            Param2 = obj.Param2;
        }
        // static constructor
        static Sample()
        {
            test = "Static Constructor";
        }
        //private constructor
        private Sample()
        {

        }
    }

Monday, 25 August 2014

Find Nth Highest Salary of Employee



SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM  Tb_EmpDetails
ORDER BY salary DESC) a
ORDER BY salary

method1:
2nd highest salary from table

SELECT TOP 1 Salary FROM
(
      SELECT DISTINCT TOP 2 Salary FROM Tb_EmpDetails ORDER BY Salary DESC
) AS T ORDER BY Salary ASC


get 3rd highest salary from table

SELECT TOP 1 Salary FROM
(
      SELECT DISTINCT TOP 3 Salary FROM Tb_EmpDetails ORDER BY Salary DESC
     ) AS T ORDER BY Salary ASC

et 4th, 5th, 6th...nth salary using the following query structure

SELECT TOP 1 Salary FROM
(
      SELECT DISTINCT TOP N Salary FROM Tb_EmpDetails ORDER BY Salary DESC
) AS T ORDER BY Salary ASC

Method2:
get 2nd highest salary from table

SELECT MAX(Salary) AS 'Salary' FROM Tb_EmpDetails
WHERE Salary NOT IN
(
 SELECT DISTINCT  TOP 1 (SALARY) FROM Tb_EmpDetails ORDER BY Salary DESC
)

get 3rd highest salary from table

SELECT MAX(Salary) AS 'Salary' FROM Tb_EmpDetails
WHERE Salary NOT IN
(
 SELECT DISTINCT  TOP 2 (SALARY) FROM Tb_EmpDetails ORDER BY Salary DESC
)

get 4th, 5th, 6th...nth salary using the following query structure

SELECT MAX(Salary) AS 'Salary' FROM Tb_EmpDetails
WHERE Salary NOT IN
(
 SELECT DISTINCT TOP N-1(SALARY) FROM Tb_EmpDetails ORDER BY Salary DESC
)

Method 3:

get 2nd highest salary from table

SELECT MIN(Salary) AS 'Salary' FROM Tb_EmpDetails
WHERE Salary IN
(
 SELECT DISTINCT  TOP 2 Salary FROM Tb_EmpDetails ORDER BY Salary DESC
)

get 4th, 5th, 6th...nth salary using the following query structure

SELECT MIN(Salary) AS 'Salary' FROM Tb_EmpDetails
WHERE Salary IN
(
 SELECT DISTINCT  TOP N Salary FROM Tb_EmpDetails ORDER BY Salary DESC
)

Method 4:

SELECT MAX(salary)AS 'Salary' FROM Tb_EmpDetails WHERE salary NOT IN (SELECT MAX(salary) FROM Tb_EmpDetails)

Method 5:

SELECT MAX(salary) AS 'Salary' FROM Tb_EmpDetails WHERE salary < (SELECT MAX(salary) FROM Tb_EmpDetails)

Tuesday, 29 July 2014

Difference between Method Overriding and Method Hiding



Difference between Method Overriding and Method Hiding

Method Overriding :

public class BaseClass
{
   public virtual void print()
   {
    console.WriteLine("Base Class Print Method");
   }
}
Public class DerivedClass : BaseClass
{
 public Override void print()
 {
  console.WriteLine("Derived Class Print Method");
 }

}

Public Class program
{
 public static void Main()
 {
  BaseClass b=new DerivedClass();
  b.print();
 }

}

output: Derived Class Print Method

Note: In method overriding  a base class reference variable pointing to the child class object,
will invoke overridden method in the child class.

Method Hiding:

public class BaseClass
{
   public void print()
   {
    console.WriteLine("Base Class Print Method");
   }
}
Public class DerivedClass : BaseClass
{
 public new void print()
 {
  console.WriteLine("Derived Class Print Method");
 }

}

Public Class program
{
 public static void Main()
 {
  BaseClass b=new DerivedClass();
  b.print();
 }

}

output: Base Class Print Method

Note:
In method hiding a base class reference variable pointing to the child class object,
will invoke the hidden method in the Base class.

-- To Hide the base class members from derived class using new keyword

how to invoke hidden base class member from derived class?

there are three ways to invoke hidden base class member from derived class

1.using base keyword

public class BaseClass
{
   public void print()
   {
    console.WriteLine("Base Class Print Method");
   }
}
Public class DerivedClass : BaseClass
{
 public new void print()
 {
   base.print();
 }

}

Public Class program
{
 public static void Main()
 {
  DerivedClass d=new DerivedClass();
  d.print();
 }

}

output: Base Class Print Method

2.cast child type to parent Type and invoke the hidden member

public class BaseClass
{
   public void print()
   {
    console.WriteLine("Base Class Print Method");
   }
}
Public class DerivedClass : BaseClass
{
 public new void print()
 {
   base.print();
 }

}

Public Class program
{
 public static void Main()
 {
  DerivedClass d=new DerivedClass();
  var b=(BaseClass)d;
  b.print();
 }

}

output: Base Class Print Method

3.  BaseClass b=new DerivedClass();
    b.print();
 see this example in acove

Thursday, 29 May 2014

cte in sql

CTE are commonly used for storing data temporarily in SQL Server.

delete duplicate records in table using cte :

WITH EmployeesCTE AS
(
   SELECT *, ROW_NUMBER()OVER(PARTITION BY ID ORDER BY ID) AS RowNumber
   FROM Employees
)
DELETE FROM EmployeesCTE WHERE RowNumber > 1



N th highest salary

using Max() function:

Select Max(Salary) from Employees

use a sub query along with Max() function :

Select Max(Salary) from Employees where Salary < (Select Max(Salary) from Employees)

To find nth highest salary using Sub-Query :

SELECT TOP 1 SALARY
FROM (
      SELECT DISTINCT TOP N SALARY
      FROM EMPLOYEES
      ORDER BY SALARY DESC
      ) RESULT
ORDER BY SALARY

To find nth highest salary using CTE :

WITH RESULT AS
(
    SELECT SALARY,
           DENSE_RANK() OVER (ORDER BY SALARY DESC) AS DENSERANK
    FROM EMPLOYEES
)
SELECT TOP 1 SALARY
FROM RESULT
WHERE DENSERANK = N


To find 2nd highest salary we can use any of the above queries. Simple replace N with 2.

Similarly, to find 3rd highest salary, simple replace N with 3.

WITH RESULT AS
(
    SELECT SALARY,
           ROW_NUMBER() OVER (ORDER BY SALARY DESC) AS ROWNUMBER
    FROM EMPLOYEES
)
SELECT SALARY
FROM RESULT
WHERE ROWNUMBER = 3

Friday, 23 May 2014

splitting table column value in sqlserver



Declare @delimiter VARCHAR(50)
Set @delimiter=','
;WITH Cte AS
(
SELECT
[Student ID],
[Student Name],
-- Replace the delimiter to the opeing and closing tag
--to make it an xml document
CAST('<M>' + REPLACE([Code], @delimiter , '</M><M>') + '</M>' AS XML) AS [Code]
FROM [Student]
)
Select
[Student ID],
[Student Name],
--Query this xml document via xquery to split rows
Split.a.value('.', 'VARCHAR(MAX)') AS [Code]
FROM Cte
CROSS APPLY [Code].nodes('/M')Split(a)


--------------------------------------------------------------------------
 ;with cte as
(
select row_number()over(order by (select 0)) row,* from [ufn_Split] (@Typeid,',')
),
cte1 as
(
select row_number()over(order by (select 0)) row,* from [ufn_Split] (@Amount,',')
)

insert into dbo.SA_FeeStructure(Typeid,Amount, courseid,remarks,Status,Date,Duration)
select convert(int,a.[value]),b.[value],@courseid,@remarks,@Status,getdate(),@Duration from cte a,cte1 b where a.row=b.row

Friday, 4 April 2014

jquery event differences

document.ready event :

document.ready event will fire whenever DOm is loaded

window.load event :

window.load event will fire after loading all assets(images,Iframes etc) of webpage.


ex1:
 <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
        //first DOM will load
            $('document').ready(function() {
                alert('DOM loaded');
            });
        //in this program, after image will load this event will fire.
            $(window).load(function() {
                alert("window loaded");
            });
        </script>
</head>
<body>
     <img src="http://www.all-wallpapers.net/wallpapers/2012/11/Dimensional-Images-1050x1680.jpg" />
</body>


ex2:

$(document).ready(function () {
    $(document.body).css('background-color', 'silver');
});

$(window).load(function () {
    alert('this will alert after all the window resources completly loaded');
    $(document.body).css("background-color", "green");
});

Thursday, 30 January 2014

Maximum Length Control of multiline Textbox in asp.net using jquery




 <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $('#txtComment').keypress(function (evt) {
                var maxLength = 50;
                // Allow Delete & BackSpace keys
                if (evt.keyCode == 8 || evt.keyCode == 46) return true;
                // Allow Shift, Ctrl & Tab Key
                if (evt.keyCode == 16 || evt.keyCode == 17 || evt.keyCode == 9) return true;
                // Allow Arrow Keys
                if (evt.keyCode == 37 || evt.keyCode == 38 || evt.keyCode == 39 || evt.keyCode == 40) return true;
                // Check and restrict other Keys
                if ($(this).val().length > maxLength) {
                    return false;
                }
            });
            //
            $('#txtComment').blur(function () {
                var maxLength = 50;
                var text = $(this).val();
                if ($(this).val().length > maxLength) {
                    $(this).val(text.substr(0, maxLength));
                }
            });
        });
    </script>