Saturday, 1 July 2017

Delegate in c#

What is Delegate in C#.

Delegate is an object which holds the  references to a method  within the delegate object.It is  type safe object and invoking the method asynchronously manner.

Advantage of Delegates

1 Improved the performance of Application
2 Call a method asynchronous.

Type of Delegate 
1. Single Delegate and
2 Multicast  Delegate
3 Generic Delegate

Single Delegate :-A delegate is need to pass single parameter as reference to a method with the delegate object.

using System;
using
 System.Collections.Generic;
using
 System.Text; 
namespace DelegateTest
{
    public delegate void Calc(int x, int y);
    class A    {
        public void add(int x, int y)
        {
            Console.WriteLine("The Sum is " + (x + y));
        } 
        public void sub(int x, int y)
        {
            Console.WriteLine("The Difference is " + (x - y));
            Console.ReadLine();
        }
    }
    class Program    {
        static void Main(string[] args)
        {
            A obj = new A();
            //make an object of class A         

            Calc objsumnew numtest(obj.add);
            objsum(10, 20);
            Calc objsubnew numtest(obj.sub);
            objsub(10, 20);
        }
    }
}


https://msdn.microsoft.com/en-us/library/system.delegate.aspx


Multicast Delegate: - When we need to pass more than one parameter as reference  to a method within delegate object.


Wednesday, 28 June 2017

How to find schema changes report in SQL Server ?

How to find schema  changes report in SQL Server ?

Select o.name As [Objcet_Name],
 s.name [Schema_Name],
 o.type_desc [Description],
 o.create_date [Creation_Date],
 o.modify_date [Modified_Date]
 from sys.all_objects o
 Left outer join sys.schemas s
 on o.schema_id=s.schema_id
 where create_date >(GETDATE()-7) or modify_date >(GETDATE()-7)



Or

We can check SSMS  Wizard .
Step 1:- Select Database 
Step 2:-click right mouse button and  Select Report.
Step 3:- Select Standard 
Step 4: finally Select Schema change History . 

Tuesday, 20 June 2017

How to rest asp.net control in web form.

Protected void Clear()
{
foreach(Control ctrl in Form.Control)
{
//Text Box Control
if(ctr is TextBox)
{
((TextBox )(ctrl)).Text="";
}
// Label Control
else if(ctrl is Label)
{
((Label)(ctrl)).Text = "";
}
//DropDownList controls
else if(ctrl is DropDownList)
{
((DropDownList)(ctrl)).ClearSelection();
or
((DropDownList)(ctrl)).selectedIndex = 0;  
}
//CheckBox controls
  else if (ctrl is CheckBox)
{
 ((CheckBox)(ctrl)).Checked = false;
}
 //RadioButton controls
  else if (ctrl is RadioButton)
        {
     ((RadioButton)(ctrl)).Checked = false;
            }
}
}

Tuesday, 6 June 2017

ASP.NET Interview Q&A

How to Delete Cookies?

HttpCookie currentUser=HttpContext.Current.Request.Cookies["CookiesValue"];
HttpContext.Current.Response.Cookies.Remove("CookiesValue");
currentUser.Expires=DataTime.Now.AddMinutes(-20);
currentUser.value=null;
HttpContext.Current.Response.SetCookie(currentUser);


Delegate is an object which holds the  references to a method  within the delegate object.It is  type safe object and invoking the method asynchronously manner.

Advantage of Delegates

1 Improved the performance of Application
2 Call a method asynchronous.

Type of Delegate 
1. Single Delegate and
2 Multicast  Delegate
3 Generic Delegate


Single Delegate :-A delegate is need to pass single parameter as reference to a method with the delegate object.


Multicast Delegate: - When we need to pass more than one parameter as reference  to a method within delegate object.


--- String and number Reverse--

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        { //---reverse string ----
            //Console.WriteLine("Enter a number for reverse");
            //int number = Convert.ToInt32(Console.ReadLine());
            //int revers = 0;
            //while (number > 0)
            //{
            //    int rem = number % 10;
            //    revers = (revers * 10) + rem;
            //    number = number / 10;
            //}
            //Console.WriteLine(revers);
            //Console.ReadLine();
            //-- Reverse string--
            Console.WriteLine("enter your name");
            string name = Console.ReadLine();
            var temp = "";
            for (int i = name.Length - 1; i >= 0; i--)
            {
                temp += name[i].ToString();
             
            }
            Console.WriteLine(temp);
            Console.ReadLine();

        }
    }
}

