New Mail Window–Just a line of code

Very useful info

Manas Ranjan Dash

In most of the website you might have noticed, the contact us or contact me link at the end. Once you click that it will open a new mail window of your default mail client and it will have the mention subject and a mail id in the to box. So we can achieve that with the below line of code.

   1:<ahref="mailto:simplymanas@gmail.com?Subject=query">Contact Me</a>

 

in HTML5 I wrote this in the footer section in this way.

 

   1:<footer>&copy; simplymanas 2013

   2:<ahref="mailto:simplymanas@gmail.com?Subject=From BR">Contact Me</a>

   3:<ahref="http:\\www.twitter.com\simplymanas">@simplymanas</a>

   4:</footer>

 

and it will appear like this

© simplymanas 2013

Contact Me@simplymanas

 

This article was also published by me in

Beyond Relational

Quote of the Day:

No person was…

View original post 18 more words

Advertisements

Deleting Primary Key Data without Deleting Foreign Key Data

Some times we face this situation where we need to delete only primary key column data and it should not effect the foreign key column data.

Here i have taken some example to elaborate it.

Create table PrimaryKeyTable
(
id int identity (1,1) primary key,
[name] varchar(100)
)

create table ForeignKeyTable
(
id int foreign key ([id]) references PrimaryKeyTable ([id]),
[age] int
)
insert into PrimaryKeyTable values (‘ram’)
insert into PrimaryKeyTable values (‘sam’)
insert into PrimaryKeyTable values (‘hari’)
insert into PrimaryKeyTable values (‘raju’)
insert into PrimaryKeyTable values (‘manju’)

Select * From PrimaryKeyTable

Out put:

id name
———–
1 ram
2 sam
3 hari
5 manju

insert into ForeignKeyTable values (1,25)
insert into ForeignKeyTable values (2,24)
insert into ForeignKeyTable values (3,29)
insert into ForeignKeyTable values (4,20)
insert into ForeignKeyTable values (5,27)

Select * From ForeignKeyTable

Out put

id age
———– ———–
1 25
2 24
3 29
4 20
5 27

ALTER TABLE ForeignKeyTable NOCHECK CONSTRAINT FK__PrimaryKeyTable__id__5E94F66B

Delete FROM PrimaryKeyTable WHERE id = 4

Select * From PrimaryKeyTable

id name
———–
1 ram
2 sam
3 hari
5 manju

Now you can enable the foreign key constraint

ALTER TABLE ForeignKeyTable CHECK CONSTRAINT FK__PrimaryKeyTable__id__5E94F66B

You might have a question that how should i enable and disable all foreign constraint in the database

The below script generate disable

SELECT  'IF EXISTS (SELECT * FROM sys.foreign_keys 
   WHERE object_id = OBJECT_ID(N''[dbo].' + FK +''') 
   AND parent_object_id = OBJECT_ID(N''[dbo].' + PT + ''')) 
   ALTER TABLE ' + PT + ' NOCHECK CONSTRAINT ' + FK + ';'
FROM 
(SELECT 
    OBJECT_NAME(constraint_object_id) as FK,
    OBJECT_NAME(parent_object_id) as PT
    FROM [sys].[foreign_key_columns] ) T
ORDER BY FK

the below script generate enable

SELECT  'ALTER TABLE ' + PT + ' WITH CHECK CHECK CONSTRAINT ' + FK + ';'
FROM 
(SELECT 
    OBJECT_NAME(constraint_object_id) as FK,
    OBJECT_NAME(parent_object_id) as PT
    FROM [sys].[foreign_key_columns] ) T
ORDER BY FK

-- Disable all the constraint in database
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

-- Enable all the constraint in database
EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"

Oops Concepts

                                                                           OOPS Concepts 

Class:
It is a collection of objects.
Object:
 
It is a real time entity.
An object can be considered a “thing” that can perform a set of related activities. The set of activities that the object performs defines the object’s behavior. For example, the hand can grip something or aStudent (object) can give the name or address. In pure OOP terms an object is an instance of a class
 
