Monday, June 27, 2011

What is the difference between hiding and overriding for casting related matters?

When you use virtual+new, the method definition of base class is hided by the definition provided in child class (called method hiding) . In this case, method of the class, whose object is created, will be called.
class Program
    {
        static void Main(string[] args)
        {
            Base obj1 = new Base();
            Child obj2 = new Child();
            Base obj3 = obj2 as Child;
            obj1.foo();
            obj2.foo();
            obj3.foo();  //Will give output Base
        }
    }

    public class Base
    {
        public virtual void foo()
        {
            Console.WriteLine("Base");
        }
    }

    public class Child : Base
    {
        public new void foo()
        {
            Console.WriteLine("Child");
        }
    }


In second case, where you want to use virtual+override, Thats method overriding. In this case child class function will be called when you create object using casting.
class Program
    {
        static void Main(string[] args)
        {
            Base obj1 = new Base();
            Child obj2 = new Child();
            Base obj3 = obj2 as Child;
            obj1.foo();
            obj2.foo();
            obj3.foo();  // output will be "Child"
        }
    }

    public class Base
    {
        public virtual void foo()
        {
            Console.WriteLine("Base");
        }
    }

    public class Child : Base
    {
        public override void foo()
        {
            Console.WriteLine("Child");
        }
    }


You can also combine both method overriding and hiding
Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:
            class A
            {
                public void Foo() {}
            }

            class B : A
            {
                public virtual new void Foo() {}
            }
     
A class C can now declare a method Foo() that either overrides or hides Foo() from class B:
            class C : B
            {
                public override void Foo() {}
                // or
                public new void Foo() {}
            }

Why does TextBox retains its value after multiple postbacks even if we set the EnableViewState property of the TextBox to False?

The reason being is ASP.NET uses this primitive to update the control's value. ASP.NET uses IPostBackDataHandler  for the controls that load the data from the form collection.
Actually all the controls which implement IPostbackdatahandler, implement the method LoadPostData and
RaisePostDataChangedEvent. But here the key method is LoadPostData, which returns true if the posted value is changed from earlier value and updates it with posted value, else it returns false.

As from the Page Life Cycle, we can see LoadPostData is called after the LoadViewState, whether viewstate is on or not,  it gets populated from the posted data. That's why the data get persisted even if viewstate is set to off for few controls. Following is the complete list of the controls, those implement IPostBackDataHandler.
  •  CheckBox
  •  CheckBoxList
  •  DropDownList
  •  HtmlInputCheckBox
  •  HtmlInputFile
  •  HtmlInputHidden
  •  HtmlInputImage
  •  HtmlInputRadioButton
  •  HtmlInputText
  •  HtmlSelect
  •  HtmlTextArea
  •  ImageButton
  •  ListBox
  •  RadioButtonList
  •  TextBox

Tuesday, June 21, 2011

Can we overload methods in WCF Service or Web Service?

Yes for a WCF Service use the Name property of OperationContractAttribute class
example:
[ServiceContract]
interface ddd
{
[OperationContract(Name = "one")]
int calc(int a,int b);

[OperationContract(Name = "two")]
double calc(double a,double b);
}

1)For a Web Service use the MessageName property of WebMethodAttribute class
2)Please comment the following line in the .cs file of the Web Service
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebMethod]
public string HelloWorld(string a) {
return "Hello"+" "+a;
}
[WebMethod(MessageName="second")]
public string HelloWorld()
{
return "Hello second";
}

What does a Windows Communication Foundation, or WCF, service application use to present exception information to clients by default?

However, the service cannot return a .NET exception to the client. The WCF service and the client communicate by passing SOAP messages. If an exception occurs, the WCF runtime serializes the exception into XML and passes that to the client.

What is the use of Is Required Property in Data Contracts?

Data Contracts, is used to define Required or NonRequired data members. It can be done with a property named IsRequired on DataMember attribute.


[DataContract]
public class test
{
      [DataMember(IsRequired=true)]
      public string NameIsMust;

      [DataMember(IsRequired=false)]
      public string Phone;          
}

Wednesday, June 15, 2011

Delete Duplicate Recods from a Table in SQL Server 2005

CREATE TABLE dbo.Dupes
(
    PK_Column INT IDENTITY(1,1),
    Dupe_String VARCHAR(32)
)



INSERT dbo.Dupes(Dupe_String)
    SELECT 'foo'
    UNION ALL SELECT 'foo'
    UNION ALL SELECT 'foo'
    UNION ALL SELECT 'bar'
    UNION ALL SELECT 'bar'
    UNION ALL SELECT 'splunge'
    UNION ALL SELECT 'splunge'
    UNION ALL SELECT 'mort'

SELECT * FROM dbo.Dupes


DELETE dbo.Dupes
    FROM dbo.Dupes d
    LEFT OUTER JOIN
    (
        SELECT Dupe_String, pk_column = MIN(PK_Column)
        FROM dbo.Dupes
        GROUP BY Dupe_String
    ) x
    ON d.pk_column = x.pk_column
    WHERE x.pk_column IS NULL

What is Event bubling?

The concept of calling parent event from child event.
For example, Let us take a GridView Control. GridView control consists of several events like row edit,update. We can edit a particular row of a gridview control by raising row edit event. The reslt of that row edit event effects to the gridview control data. Hence this concept is called Event Bubbling