----- Duplicate character in string --
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.Write("Enter a word");
            string data = Console.ReadLine();

            RemoveDuplicatecharacter(data);
        }
        static string RemoveDuplicatecharacter(string value)
        {
            string result = string.Empty;
            string temp = string.Empty;
            foreach (char data in value)
            {
                if (temp.IndexOf(data) == -1)
                {
                    temp += data;
                    result += data;
                }
            }
            Console.WriteLine(result);
            Console.Read();
            return result;
           
         
           
        }
    }
}

SQL INTERVIEW QUESTION

tbl_employee
 EmployeeId EmployeeName Gender
1                     ABC                          male
2                      YYC                       Female
Question: update all male into female and all female into Male
Ans';-
Update tbl_employee
Set Gender=case Gender When 'Male' Then Female When  'Female' Then 'Male' Else gender End.

----------------------------------------------------------------------------------------------------------------------------------------


Different between Store procedure and function are

SNo Function  Store Procedure
1Function must return a value.Stored procedure may or not return values.
2Will allow only Select statement, it will not allow us to use DML statements.Can have select statements as well as DML statements such as insert, update, delete
etc
3It will allow only input parameters, doesn’t support output parameters.It can have both input and output parameters.
4It will not allow us to use try-catch blocks.For exception handling, we can use try-catch blocks.
5Transactions are not allowed within functions.Can use transactions within Stored procedures.
6We can use only table variables, it will not allow using temporary tables.Can use both table variables as well as a temporary table in it.
7Stored procedures can’t be called from function.Stored Procedures can call functions.
8Functions can be called from the select statement.Procedures can’t be called from Select/Where/Having etc statements. Execute/Exec
the statement can be used to call/execute the stored procedure.
9function can be used in join clause as a result set.Procedures can’t be used in Join clause

Q- HowTo find out the Nth highest salary (for example: here I am finding 3rd highest salary),
A-
SELECT EmpId, LastName, Salary FROM Employee
EmpId LastName Salary
1 Agarwal 50000
2 Agarwal 50000
4 Kumar 60000
5 Agarwal 75000
6 Agarwal 75000
SELECT EmpId,LastName, Salary FROM Employee a1 WHERE 3-1= (SELECT COUNT(DISTINCT Salary) FROM Employee a2 WHERE a2.Salary > a1.Salary)
EmpId LastName Salary


1 Agarwal 50000
2 Agarwal 50000

SELECT TOP 1 SAL FROM(SELECT DISTINCT TOP 2 SAL FROM EMP ORDER BY SAL DESC) AS TEMP
Order by SAL ASC
----------------------------------------------------------------------------------
-- PAGING---
----------------------------------------------------------------------------------
SELECT * FROM (SELECT ROW_NUMBER() OVER( Order By OrderItems_PKey) AS RowNum,* FROM OrderItems) AS RESULT
WHERE RowNUM  >=10 AND RowNum <=20
order by RowNum

------------------------------------------------------------------------------
select * from InventoryItem-- DELETIG THE DUPLICATE REOCRED
------------------------------------------------------------------------------
DELETE from InventoryItem
where InventoryItem_ID Not in
(select max (InventoryItem_ID)
from InventoryItem
group by Name)
-------------------------------------------------------------------------
SELECT * FROM Orders--- SECOND COSTLY ORDER VALUE

SELECT max(GrandTotal)FROM Orders WHERE GrandTotal NOT IN (SELECT MAX(GrandTotal) FROM Orders);
------------------------------------------------------------------------------------------------
--To get list of primary AND  FOREIGN KEY IN ENTIRE DATABASE------------
--------------------------------------------------------------------------------------------
select distinct
constraint_Name as [ConstraintName],
Table_Name As [Table Name]from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
------------------------------------------------------------------------------

