Sunday, June 26, 2011

.Net Interview Questions - OOP

What is Object Oriented Programming (OOP)?
Object-oriented programming (OOP) is a programming language model organized around objects. At this poing you may have a question in your mind, what is an object? its simple, an object can be anything which has some attributes and hehaviour. For example, hope now you are infront of a computer monitor, the monitor itself is an object, it has a lot of attributes like Make, Size, Type etc...

In OOP we use classes and objects to represent our data and logic. For example consider the following C# example.

public class Monitor
{
public string Make
{
set;
get;
}

public int Height
{
set;
get;
}

//etc......

}
The above class defines a structure of the computer monitor. An object of this class can represent a computer monitor.
OOP Concepts


  1. Data Abstraction : The act of representing essential features without showing the back ground details.

  2. Data Encapsulation: The wrapping up of data together into a single unit.

  3. Inheritance: The act of inheriting properties of an object into another.

  4. Polymorphism: The ability of a data to behave in multiple forms.

Possible questions about OOP concepts from a .NET stand point are:



  1. What is Data Abstraction? ans: see above definition

  2. What is Encapsulation? ans: see above definition

  3. What is inheritance? ans: see above definition

  4. What is Polymorphism? ans: see above definition

  5. Does .Net support multiple inheritance? ans: no

  6. Example for polymorphism? ans:function overloading, overriding and operator overloading

  7. Real world example for data abstraction? ans: when we press the start switch of a car or bike will get started, we might not need to know the internal process. we gave an input and we got an outcome.

  8. What is a class and an object? ans: class is a blueprint for creating objects. An object is an instance of a class and it have the properties and behaviours defined in the class.

Namespace in C# and its Advantages

Namespaces are a way of grouping type names and reducing the chance of name collisions. A namespace can contain both other namespaces and types. The namespace keyword is used to declare a scope.

We can organize our types based on their bahaviour and functionalities. For example consider the following example. Suppose you have a collection of songs, how will you store it in your computer? normally one do this by creating root folder named "Songs" then create sub folders based on song types, then create subfolders for albums etc....

Have a look on the following C# example to understand the advantages of the namespace.

namespace Songs
{
public class Songs
{
public void PlayAll()
{
Console.WriteLine("Playing all songs..");
}
}
namespace English
{
public class Titanic
{
public void Play()
{
Console.WriteLine("Playing titanic..");
}
}
}
namespace Malayalam
{
public class Urumi
{
public void Play()
{
Console.WriteLine("Playing urumi.....");
}
}
}
}

Thursday, June 23, 2011

SQL Server Views Tutorial - Create, Alter, Insert, Update, Delete etc..

SQL Server Views
Views allow you to create a virtual representation of table data defined by a SELECT statement.


Views can be used to simplify data access for query writers, obscuring the underlying complexity of the SELECT statement.


Views are also useful for managing security and protecting sensitive data. If you wish to restrict direct table access by the end user, you can grant permissions exclusively to views, rather than to the underlying tables. You can also use views to expose only those columns that you wish the end user to see, including just the necessary columns in the view definition.


Views work as an interface between database tables and user.


You cannot use certain SELECT elements in a view definition, including INTO, OPTION, COMPUTE, COMPUTE BY or ORDER BY. We cannot reference a temporary table.


Views can be used for different purposes:



  • To restrict the use of particular columns and/or rows of tables.

  • To hide the details of complicated queries. If database applications need queries that involve complicated join operations, the creation of corresponding views can simplify the use of such queries

CREATE VIEW Statement:
A view is created using the CREATE VIEW statement.

/*
create a view for viewing data within the table
Teachers for the user student
*/
CREATE VIEW Student.vu_SelectTeachers
AS
SELECT Name,JobTitle FROM Teacher.Teachers
GO

/*
select data by using the view
*/
SELECT * FROM Student.vu_SelectTeachers
Eg:
/*
create a view for the user teacher
*/
CREATE VIEW Teacher.vu_SelectTeachers
AS
SELECT Id, Name, JobTitle from Teacher.Teachers
GO

/*
select data
*/
SELECT * FROM Teacher.vu_SelectTeachers


Altering and Removing Views
ALTER VIEW statement is used to modify the definition of the view query. The DROP VIEW statement removes the definition of the specified view from the system tables.
Eg:


/*
Altering views
*/
ALTER VIEW Student.vu_SelectTeachers
AS
SELECT Name,JobTitle,Phone from Teacher.Teachers
GO
Eg:
/*
remove a view
*/
DROP VIEW Student.vu_SelectTeachers


INSERT, UPDATE and DELETE statement and VIEW
A view can be used with the INSERT, UPDATE and DELETE statements. When a view is used to insert, update or delete, the rows are actually insert, update or delete into the underlying base table.
The inserting, updating and deleting of rows into the underlying tables is not possible if the corresponding view contains any of the following features:



  • The FROM clause in the view definition involves two or more tables and the column list includes columns from more than one table

  • A column of the view is derived from an aggregate function

  • The SELECT statement in the view contains the GROUP BY clause or the DISTINCT option

  • A column of the view is derived from a constant or an expression (except in the case of deletion)


/*
INSERT, UPDATE and DELETE with Views
*/
INSERT INTO Teacher.vu_SelectTeachers(Id, Name,JobTitle) VALUES(4,'jobin','Faculty')

