Thursday, August 16, 2012

SQL SERVER – Introduction to JOINs – Basic of JOINs


The nature of relational database design shows that we will have related data that is stored in different tables. To retrieve data from two or more tables we use JOINS.

The JOIN clause tells the database how the data in the two tables is related so that it can return a correct representation of the related data.

There are different types of join. 

1. Self-Join 
2. Inner-Join 
3. Outer-Join 
   3.1 RightOuterJoin 
   3.2 LeftOuterJoin 
   3.3 FullOuterJoin 
4. Cross join 

Self Join : Select e1.emp_name 'manager',e2.emp_name 'employee' 
from employees e1 join employees e2 on e1.emp_id=e2.emp_manager_id 











Sunday, August 5, 2012

Difference Between ToString() vs Convert.ToString()


ToString() raise exception when the object is null
So in the case of object.ToString(), if object is null, it raise NullReferenceException.
Convert.ToString() return string.Empty in case of null object
(string) cast assign the object in case of null
So in case of
MyObject o = (string)NullObject;
But when you use o to access any property, it will raise NullReferenceException.
Example : int i =0;    MessageBox.Show(i.ToString());        MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
The basic difference between them is “Convert” function handles NULLS while “i.ToString()”
does not it will throw a NULL reference exception error. So as good coding practice using
“convert” is always safe.


Code Sample :

C#
    public String FullName = null;
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            Response.Write("<b>.ToString</b><br>");
            Response.Write("FullName :" + FullName.ToString());
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

        Response.Write("<br><br>");

        try
        {
            Response.Write("<b>Convert.ToString</b><br>");
            Response.Write("FullName :" + Convert.ToString(FullName));
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

                         OutPut:

Saturday, July 28, 2012

Difference between User control & Custom controls


User control

1) Reusability web page
2) We can’t add to toolbox
3) Just drag and drop from solution explorer to page (aspx)
4) U can register user control to. Aspx page by Register tag
5) A separate copy of the control is required in each application
6) Good for static layout
7) Easier to create
8)Not complied into DLL
9) Here page (user page) can be converted as control then
We can use as control in aspx

Custom controls

1) Reusability of control (or extend functionalities of existing control)
2) We can add toolbox
3) Just drag and drop from toolbox
4) U can register user control to. Aspx page by Register tag
5) A single copy of the control is required in each application
6) Good for dynamics layout
7) Hard to create
8) Compiled in to dll

Difference between abstract class and interface


1. Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods.

2. Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interacace is a Interface which starts with interface keyword.

3. Abstract class contains one or more abstract methods. Whereas Interface contains all abstract methods and final declarations.

4. Abstract class contains the method definition of the some methods. But Interface contains only method declaration, no definition provided.

5. Abstract classes are useful in a situation that some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented we can use this scenario

When to Use
If the functionalites(declaration and definition) is same for all classes in the project and that is mandatory that time we use Abstract class.
If the declaration is same but the implementation logic is going  differ in class to class that time we use Interface

Difference between Class and Structure


1)    structure :- In structure have a by default public.
In class have a by default private.
2)    Structure cannot be inherited. But class can be
inherit.
3)    There is no data hiding features comes with
structures. Classes do, private, protected and public.
4)    A structure can't be abstract, a class can.
5)    A structure is a value type, while a class is a
reference type.
6)    A structure is contain only data member , but class
contain data member and member function.
7)    In a Structure we can't initilse the value to the
variable but in class variable we assign the values.
8)    Structure are value type, They are stored as a
stack on memory. where as class are reference type. They
are stored as heap on memory.


note: 'this' pointer will work only in class.

Difference Between Data-set & Data-reader :


Candidate Key, Primary Key, Unique Key :


Candidate Key – A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.

Primary Key – A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.

1)Primary key is nothing but it is uniqly identified each row in Table.
2) Primary key does not Allows Duplicate values and Null values.
3) Primary key is default Clustered indexes
4) one table can have only one Primary key.

Unique Key:
1)Unique Key is nothing but it is uniqly identified each row in Table.
2) Unique Key does not Allows Duplicate values but allows only one Null value.
3) Primary key is default Non- Clustered indexes

Primary key: -
                     1) Can only one in a table
                     2) We can't insert duplicate value or same value
                     3) Can use as a foreigner key for other table
                     4) It never allow Null values.
Unique key: -
                     1) there can be more than one unique key in one table
                     2) Unique key can have Null Values
                     3) It can't be a candidate key