Monday 21 April 2014

Anonymous access allows users to view the pages on this site without logging in

Step 1: Enabling the anonymous accesss to the Web application from Central Admin. This does not mean that the whole web application is now anonymous. This step signifies that, this web application has ability to host resources that can be accessed by Anonymous user.





With out user name password we can login site.



Tuesday 15 April 2014

Basic SQL


Constraints
Primary Key Constraint
Unique Key Constraint
Foreign Key Constraint
Not Null Constraint
Check Constraint

A Constraint is a property that we can assign to column in table. 
What is Primary Key?
             Primary key constraint is used to uniquely identify each record in database table. It won’t allow repetition or duplication of data. Each table is having only one primary key constraint and it contains only unique values. Primary key constraint doesn’t accept null values.
Right click the student table and set primary key


It is look like 

Primary key constraint doesn’t accept null values.









AUTO INCREMENT
Very often we would like the value of the primary key field to be created automatically every time a new record is inserted.
We would like to create an auto-increment field in a table.








Query Level


CREATE TABLE [dbo].[tblStudent](
[StudId] [int] IDENTITY(1,1) NOT NULL,
[StudName] [varchar](20) NULL,
[Class] [int] NULL,
 CONSTRAINT [PK_tblStudent] PRIMARY KEY CLUSTERED
(
[StudId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

The primary key created for the StudId column will create a clustered index for the Studid column. A table can have only one clustered index on it.
When creating the clustered index, SQL server 2008 reads the id column and forms a Binary tree on it. This binary tree information is then stored separately in the disc. Expand the table Student and then expand the Indexes. You will see



Unique Key Constraint
Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column. Unique Key allows only one NULL Value.
CREATE TABLE Persons(P_Id int NOT NULL UNIQUE,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255))

Foreign key constraint
A Foreign key in one table point to primary key in another table. The foreign key constraint is used to prevent the actions that would destroy the links between two tables.
NOT NULL Constraint
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.



Check Constraint

The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a single column it allows only certain values for this column.
EX:
CREATE TABLE brand (
    code        char(8)       NOT NULL,
    valid       int           NOT NULL,
    rowid       numeric(10,0) IDENTITY,
    CONSTRAINT brand_pk PRIMARY KEY (code),
 -- The following UNIQUE constraint lets the pair of values be
-- the target of  a foreign key reference.
    CONSTRAINT brand_is_valid UNIQUE (code, valid),
 CONSTRAINT valid_check CHECK (valid IN (0,1))
);

CREATE TABLE product (
    code        char(8)       NOT NULL,
    valid       int           NOT NULL,
 -- The column "code" is a PK in the referenced table, so this still works. It's
    -- a 1:0 or 1:1 relationship.
    CONSTRAINT product_pk PRIMARY KEY (code),
 -- The next constraint requires a unique constraint on the pair of
    -- columns in the table "brand".  By itself, it references every row
    -- in "brand". That's too many rows.
CONSTRAINT product_fk FOREIGN KEY (code, valid)
 REFERENCES brand (code, valid),
 -- But this constraint restricts the foreign key references to only those
-- rows that have valid = 1 in the table "brand".
    CHECK (valid = 1)
);


Brand Table Values



Check Constraint
Table name Brand
CONSTRAINT valid_check CHECK (valid IN (0,1))




 Example Condition :
CHECK (id BETWEEN 100 and 9999)
CHECK (name = upper(name))



The DELETE statement conflicted with the REFERENCE constraint "product_fk". The conflict occurred in database "SampleDB", table "dbo.product".
The statement has been terminated.
How to delete Brand table value?

  • First Delete product table row.
  • After Delete Brand table value.






Wednesday 9 April 2014

Loosely Coupled Application (delegates in c# )



Short Introduction Loose and Tight Coupling
Loose Coupling means reducing dependencies of a class that use a different class directly. In tight coupling, classes and objects are dependent on one another. In general, tight coupling is usually bad because it reduces flexibility and re-usability of code.



Business layer Code
 namespace BusinessLayer
{
    public class Calc
    {
        //public delegate int CalculationHandler(int x, int y);
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }

        public int Mul(int a, int b)
        {
            return a * b;
        }
    }
}

UI Layer Code
   class Program
    {
        static void Main(string[] args)
        {
            Calc obj = new Calc();
            Console.WriteLine( obj.Add(10, 12));
            Console.WriteLine(obj.Sub(10, 12));
            Console.WriteLine(obj.Mul(10, 12));
            Console.Read();
        }
    }
Problem:
     If I've changed Business layer function name(Add() to AddNumber(), I should be change UI Layer also.

Loosely Coupled:

What is Delegate?
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates.
You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:
public delegate int CalculationHandler(int x, int y);

Loosely Coupled Code

 namespace BusinessLayer
{
    public class Calc
    {
        public delegate int CalculationHandler(int x, int y);
        public CalculationHandler Calculator(int Choice)
        {
            CalculationHandler objCalc = null;
            if (Choice == 1)
                objCalc += Add;
            else if (Choice == 2)
                objCalc += Sub;
            else if (Choice == 3)
                objCalc += Mul;
            return objCalc;
        }


        private int Add(int a, int b)
        {
            return a + b;
        }

        private int Sub(int a, int b)
        {
            return a - b;
        }

        private int Mul(int a, int b)
        {
            return a * b;
        }
    }
}

UI Layer:
class Program
    {
        static void Main(string[] args)
        {
            Calc obj = new Calc();
            Console.WriteLine( obj.Calculator(1).Invoke(12,2));
            Console.WriteLine(obj.Calculator(2).Invoke(10, 12));
            Console.WriteLine(obj.Calculator(3).Invoke(10, 12));
            Console.Read();
        }
    }

If I've changed Business layer function name(Add() to AddNumber()), Here no need to change UI Layer. So it is calling Loosely Coupled.


Monday 7 April 2014

Top in SQL SERVER 2008

The TOP keyword allows you to return the first n number of rows from a query based on the number of rows or percentage of rows that you define.
The first rows returned are also impacted by how your query is ordered.
In previous versions of SQL Server, developers used SET ROWCOUNT to limit rows returned or impacted.

We can use the top keyword with the following queries:
Select
Insert
Delete
Update
Basically, most of us know about top, we can use it in a select query to get the top element from the query.

But top can also be used in Insert, Delete and Update commands for the table as well.

We will see how we can use top with Insert, Delete and Update.



Two sample tables



1 Top with select statement

Syntax: - SELECT TOP 10 * FROM <TNAME>

Example:- 


SELECT top(1) Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable]



2 Top with Insert Statement

It is when we have to insert only the top few records that we can use this:

Syntax:-
 

Insert top (10) into <TName> (column1, column2.....)
Select column1, column2..... From <TName1> 
or 
Insert into <TName> (column1, column2.....)
Select TOP (10) column1, column2..... From <TName1> 

Example:- 

INSERT TOP (2) INTO [dbo].[SampleTable1]
SELECT Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable]

ORDER BY Class DESC


SELECT Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable]


SELECT Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable1]


 Top with Delete Statement

Deleting top records from a table:

Syntax: - Delete top (10) from <TName> where <Cond>

Example:- 


SELECT  Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable]
DELETE Top(1) FROM [SampleDB].[dbo].[SampleTable]
SELECT  Name,Class,Age,Address,Phone,Pin FROM [dbo].[SampleTable]



Top with Update statement

Updating top records from a table:

Syntax: - Update top (10) <TName> set Colmn1=<value> 

Example: - 


UPDATE TOP (1) [dbo].[SampleTable] SET Class='Hindi' WHERE Class='Msc'