Encapsulation:
Encapsulation is a process of binding the data members and member functions into a single unit.
Example for encapsulation is class. A class can contain data structures and methods.
Abstraction:
 
Abstraction is a process of hiding the implementation details and displaying the essential features.
How to abstract: – By using Access Specifiers
 
.Net has five access Specifiers
 
Public — Accessible outside the class through object reference.
Private — Accessible inside the class only through member functions.
Protected — Just like private but Accessible in derived classes also through member
functions.
Internal — Visible inside the assembly. Accessible through objects.
Protected Internal — Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
Inheritance:
 
Inheritance is a process of deriving the new class from already existing class
C# is a complete object oriented programming language. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective use of inheritance, you can save lot of time in your programming and also reduce errors, which in turn will increase the quality of work and productivity. A simple example to understand inheritance in C#.
Using System;
Public class BaseClass
{
    Public BaseClass ()
    {
        Console.WriteLine (“From Base Class Constructor”);
    }
    Public void Write ()
    {
        Console.WriteLine (“Write method From Base Class”);
    }
}
Public class ChildClass: BaseClass
{
    Public ChildClass ()
    {
        Console.WriteLine(“From Child Class Constructor”);
    }
    Public static void Main ()
    {
        ChildClass CC = new ChildClass ();
        CC.Write ();
    }
}
In the Main () method in ChildClass we create an instance of childclass. Then we call the write () method. If you observe the ChildClass does not have a write() method in it. This write () method has been inherited from the parent BaseClass.

The output of the above program is

Output:

From  Base Class Constructor
From Child Class Constructor
Write method From Base Class
this output proves that when we create an instance of a child class, the base class constructor will automatically be called before the child class constructor. So in general Base classes are automatically instantiated before derived classes.
C# supports single class inheritance only. What this means is, your class can inherit from only one base class at a time.

In C# Multi-Level inheritance is possible. Code snippet below demonstrates mlti-level inheritance. Class B is derived from Class A. Class C is derived from Class B. So class C, will have access to all members present in both Class A and Class B. As a result of multi-level inheritance Class has access to A_Method(),B_Method() and C_Method().

We can achieve multiple inheritance in C# using interface. like below

Public Class A
{

}

Interface Test

{

}

Public Class B:A,Test
{

}

Method Hiding and Inheritance We will look at an example of how to hide a method in C#. The Parent class has a write () method which is available to the child class. In the child class I have created a new write () method. So, now if I create an instance of child class and call the write () method, the child class write () method will be called. The child class is hiding the base class write () method. This is called method hiding.

If we want to call the parent class write () method, we would have to type cast the child object to Parent type and then call the write () method as shown in the code snippet below.

Using System;
Public class Parent
{
    Public void Write ()
    {
        Console.WriteLine (“Parent Class write method”);
    }
}
Public class Child: Parent
{
    Public new void Write ()
    {
        Console.WriteLine (“Child Class write method”);
    }
    Public static void Main ()
    {
        Child C1 = new Child ();
        C1.Write ();
        //Type caste C1 to be of type Parent and call Write () method
        ((Parent) C1).Write ();
    }
}
Polymorphism:
 
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is one of the fundamental concepts of OOP.
Polymorphism provides following features:
  • It allows you to invoke methods of derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called through the same name.
Polymorphism is of two types:
  1. Compile time polymorphism/Overloading
  2. Runtime polymorphism/Overriding
Compile Time Polymorphism
Compile time polymorphism is method and operators overloading. It is also called early binding.
In method overloading method performs the different task at the different input parameters.
Runtime Time Polymorphism
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.
When overriding a method, you change the behavior of the method for the derived class.  Overloadinga method simply involves having another method with the same prototype.
Caution: Don’t confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.
Method overloading has nothing to do with inheritance or virtual methods.
Following are examples of methods having different overloads:
void area(int side);
void area(int l, int b);
void area(float radius);
Practical example of Method Overloading (Compile Time Polymorphism)

using System;