How to find second or nth maximum salary from  Employee table?
Here EMP is Table and SAL Is Salary Filed


SELECT * FROM EMP emp1
WHERE(2)=(SELECT COUNT(DISTINCT(emp2.SAL)) FROM EMP emp2 WHERE emp2.SAL >=emp1.SAL)


----------------------------------------------------------------------------------------------------------------

















Friday, 3 March 2017

Find out the second highest no in array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myarr = new Int32[] { 1, 8, 9, 88, 77, 76, 22, 225 };
            int mxval = 0;
            int secondHv = 0;
            int sum = 0;
            int count = 0;
            int avg = 0;
            foreach (int i in myarr)
            {

                if (i > mxval)
                {
                    secondHv = mxval;
                    mxval = i;

                }
                else if (i > secondHv)
                    secondHv = i;
                sum += i;
                count++;
                avg = sum / count;
              
                        }
            Console.WriteLine(mxval);
            Console.WriteLine(secondHv);
            Console.WriteLine(sum);
            Console.WriteLine(count);
            Console.WriteLine(avg);
            Console.ReadLine();
        }
    }

}

Wednesday, 15 February 2017

Constructor in C#


A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

Some key point of constructor
·         Constructor is member of a class and structure.
·         Constructor is a special type of function which will be fire at time of object is creating or class is load.
·         Constructor name is always same name as class name.
·         Constructor not contains any return type, because it not returns any value.
·         Constructor can contain a parameter or not.

Syntax of constructor
<Access modifier><class name (type><parameter>)>
{
Code initialization
}

Instance constructor:-While define a constructor without using static key word is known as instance constructor.
Key point
·        Only instance constructor can hold parameter
·        It initialize at the time of object is created.
·        In a single class multiple instance constructors can be created but condition is each constructor should be unique to other by using different signature and number of parameter should vary.

Static constructor
When a constructor is created as static, it will be invoked only once for all of instances of the class and it are invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Key Point constructor
Ø A static constructor does not take access modifiers or have parameters.
Ø A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
Ø A static constructor cannot be called directly.


A default constructor is a constructor which can be called with no arguments /with any parameter
 Key point  
Ø Definition of the constructor outside the class body.
Ø Inhibiting the automatic generation of a default constructor by the compiler.
Ø Explicitly forcing the automatic generation of a default constructor by the compiler.

using System;
namespace
 DefaultConstractor
 {
  
  class  demo
    {
       
 int a, b;  // class variable declartion
        public addition()   //without  any parameter  or argument
        {
            a = 100;
            b = 175;
        }

        public static void Main()
        {
            addition obj = new addition(); 
            Console.WriteLine(obj.a);
            Console.WriteLine(obj.b);
            Console.Read();
        }
      }
    }


Parameterized Constructor:
A constructor with at least one parameter is called parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class to different values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace parameterizedConstuctor
{
    class Course
    {
        ulong cid;
        string cname;
        double cfees;
        internal Course(ulong CourseID, string courseName, double courseFees)
        {
            cid = CourseID;
            cname = courseName;
            cfees = courseFees;
                    }
        internal void DisplaycourseDetails()
        {
            Console.WriteLine("Course Code : " + cid);
            Console.WriteLine("Couse Name : " + cname);
            //Console.WriteLine("Couse Name : " + string.Format("{0:0.00}"+ cfees));
            // string s = string.Format("Course Fees is {0:0.00}", +cfees);
            // Console.WriteLine(s);
            Console.WriteLine( string.Format("Course Fees is {0:0.00}", +cfees));
        }
        static void Main(string[] args)
        {
            Course obj1 = new Course(101, "C# .net", 1100.00);
            obj1.DisplaycourseDetails();
            Course obj2 = new Course(102, "Angular", 7000.00);
            obj2.DisplaycourseDetails();
            Course obj3 = new Course(103, "MVC", 1100.00);
            obj3.DisplaycourseDetails();
            Console.ReadLine();
        }
    }
}


Secure and Unsecure configuration in Dynamics CRM 365.

Overview : In Dynamics CRM, when we create a plugin, we sometimes need to pass data to that plugin, instead of writing this data inside the ...