The blog has moved to the new site F10Debug.com

Wednesday, August 26, 2015

How to set a default value for an existing column

How to set a default value for an existing column



Syntax: 
ALTER TABLE [TableName] ADD CONSTRAINT [ContraintName] DEFAULT 'Value' FOR [ColumnName];
Example:
ALTER TABLE EmpMaster ADD CONSTRAINT Test DEFAULT 'Pune' FOR City;

alter column with default value in sql server

Alter column with default value in sql server Or 

Alter column with default constraint in sql server

Syntax: 
ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL| NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
[WITH VALUES]
Example:
ALTER TABLE EmployeeMaster
ADD City NOT NULL
CONSTRAINT EmpMaster_City DEFAULT 'Pune'

Tuesday, August 25, 2015

how to get xml node value in sql server 2008

How to get XML node value in SQL server 2008


If we have an xml data into sql server and want to retrieve xml Node value as below,

XML Data into SQL Server Column as below,

<Employee>
         <EmployeeNumber>834401</EmployeeNumber>
         <Department>Insurance</Department>
</Employee>

Retrieve value as below,

SELECT 
[xmlColumn].value('(/Employee//EmployeeNumber/node())[1]','nvarchar(max)')as EmployeeNumber,

[xmlColumn].value('(/Employee//Department/node())[1]', 'nvarchar(max)') 
as Department
 
FROM [TableName]

Here [xmlColumn] = “Name of xml column of table”


And [TableName] = “Name of table”

Sunday, August 16, 2015

string.IsNullOrEmpty C#

C# string.IsNullOrEmpty Method

String.IsNullOrEmpty method checks for string is null or empty.  If string is null or empty, it returns true else it returns false.

Example:

 Here we are going to check example on string with null value, empty string and normal string having some data.

using System;

class Program
{
    static void Main()
    {
 // 1 - string is Null
 string sFirstName = null;
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }

 // 2. string is empty
 string sFirstName = "";
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }

 // 3. string - having some value
 string sFirstName = "Jatin Nahar";
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }
    }
}

Output as below:

1) First Name is Null or empty
2) First Name is Null or empty
3) First Name is having some value

Sunday, May 3, 2015

Interview Questions on Inheritance in C#.Net

Interview Questions Related to Inheritance


1) What is inheritance hierarchy?     

In Inheritance, The class which derives fundamental functionality from base class (Parent class) is called as derived class (Child class). This derived class can also be act like base class for another class. There for, it is possible to create tree-like structure that illustrates the relationship between all related classes. This structure is known as the inheritance hierarchy.

For E.g.:


Inheritance Hierarchy


In above example Class A is parent class for class B & C (Derived classes), Also class B is parent class for class D & E and also same time it is child class of class A. this is call as Inheritance Hierarchy.

2) Can we have multiple inheritances in .NET?

.NET supports only single inheritance. But multiple inheritance can be achieved using multiple interfaces.


3) Why don’t we have multiple inheritance in .NET?
Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C?

Diamond Problem 


4) How do you prevent a class from inherited in C#.Net? 
  Sealed keyword is used to prevent class from being inherited.  

  Some important points related to Sealed Class as follows,
        a)      Sealed class cannot be used as abstract class or base class. 
// trying to use sealed class as abstract class
Sealed abstract Class SealedClass // error will occur, you can’t declare like this
        
       b)      You can create instance of sealed class to access its members & properties.
// Sealed class
Sealed Class SealedClass
{
   Public int a,
   Public string b
}
// you can create instance of above class as follows,
SealedClass Obj = new SealedClass();

         c)  You cannot derived a class form Sealed class    
       Class SealedClass: ClassA 

  5) If we inherit the class does the private variables also inherited?
       Yes

Note: Guys please comment, if this blog help you. 
Also if you come across any question's which are not covered or ask somebody you in       interview, so please feel free to ask.

Saturday, May 2, 2015

Advantage of abstract class over Interface in C#.Net

Advantage of abstract class over interface in C# is, If you want to have additional functionality for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.

For e.g: Suppose we have interface ITest having DoTask() method, and both ClassA & Class B is derived from ITest interface and implementing DoTask method of Interface.

interface ITest
    {
        void DoTask();
    }

    public class ClassA : ITest
    {
        public void DoTask()
        {
            int a;
        }
    }
    
    public class ClassB : ITest
    {
        public void DoTask()
        {
            int a;
        }
    }

Code will work fine.

Now, suppose if we add one more method (DOWork) into interface ITest, then all the child classes (A & B) which are derived from ITest interface must implement new method, otherwise it will give following error,

Advantage of abstract class over Interface

Now check same example with abstract class, it will not give error like above,

//abstract class with abstract method, and its implementation in both derived classes.
    abstract class AbsClass
    {
        void DoTask();
    }

    public class ClassA : AbsClass
    {
        public void DoTask()
        {
            int a;
        }
    }
    
    public class ClassB : AbsClass
    {
        public void DoTask()
        {
            int a;
        }
    }

Now If we add default method (method with default implementation) in abstract class DoWork(), then no need to add method implementation to its all derived classes.

public abstract class AbsClass
    {
        // abstract method
        public abstract void DoTask();

        //Non- abstract method
        void DoWork()
        {
            int a;
        }
    }

    public class ClassA : AbsClass
    {
        public override void DoTask()
        {
            int a;
        }
    }

    public class ClassB : AbsClass
    {
        public override void DoTask()
        {
            int a;
        }
    }

In short Advantage of abstract class over interface is we can easily add new default implemented method to existing abstract class and no need to implement that method in all the child classes.
But if we add new method to existing interface then we need to implement that method in every derived classes.

Abstract Class Vs Interface in C#.Net


Abstract Class Vs Interface in C#.Net

Memebers Allowed

Abstract as well as non abstact allowed.

Only abstract members are Allowed.

Implementation of methods in derived classes

Not necessary to implement all the methods of base (abstract) class for derived classes.

The derived class must implement all the methods of interface.

Fields

Can be defined.

Can not be defined.

Access Modifier

Can contain access modifiers for the functions, properties.

Cannot have access modifiers for the functions, properties all members of interface is assumed as public.

Advantage of Interface over Abstract class
A class may inherit only one abstract class. A class may inherit several interfaces.

Advantage of Abstract class over Interface
Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

Now explanation of above points as below,

Members Allowed & Fields

Abstract class with abstract as well as non-abstract members as below,



Interface allows only abstract members (i.e. methods with declaration only not implementation) as below,


Access Modifier and Implementation of methods in derived classes

                                                                                   Interface implementation in Derived classes




Abstract Class implementation in derived classes