namespace method_overloading
{
    class Program
    {
        public class Print
        {
            public void display(string name)
            {
                Console.WriteLine (“Your name is : ” + name);
            }
            public void display(int age, float marks)
            {
                Console.WriteLine (“Your age is : ” + age);
                Console.WriteLine (“Your marks are :” + marks);
            }
        }
        static void Main(string[] args)
        {
            Print obj = new Print ();
            obj.display (“George”);
            obj.display (34, 76.50f);
            Console.ReadLine ();
        }
    }
}
Note: In the code if you observe display method is called two times. Display method will work according to the number of parameters and type of parameters.
When and why to use method overloading
Use method overloading in situation where you want a class to be able to do something, but there is more than one possibility for what information is supplied to the method that carries out the task.
You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing.
Method overloading showing many forms.

using System;

namespace method_overloading_polymorphism
{
    Class Program
    {
        Public class Shape
        {
            Public void Area (float r)
            {
                float a = (float)3.14 * r;
                // here we have used function overload with 1 parameter.
                Console.WriteLine (“Area of a circle: {0}”,a);
            }
            Public void Area(float l, float b)
            {
                float x = (float)l* b;
                // here we have used function overload with 2 parameters.
                Console.WriteLine (“Area of a rectangle: {0}”,x);
            }
            public void Area(float a, float b, float c)
            {
                float s = (float)(a*b*c)/2;
                // here we have used function overload with 3 parameters.
                Console.WriteLine (“Area of a circle: {0}”, s);
            }
        }
        Static void Main (string[] args)
        {
            Shape ob = new Shape ();
            ob.Area(2.0f);
            ob.Area(20.0f,30.0f);
            ob.Area(2.0f,3.0f,4.0f);
            Console.ReadLine ();
        }
    }
}
Things to keep in mind while method overloading
If you use overload for method, there are couple of restrictions that the compiler imposes.
The rule is that overloads must be different in their signature, which means the name and the number and type of parameters.
There is no limit to how many overload of a method you can have. You simply declare them in a class, just as if they were different methods that happened to have the same name.
Method Overriding:
 

Whereas Overriding means changing the functionality of a method without changing the signature. We can override a function in base class by creating a similar function in derived class. This is done by usingvirtual/override keywords.

Base class method has to be marked with virtual keyword and we can override it in derived class usingoverride keyword.

Derived class method will completely overrides base class method i.e. when we refer base class object created by casting derived class object a method in derived class will be called.

Example:

// Base class
public class BaseClass
{
public virtual void Method1()
{
Console.Write(“Base Class Method”);
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write(“Derived Class Method”);
}
}
// Using base and derived class
public class Sample
{
public void TestMethod()
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}

Output
———————

Derived Class Method

Derived Class Method
Constructors and Destructors:
 
Classes have complicated internal structures, including data and functions, object initialization and cleanup for classes is much more complicated than it is for simple data structures. Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. Construction may involve memory allocation and initialization for objects. Destruction may involve cleanup and deallocation of memory for objects.
  • Constructors and destructors do not have return types nor can they return values.
  • References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
  • Constructors cannot be declared with the keyword virtual.
  • Constructors and destructors cannot be declared const, or volatile.
  • Unions cannot contain class objects that have constructors or destructors.
Constructors and destructors obey the same access rules as member functions. For example, if you declare a constructor with protected access, only derived classes and friends can use it to create class objects.
The compiler automatically calls constructors when defining class objects and calls destructors when class objects go out of scope. A constructor does not allocate memory for the class object it’s this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator. During cleanup, a destructor may release objects allocated by the corresponding constructor. To release objects, use the delete operator.

Example of Constructor

class C

{
       private int x;
       private int y;
       public C (int i, int j)
       {
                 x = i;
                 y = j;
       }
       public void display ()
       {
               Console.WriteLine(x + “i+” + y);
       }

}

Example of Destructor

class D
{
        public D ()
        {
            // constructor
        }
        ~D ()
        {
           // Destructor
        }
}