UPDATE Teacher.vu_SelectTeachers SET JobTitle='Trainer' WHERE Id=4

DELETE Teacher.vu_SelectTeachers WHERE Id=4



WITH Encryption:
The WITH ENCRYPTION option encrypts the SELECT statement, thus enhancing the security of the database system. Software vendors who use SQL Server in the back end often encrypt their views or stored procedures in order to prevent tampering or reverse-engineering from clients or competitors. If you use encryption, be sure to save the original, unencrypted definition.

Eg:
/*
creating a view with Encryption
*/
CREATE VIEW Teacher.vu_SelectTeachers
WITH ENCRYPTION
AS
SELECT Id, Name, JobTitle from Teacher.Teachers
GO


WITH SCHEMABINDING
The SCHEMABINDING clause binds the view to the schema of the underlying table. Creating a view with the SCHEMABINDING option locks the tables being referred by the view and prevents any changes that may change the table schema.

Notice two important points while creating a view with SCHEMABINDING OPTION:


  • The objects should be referred to by their owner names [two part name].

  • SELECT * is not permitted.

/*
create schema with SCHEMABINDING
*/
DROP VIEW Teacher.vu_SelectTeachers

CREATE VIEW Teacher.vu_SelectTeachers
WITH SCHEMABINDING
AS
SELECT Id, Name, JobTitle from Teacher.Teachers
GO

/*
try to change schema of tale Teacher
*/
alter schema student TRANSFER Teacher.Teachers
alter schema teacher TRANSFER student.Teachers


WITH CHECK OPTION
The option WITH CHECK OPTION is used to restrict the insertion of only such rows that satisfy the conditions of the query. If this option is used, Database Engine tests every inserted row to ensure that the conditions in the WHERE clause are evaluated to true.

/*
create schema with CHECK OPTION
*/
DROP VIEW Teacher.vu_SelectTeachers

CREATE VIEW Teacher.vu_SelectTeachers
AS
SELECT Id, Name, JobTitle FROM Teacher.Teachers WHERE JobTitle='faculty'
WITH CHECK OPTION
GO

-- insertion fails on the following query because not faculty
INSERT INTO Teacher.vu_SelectTeachers(Id, Name, JobTitle) VALUES (5,'fff','Manager')
--insertion succeed on the following query because his JobTitle is faculty
INSERT INTO Teacher.vu_SelectTeachers(Id, Name, JobTitle) VALUES (5,'fff','faculty')

Monday, June 20, 2011

Styling Asp.Net Menu control with CSS - Example

Hello friends, here is an example to styling asp.net menu control by using a simple css and a background image. Hope it will help you

To create this stylish menu follow the following steps.

1. Create Menu.css file and type the following css code.

.Menu
{

}

.Menu ul
{
background:#7795BD;
}

.Menu ul li
{
background:#7795BD url(menu_bg.gif) repeat-x;
text-align:center;
/* set width if needed.*/
width:200px;
}

.Menu ul li a
{
color: black;
padding: 4px 2px 4px;
padding-left:8px !important;
border:1px solid #648ABD;
border-bottom: 0;
}

.Menu ul li a:hover
{
background-image: none;
}

.Menu ul li a:hover
{
color: White;
}

.Menu ul li a
{
color: Black;
}



2. add the following image into the web application.



3. place menu control and set the CssClass property as given below

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" CssClass="Menu">

<Items>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item"></asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item">
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
<asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
</Items>

</asp:Menu>

Tuesday, June 14, 2011

Hosting a WCF Service in a Managed Application

WCF enables you to host your service through any managed application. This means you can use a Console application, a Windows service, a Windows Forms application, or even an application built with Windows Presentation Foundation.

Hosting a WCF Service by Using a Console Application

The Console application must specifically create and open an instance of the ServiceHost object. The ServiceHost then remains open and available until it is no longer needed.

Eg:
• Create a New Project from the File menu and select WcfServiceLibrary.
Create and Implement service.
Build the project.
• Create ConsoleApplication named ConsoleServiceHost. From the project menu select Add Reference option and select the WcfServiceLibrary1.dll. Click ok.
Write the following code in the Main function.


ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
host.Open();
Console.WriteLine("Service Started...");
Console.WriteLine("\nPress any key to stop...");
Console.ReadLine();
host.Close();

Add an Application Configuration file to the project. And write the following configuration settings in the app.config.
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceLibrary1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

• Create a new console application. Copy the address from the app.config of ConsoleServiceHost application. Add service reference. Write the following code in the main function.
Console.WriteLine("Press ENTER when the service has started");
Console.Read();

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

Console.WriteLine(client.GetData(33));
client.Close();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
Console.ReadLine();

Monday, June 13, 2011

Hosting a WCF Service on a Web Server

Hosting a Service on an IIS Web Server
Hosting a service on an IIS Web server is very similar to hosting a traditional Web service. Services exposed this way will be highly available and scalable, but they will require the installation and configuration of an IIS Web server.

To register WCF in IIS Run the following command.

C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation>ServiceModelReg.exe -i.

You can host a WCF Service in two different ways.

1. Create WCF service in HTTP mode.
When creating a WCF Service in HTTP Mode it automatically hosted in IIS webserver. To create a service in HTTP Mode, your system must have IIS get installed.




2. Manually host the service in IIS like websites.
You can also do hosting a service by creating a virtual directory in IIS and paste you service files into your virtual directory.