8 September 2012

Web application vs Webservice vs WCF in asp.net


Web application vs Webservice vs WCF in asp.net

Disadvantage of Web application
-------------------------------
In web application, its not possible to connect different technologies for eg[Java and Dotnet] and not possible to connect the application in two different servers.

Why Webservice?
-------------------
Webservice use SOAP (Simple Object Access protocol) and it is used to connect the web application using different type of technologies, and possible to connect the application that have hosted in different type of servers.

Disadvantage of Webservice
-----------------------------
Webservice use only HTTP Protocol.
It provides only singlex communication, not half duplex and full duplex communication.
They work in an stateless fasion over HTTP and are hosted inside a web server like IIS

Half duplex
-----------
Half-duplex data transmission means that data can be transmitted in both directions on a signal carrier, but not at the same time.
half-duplex transmission implies a bidirectional line (one that can carry data in both directions).

They work in an stateless fashion over HTTP and are hosted inside a web server like IIS

Full duplex
------------
Full-duplex data transmission means that data can be transmitted in both directions on a signal carrier at the same time.
Full-duplex transmission necessarily implies a bidirectional line (one that can move data in both directions).


Advantages of WCF Service
-----------------------------
WCF Service support  HTTP, TCP, IPC, and even Message Queues for communication.
We can consume Web Services using server side scripts (ASP.NET), JavaScript Object Notations (JSON), and even REST (Representational State Transfer).
It can be configured to have singlex, request-response, or even full duplex communication.
These can be hosted in many ways inside IIS, inside a Windows service, or even self hosted.

Difference between Sleep and Hibernate in Laptops?


Difference between Sleep and Hibernate in Laptops?

-Sleep is a power-saving state that allows a computer to quickly resume full-power operation, Sleep puts your work and settings in memory and draws a small amount of power.

-Hibernation is also a power-saving state designed primarily for laptops. It puts your open documents
 and programs on your hard disk and then turn off your computer.

Off all the power-saving states in Windows, hibernate uses the least amount of power.

If you won't use your laptop for an extended period and won't have an opportunity to charge the battery during that time - Hibernate is most efficient one.



21 August 2012

SQL SERVER – Query to Find Seed Values, Increment Values and Current Identity Column value of a table



SQL SERVER – Query to Find Seed Values, Increment 

Values and Current Identity Column value of a table



SELECT IDENT_SEED(TABLE_NAME) AS Seed,
IDENT_INCR(TABLE_NAME) AS Increment,
IDENT_CURRENT(TABLE_NAME) AS Current_Identity,
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
AND TABLE_TYPE = 'BASE TABLE' and
 TABLE_NAME='MSubject' -- [Table name]


For all the table in Database:


SELECT IDENT_SEED(TABLE_NAME) AS Seed,
IDENT_INCR(TABLE_NAME) AS Increment,
IDENT_CURRENT(TABLE_NAME) AS Current_Identity,
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
AND TABLE_TYPE = 'BASE TABLE'

7 August 2012

Lamda expression in C#

Lamda expression  in C#

lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to".

The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."

 This expression can be assigned to a delegate type as follows:


delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}


To create an expression tree type:

using System.Linq.Expressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Expression myET = x = > gt; x * x;

}
}
}

The =>; operator has the same precedence as assignment (=) and is right-associative.
Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as Where.

6 August 2012

Anonymous Types in C#

Anonymous Types in C#

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

The following example shows an anonymous type that is initialized with two properties named Amount and Message.

var v = new { Amount = 108, Message = "Hello" };

// Rest the mouse pointer over v.Amount and v.Message in the following
// statement to verify that their inferred types are int and string.
Console.WriteLine(v.Amount + v.Message);
 In the following example, the names of the properties of the anonymous type are Color and Price.

var productQuery = 
    from prod in products
    select new { prod.Color, prod.Price };

foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}

11 July 2012

String vs string in c#

String vs string in C#

we use either the keyword string or  String .

Is both String and string same in C# ?

string is just an alias name for the class System.String which means both has the same functionality.
The IL code generated for both of them is also same .
Note that string is a keyword , but String isn’t which lets you create an variable with the name String like


Another interesting thing is we can also declare variable name as String

 String String = "Arun Prakash";
 MessageBox.Show(String);


 If you declare the variable name as string, Error occured

 String string = " Arun Prakash";
 MessageBox.Show(string);







Use of Using Statement in C#


Use of Using Statement in C#

In C#, using Keyword is used in two different ways and are hence referred to differently.
 As a Directive
In this usage, the "using" keyword can be used to include a namespace in your program. (Mostly used)
As a Statement
Using can also be used in a block of code to dispose an IDisposable objects.

SqlConnection connection = new SqlConnection("connection string");
SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection);
SqlDataReader reader = cmd.ExecuteReader();
connection.Open();
if (reader != null)
{
      while (reader.Read())
           {
                          //do something
           }
}
reader.Close(); // close the reader
connection.Close(); // close the connection

  
Disadvantages of the above code:

If any exception occurs inside while block it throws exception, the connection.close() process will not executed due to the exception. To avoid this situation, we can take the help of the try....catch... finally block   by closing the connection inside the finally block or inside the catch block.

Advantages of the below code:

"Using" keyword takes the parameter of type IDisposable. Whenever you are using any IDisposable type
object you should use the "using" keyword to handle automatically when it should close or dispose.
Internally using keyword calls the Dispose() method to dispose the IDisposable object.

using (SqlConnection connection = new SqlConnection("connection string"))
{

connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection))
{

   using (SqlDataReader reader = cmd.ExecuteReader())
   {
         if (reader != null)
        {
             while (reader.Read())
                {
                   //do something
                }
         }
   } // reader closed and disposed up here

// command disposed here
//connection closed and disposed here

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...