Thursday, 30 May 2013

HTTP Handler vs HTTP Modules

 we will see what are http handlers and modules. And in which condition which one is suitable. For that first we need to understand when user request
application resource to web server, what happened?
The user requests for a resource on web server.
The web server examines the file name extension of the requested file,
and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension. For example when an .aspx page is requested
it is passed to ASP.Net page handler.
Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse are created.
 Then instance of HttpApplication is created and also instance of any configured modules. One can register different events of HttpApplication class like BeginRequest,
 AuthenticateRequest, AuthorizeRequest, ProcessRequest etc.

HTTP Handler
HTTP Handler is the process which runs in response to a HTTP request. So whenever user requests a file it is processed by the handler based on the extension.
 So, custom http handlers are created when you need to special handling based on the file name extension. Let's consider an example to create RSS for a site. So,
 create a handler that generates RSS-formatted XML. Now bind the .rss extension to the custom handler.

HTTP Modules
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is passed through all the modules in the pipeline of the request.
So generally http modules are used for:
Security: For authenticating a request before the request is handled.
Statistics and Logging: Since modules are called for every request they can be used for gathering statistics and for logging information.
Custom header:  Since response can be modified, one can add custom header information to the response.

Tuesday, 21 May 2013

write a program int i=123 calculate sum=1=2=3 optimized solution


write a program int i=123 calculate sum=1=2=3 optimized solution

q: How to sum individual digits of integer?

I have an integer value (ex: 723) and i want to add up all the values in this integer until i get a single value.


ex: 7 + 2 + 3 = 12
    1 + 2     = 3


Ans


int i = 723;
int acc;
do {
    acc = 0;
    while (i > 0)
    {
        acc += i % 10;
        i /= 10;
    }
    i = acc;
} while(acc>=10);
% 10 gives the final digit each time, so we add that into an accumulator. /= 10 performs integer division, essentially removing the final digit each time. Then we repeat until we have a small enough number.
---------------------------------------------------------------------------------


hint....
Using conversion from int to string and back probably isn't that fast. I would use the following
public static int[] ToDigitArray(int i)
{
    List<int> result = new List<int>();
    while (i != 0)
    {
        result.Add(i % 10);
        i /= 10;
    }
    return result.Reverse().ToArray();
}

C# - Finding Greatest Common Divisor (GCD) and Least Common Multiple (LCM)


C# - Finding Greatest Common Divisor (GCD) and Least Common Multiple (LCM)







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

namespace ConsoleApplication1
{
    class Program
    {
        static int GetGCD(int num1, int num2)
        {
            while (num1 != num2)
            {
                if (num1 > num2)
                    num1 = num1 - num2;

                if (num2 > num1)
                    num2 = num2 - num1;
            }
            return num1;
        }


        static int GetLCM(int num1, int num2)
        {
            return (num1 * num2) / GetGCD(num1, num2);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("C# Program for LCM and GCD");

            Console.Write("Enter First Number: ");
            int a = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter Second Number: ");
            int b = Convert.ToInt32(Console.ReadLine());

            int gcd = GetGCD(a, b);
            int lcm = GetLCM(a, b);

            Console.WriteLine("\nGCD({0,4},{1,4}) = {2,6}", a, b, gcd);
            Console.WriteLine("\nLCM({0,4},{1,4}) = {2,6}", a, b, lcm);

        }
    }
}

Output



C# Program for LCM and GCD
Enter First Number: 10
Enter Second Number: 135

GCD(10,135) = 5
LCM(10,135) = 270

GCD of 2 numbers using C#.NET


Write a program to find GCD of 2 numbers using C#.NET


using System;
class myclass
{
    static void Main()
    {
        int i1, i2;

        Console.WriteLine("Enter 2 numbers to find GCD");
        i1 = int.Parse(Console.ReadLine());
        i2 = int.Parse(Console.ReadLine());

        int n1, n2;
        //Making sure n1 is greater than n2
        if (i1 > i2)
        {
            n1 = i1;
            n2 = i2;
        }
        else
        {
            n1 = i2;
            n2 = i1;
        }
        int result = gcd(n1, n2);
        Console.WriteLine("The GCD of {0} and {1} is {2}",i1,i2,result);
        Console.Read();
    }

    private static int gcd(int n1, int n2)
    {
        int rem = 5;
        while (n2 > 0)
        {
            rem = n1 % n2;
            if (rem == 0)
                return n2;
            n1 = n2;
            n2 = rem;

        }
        //gcd of any number with 0 is number itself.

        return n1;

    }
}

Output

Enter 2 numbers to find GCD
125
85
The GCD of 125 and 85 is 5

Thursday, 16 May 2013

What is the use of “overrides” and “overridable” keywords


Question. What is the use of “overrides” and “overridable” keywords

Overridable is used in parent class to indicate that a method can be overridden. Overrides
is used in the child class to indicate that you are overriding a method

sub query with comma delimited output in one column [duplicate]


sub query with comma delimited output in one column [duplicate]


I need help with writing a query where the query output displays a column with comma delimited data.
I have two tables with one to many relationship. I want to write a query which outputs all records from table with one record but display comma delimited data in one column from the table having many records.
Table1
Col1 Col2 Col3
a1     1   4
a2     2   5
a3     3   6
a4     7   8
Table2
Col1 Col4
a1     10
a1     11
a1     22
a2     10
a2     11
a3     19
a3     22
a3     23
a4     15
Query output:
Col1 Col2 Col3 Col4
a1     1   4    10,11,22
a2     2   5    10,11
a3     3   6    19,22,23
a4     7   8    15
You can use the following:
select t1.col1,
  t1.col2, 
  t1.col3,
  left(t2.col4, len(t2.col4)-1) col4
from table1 t1
cross apply
(
  select cast(t2.Col4 as varchar(10)) + ', '
  from Table2 t2
  where t1.col1 = t2.col1
  FOR XML PATH('')
) t2 (col4)

or
Or you can use:
select t1.col1,
  t1.col2, 
  t1.col3,
  STUFF(
         (SELECT ', ' + cast(t2.Col4 as varchar(10))
          FROM Table2 t2
          where t1.col1 = t2.col1
          FOR XML PATH (''))
          , 1, 1, '')  AS col4
from table1 t1

subtracting col2 from col1


select col1 as figure1, col2 as figure2, col1 - col2 as result 
from table1