The development tools of the .NET Framework enable you to create different types of applications using .NET compliant languages. The types of applications that you can create in the .NET Framework are:
Windows applications: Run only under Windows environment. These applications
ASP.NET Web applications: Run on a distributed environment, such as the Web. These applications use HTTP as the communication protocol and deliver information to the end users in an HTML format. ASP.NET Web applications can range from simple Web sites that consist of HTML pages to advanced enterprise applications that run on local and remote networks. These enterprise applications also provide
ASP.NET Web services: Are small pieces of code that are modular, self-contained, and reusable. Web services allow exchange of data between different end users on the Web, regardless of the language, platform, or component model they use. Web services use a standard set of five Web protocols: XML, HTML, Simple Object Access Protocol (SOAP), Web Service Description Language (WSDL), and Universal Description, Discovery and Integration (UDDI). You use XML to structure the data, HTTP to transport the data, and SOAP to define the rules of communication between Web services. WSDL allows you to describe the Web services and UDDI helps publish the available Web services.
Windows services: Are long-running executable applications that run on the system as a background process. These applications do not interfere with the working of the other processes that run on the same computer. Windows services execute within separate Windows sessions created
Console applications: Are software programs that run on the console or the input/output terminal of the end user’s computer. A console application responds to end user actions on the keyboard and generates a response on the screen. These applications run in a text-only window and in a character-based mode.
Mobile applications: Can run on multiple mobile devices, such as Pocket PCs, mobile phones, or personal digital assistants. These applications provide ubiquitous access to data from mobile devices. For example, an application may return the local time depending upon the current location of the mobile device. The .NET Framework automatically makes changes to these applications to enable them to run on multiple browsers, depending on the mobile device.
Class libraries: Are components that you create once and reuse a number of times in multiple applications. Class libraries allow you to define several classes, along with their
All types of .NET applications use one or more .NET compliant languages for their design and development. The .NET Framework includes various technologies, such as ASP.NET, VB.NET, VC++.NET, and ADO.NET. You use ASP.NET to build Web applications and services, VB.NET and VC++.NET to create Windows applications, and ADO.NET for flexible access to databases.
ASP.NET helps develop server-side Web applications and Web services that are robust, scalable, interoperable, and efficient. ASP.NET uses several features of the .NET Framework, such as compiled code, server side controls, data-bound controls, and a collection of other objects that are accessible by all CLS-compliant languages, such as VB.NET, C#, or J#.
ASP.NET eliminates the use of scripts and
The extension for an ASP.NET page is .aspx. ASP.NET pages are not interpreted; they are compiled to .NET assemblies. The entire ASP.NET application, which might include several pages and custom controls, compiles to one executable assembly at the time of compilation.
An ASP.NET application runs within the CLR, and has access to every object and service the CLR provides. The features the CLR provides to an ASP.NET application are:
Cross language integration
Structured error handling
For example, suppose, you want to access a database to retrieve information about the
Listing 1-3: Error handling using VBScript
<% On Error GOTO
errorhandlerSet conn=Server.CreateObject("ADODB.Connection") Set rs=Server.CreateObject("ADODB.Recordset") conn.Open "Provider=SQLOLEDB;UID=sa;PWD=sa;Initial Catalog=CRM" rs. Open"SELECT * FROM Employees, 3, 3" errorhandler: lblError.Caption="Unable to retrieve employee information" %>
The above listing shows how to use the errorhandler statement in VBScript to trap an error and generate an error message.
Listing 1-4 shows a piece of code in ASP.NET that is equivalent to the VBScript code of Listing 1-3:
Listing 1-4: Error handling in ASP.NET
Public Sub someMethod() Dim conn as new SqlConnection("Server=localhost; UID=sa; PWD=sa; Initial Catalog=CRM") Try conn.Open Catch ex as Exception lblError.Text="Unable to connect to the database" Exit Sub End Try Try Dim cmd as new SqlCommand("SELECT * FROM Employees",conn) Dim da as new SqlDataAdapter(cmd) Catch ex as Exception lblError.Text="Unable to retrieve employees information" End Try End Sub
The above listing shows how ASP.NET handles errors
ASP.NET provides various features, such as compiled code,
When a client requests an ASP.NET page for the first time, which is a file with .aspx extension or a Web service with .asmx extension, the CLR compiles the code in that file into the MSIL code and later into the native code. The .NET runtime puts that file in the Global Assembly Cache (GAC) and maintains this cached copy until you restart your Web server or computer. If a client
ASP.NET allows you to use any CLS-compliant language, such as VB.NET, C#, or J#, to develop your Web applications. This is because all these languages eventually compile to the same MSIL code that executes within the .NET run-time environment.
An ASP.NET page can run on multiple devices, such as desktop computers and mobile devices, and multiple browsers, such as Internet Explorer, Netscape, Opera, and America On-Line (AOL). This is because ASP.NET dynamically determines the target browser’s capabilities and makes appropriate adjustments to the Web page to display it in HTML format on the browser. ASP.NET also provides a set of Mobile controls that automatically generate the output of your application in the language appropriate to the target mobile device. Mobile controls automatically generate the output in Wireless Markup Language (WML), HTML, or XML, as required by the requesting device.
ASP.NET provides a set of server-based controls to control and customize your applications. With ASP.NET, you can add a control to the page and support the control with a few lines of code that executes when the end user
Server-aware HTML controls: Are a set of HTML controls that are common to both ASP.NET and other server-based technologies. ASP.NET lets you convert these standard HTML controls to server controls by adding a few attributes. For example, the following code shows how to run an ASP.NET page with a standard HTML text box on the server, by adding the
<input type="text" id="mytext" runat="server"/>
adding the server-aware HTML control to your Web form, you can work with the control in your program by referring to its id. For example, the following lines of code show how to set the value property of the text control, whose id is mytext, when an ASP.NET page loads:
Public Sub Page_Load(Byval sender as Object, Byval e as EventArgs) mytext.value="Welcome to my web application" End Sub
ASP.NET built-in controls: Are similar to HTML controls, except that built-in controls share a common set of attributes. Each control that ASP.NET provides extends the functionality from the System.Web.UI.WebControls.WebControl class. As a result, the ASP.NET built-in controls have a consistent interface. This makes it easy for you to use these controls in your program. For example, the following lines of code show how a text box and a check box in ASP.NET share common attributes, such as id and Text:
<asp:TextBox id="MyText" Text="Welcome to my Web application" runat="server" /> <asp:CheckBox id="MyCheckBox" Text="Unchecked" runat="server" />
controls share similar properties, you can work with different controls in a consistent manner, as shown in the following lines of code:
MyText.Text="Welcome to my home page" MyCheckBox.Text="Checked"
Data-bound controls: Provide the ability to bind data to the controls. You can access a data source and retrieve the data in a DataReader or a DataSet object. You can then bind the retrieved data to a specific control, such as ASP:DataGrid control, which automatically displays the retrieved data in an easy-to-comprehend tabular form, without explicitly coding for the DataGrid control. In addition, you can define the font-styles, background colors, header styles, and other features of the DataGrid control to make the data grid attractive and user friendly.
ASP.NET provides a built-in functionality that simplifies the task of authentication, authorization, and impersonation. Each ASP.NET page or Web service contains a Web.config file within the virtual root directory of the application. This file is written in XML and defines the configuration and security settings for the application. For example, the following XML tags in a Web.config file configure the security settings to deny anonymous requests to the application:
<authentication mode="Windows" /> <authorization> <deny users="?" /> </authorization>
The ? character in the above code represents anonymous requests to the application.
Instead of making changes to the Internet Information Server (IIS) security settings and Windows user accounts, you can simply change the security settings in the Web.config file by editing it using any text editor.
Visual Studio .NET Integrated Development Environment (IDE) allows you to create Web applications with the same ease with which you create Windows-based applications in Visual Basic. In ASP.NET, you can visually design the Web forms by simply dragging the controls from the toolbox to the desired places on the form. The IDE also contains features, such as code completion and
ASP.NET allows you to dynamically update the components compiled in .NET by overwriting the existing DLL with its updated version, without restarting the Web server. The running application automatically detects the change and starts using the new code. In contrast, for other server-side technologies, you need to restart the Web server each time you want to deploy an updated component.
An ASP.NET application runs in a process under the IIS, which delivers the content from a particular virtual directory to the requesting Web browser. An ASP.NET application is just a virtual directory in IIS. The entire source code of your ASP.NET application, including the
Figure 1-3 shows the architecture of an ASP.NET application:
Figure 1-3: An ASP.NET Application
The basic components that
Global.asax file: Is the
Web.config: Controls various aspects of the ASP.NET application, such as security settings, debug settings, or session state management. Web.config is an XML file and is
Web forms: Are the Web pages that constitute a Web application. These pages are equivalent to Windows forms in Windows applications. Each Web form has a user interface and a code-behind file that separates the presentation logic from the business logic. For example, a login page in your Web application will be a combination of the Login.aspx Web form and the Login.aspx.vb file that stores the code behind the various controls in the form.
Controls: Enable you to create user interfaces to allow clients to interact with your Web applications. There are two types of controls, server and customized. While ASP.NET provides server controls, you can also create customized controls. A customized control has a .ascx extension. For example, suppose, you want a set of hyperlinks to appear on each page of your application. Instead of adding them separately on each page, you can create a customized control that contains all
Web services: Are XML based and allow applications to communicate and share data over the Internet on disparate operating systems and programming languages. Web services use SOAP to exchange XML messages with client applications. ASP.NET makes it easy for you to call XML Web services from your application.
.NET Components: Enable .NET applications to reuse existing components. These components are
To learn more about ASP.NET, see the Introducing ASP.NET ReferencePoint.
Visual Basic.NET helps create Windows and Web applications. VB.NET is the
In VB.NET, the Class and End Class pair creates a class. Listing 1-5 shows how to define a class, its member variables, and methods in VB.NET:
Listing 1-5: Class Definition in VB.NET
Public Class MyClass Dim myInteger as Integer=15 Dim myString as String="Hello World" Public Function myMethod() as String Return myString End Function End Class
The above listing defines the MyClass class that contains two member variables, myInteger and myString, and a myMethod() function. In VB.NET, you can initialize the variable at the time of declaration, which was not possible in VB 6.0. In addition, in VB.NET, you can use the Return keyword to return a value from a function, which too was not possible in VB 6.0. To use the functionality of MyClass, you need to create an object of MyClass, as shown in the following code:
Dim objMyClass as new MyClass
This declaration creates an instance of the MyClass class. After instantiating the class, you can use the class object, objMyClass, to invoke the myMethod() function.
In VB.NET, the Property-End Property pair creates a property. The Get method of a property retrieves the property value and the Set method sets the property value. Listing 1-6 shows how to define and use a property in a class in VB.NET:
Listing 1-6: Property Definition in VB.NET
Public Class MyClass Dim myString as String="Hello World" 'Adding Display property to the class Public Property Display() As String Get Return myString End Get Set(ByVal Value As String) myString = Value End Set End Property End Class Public Class TestClass Sub main() Dim objMyClass As new MyClass MsgBox objMyClass.Display objMyClass.Display="Hello Rest of the World" MsgBox objMyClass.Display End Sub End Class
In the above listing, the MyClass class defines a property, Display, along with its methods that return and set the value of the variable, myString. The TestClass class defines the main() function that creates an object of MyClass and retrieves the default value of the Display property of MyClass. The method then sets the value of the Display property to "Hello Rest of the World" and displays the updated value to the end user.
You use inheritance to acquire the features of a class and extend those features to create a specialized class. Listing 1-7 shows how to implement inheritance in VB.NET:
Listing 1-7: Subclassing in VB.NET
Public Class A Public Function methodA() as String Return "Method of Class A" End Function End Class Public Class B inherits A Public Function methodB() as String Return "Method of Class B" End Function End Class Public Class TestClass Public Sub main() Dim objB as new B Console.WriteLine(objB.methodA()) Console.WriteLine(objB.methodB()) End Sub End Class
In the above listing, class B inherits class A to extend the functionality that class A provides. You need to use the inherits keyword to implement inheritance.
In VB.NET, you can declare multiple variables of the same type in one declaration. This feature was not available in VB 6.0. For example, you can declare two integer variables in one declaration, as shown in the following code:
Dim sampleInteger1, sampleInteger2 As Integer
In addition, you can initialize variables at the time of their declaration in VB.NET. This feature was also not available in VB 6.0. For example, you can declare a string variable and initialize it in one step, as shown in the following code:
Dim myName As String = "Ed Smith"
All components of VB.NET language are objects. The built-in data types, such as Integer or String, are also objects. You create an object using the new keyword. For example, you can create a string variable in two ways, as shown in the following code snippets:
Dim myString as String Dim myString as String=new String()
VB.NET eliminates the use of the Set keyword to assign objects to variables. In VB 6.0, you need to use the Set keyword to assign an object to a variable, as shown in the following code:
Set myObject=new MyObject
The code to perform the same task in VB.NET is:
VC++.NET provides an environment to develop different types of applications, such as Windows applications, Web applications, XML Web services, and mobile applications, using C++. VC++.NET includes components, such as Active Template Library (ATL), Microsoft Foundation Class (MFC) libraries, and advanced language extensions, which simplify the task of developing applications in VC++. In addition, VC++.NET is integrated with the Visual Studio.NET IDE that has its own built-in features to facilitate application development. VC++.NET is an object-oriented language that is supported by an optimized compiler. This helps create robust components and applications.
VC++.NET provides several new features and enhancements, which help traditional C++ programmers create applications that target the .NET platform. These features include modifications to the preprocessor, compiler, linker, libraries, and development environment. The enhancements include:
Managed extensions for C++: Are additions to the syntax and keywords of C++. These extensions allow you to create C++ applications that execute in the .NET runtime and use all the services of the CLR, such as memory management and exception handling. Managed extensions enable integration of managed and unmanaged code within the same application. This saves the investment made in developing the existing C++ code.
Platform invoke capabilities: Help invoke unmanaged Application Programming Interfaces (APIs) that are provided by the Win32 DLLs, from managed applications by simply including the header file in your application and linking the application with the import library.
Web services creation facility: Helps create Web services using C++. VC++.NET includes an ATL Server, which is a set of unmanaged C++ classes. You use these classes to create Web applications, Web services, and other applications for the server.
To learn more about managed extensions, see the Introduction to Managed Extensions ReferencePoint.
Visual Studio.NET includes four broad project categories that provide the templates to create different VC++.NET applications:
ATL provides a set of template-based C++ classes that allow you to create Component Object Model (COM) objects. The ATL project type provides two templates to create COM components:
ATL project: Creates a project that provides facilities to include COM objects in your application. You can add objects, controls, and COM+ objects to an ATL project.
ATL server Web service: Provides classes that allow you to create Web services using C++. The classes that support the creation of XML Web services are collectively termed as ATL server.
MFC classes provide tools to create ActiveX controls, database applications, and Windows desktop applications. The MFC project type provides the following templates to create applications that use the MFC classes:
MFC ActiveX control: Generates an MFC control class that serves as an ActiveX control. You can also specify the properties of the control that generates a property page class.
MFC application: Provides a Windows executable .exe application that can have Multiple Document Interface (MDI) and database support. The MFC application includes C++ source (.cpp) files, resource (.rc) files, header (.h) files, and a project (.vcproj) file.
MFC DLL: Creates an MFC DLL application that implements the basic features of a DLL. This application includes C++ source (.cpp) files, resource (.rc) files, and a project (.vcproj) file.
Win32 applications use Win32 APIs to provide the operating system functionality.
You can use the Win32 project type to create Win32 applications, such as a console application, a Windows application, a DLL, or a static library.
Managed extensions for C++ in .NET extend the C++ language to allow you to use C++ in the .NET Framework and target the CLR for code execution. The .NET project type allows you to create various applications, such as console applications, ASP.NET Web service, Class library, Windows Forms applications, and Windows control library, using managed C++ extensions.
ADO.NET helps connect a .NET application to multiple sources of data, such as a database, a text file, an XML file, or a spreadsheet. ADO.NET includes a set of classes under the System.Data namespace that provides
Three key issues you need to examine when you design and develop your application are speed of data access, scalability, and data interoperability. To address all these issues, Microsoft developed ADO.NET, which is a subset of .NET Framework. The benefits of using ADO.NET are:
Leverages the existing knowledge of ADO. ADO.NET ensures smooth and efficient migration from ADO to ADO.NET. The basic constructs in the two languages
Enhances interoperability by supporting XML for data exchange. Because XML encodes data in a text format, any end user can access it from
Makes applications scalable by providing disconnected data access. This means you can open a connection to the database in your application and use it until the data access request completes execution. After that, you can close the connection. In contrast, ADO provides connected data access, where the connection to the database remains active throughout the lifetime of the application. Disconnected data access
The architecture of ADO.NET facilitates interaction with the data sources by separating data access from data manipulation. The object model of ADO.NET consists of two prime components:
Connected data model: Includes the .NET Framework Data Provider, which contains components that connect to the database, directly execute commands against it, and read data from the database. The .NET Framework Data Provider includes four components: Connection, Command, DataReader, and DataAdapter.
Disconnected data model: Includes the DataSet that provides a local cache for disconnected data. This means that you can manipulate the retrieved data in the cached copy, without maintaining an active connection with the database. You establish a connection with the database only when you are ready to update the data to the database. Because DataSets are XML-based, they allow exchange of data between applications.
Figure 1-4 shows the components that make up the architecture of ADO.NET:
Figure 1-4: Architecture of ADO.NET
The components of the .NET Framework Data Provider help establish and maintain a connection with the database. These components also allow you to manipulate data in the database, such as update and delete data, and access the data in a rapid, forward-only, or read-only manner.
You can write the .NET Data Provider for any data source. The .NET Data Providers that are shipped along with the .NET Framework are:
The .NET Framework Data Provider for OLE DB: Works with any source of data other than Microsoft SQL Server 7.0 or later, such as Microsoft Access database or the Oracle database. To use this provider, your application needs to import the System.Data.OleDb namespace that contains all the classes for this provider.
The .NET Framework Data Provider for SQL Server: Works directly with Microsoft SQL Server 7.0 or later. To use the SQL Server .NET Data Provider, your application needs to import the System.Data.SqlClient namespace that contains all the classes for this provider.
The .NET Framework Data Provider includes four elements that provide various functionalities for efficient data access:
Connection object: Is an instance of a class that implements the IDbConnection interface and helps connect to the database.
Command object: Is an instance of a class that implements the IDbCommand interface and helps execute commands to the database across a connection. These commands allow you to retrieve and modify data, execute stored procedures on the database, and send or retrieve information about the different parameters in the database.
DataReader object: Is an instance of a class that implements the DataReader interface. DataReader provides a read-only, forward-only, and connected access to the ADO.NET data. You cannot directly create an instance of the DataReader object in your application. When you execute the ExecuteReader() method of the Command object, this method returns a DataReader object. The main advantage of using DataReader is that it does not buffer the data in the memory. As a result, DataReader reduces the overhead on the system’s performance. One of the drawbacks of using the DataReader is that it provides connected access to the data. This means that you need to have an active connection with the database throughout the lifetime of the DataReader object.
DataAdapter object: Is an instance of a class that derives from the DbDataAdapter class that implements the IDbDataAdapter interface. DataAdapter acts as the intermediary between the data source and the DataSet, by populating the DataSet with the data retrieved from the database. After you finish manipulating the data in the DataSet, you can use the DataAdapter to commit the changes to the database. The DataAdapter object provides a set of commands to the database, which include SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand.
The DataSet object provides a local cache of disconnected data. This means that you do not work with live data on the server at any point of time. Whatever manipulations and updates you make to the data in the dataset are independent of the database. These changes are made to the local cached copy only and can be committed to the database at a later point of time. You can also fill data in the dataset regardless of the data source, such as a database or an XML file. This is because the native form of a dataset is XML. In addition, you can load files directly from XML documents to a dataset, without using a DataAdapter object. As a result, data from multiple and varied sources, such as Microsoft SQL server database or an Oracle database, can be collated in one DataSet object. A DataSet object cannot connect to the database or retrieve data from the database on its own. You need to use a DataAdapter object to fill the DataSet object with the
You can use the DataSet object:
To navigate between multiple tables of results.
To add, edit, and delete rows of data in a disconnected cache.
To manipulate data retrieved from multiple sources. For example, you can use a DataSet object to integrate data from more than one database, from an XML file, and from a spreadsheet.
To exchange data between tiers by using an XML Web service because data in a DataSet can be exchanged remotely.
To reuse a set of rows for various
To perform a large amount of processing for each row. If your application involves
The DataSet object provides a relational view of the data in the database. The main components of a DataSet object are:
DataTableCollection: Includes one or more DataTable objects. Each DataTable object represents one table of the locally cached data. The DataTableCollection includes three collections:
DataRowCollection: Includes DataRow objects, each of which is a pointer to an actual row of data in the database.
DataColumnCollection: Includes DataColumn objects, each of which provides schema information about one column of data.
ConstraintCollection: Includes Constraint objects that allow you to define the constraints between the database entities, such as primary and foreign key constraints. These constraints help maintain the integrity of data in the tables.
DataRelationCollection: Contains objects that help define relationships between different DataTable objects. These relations facilitate navigation between parent and child rows of the retrieved data.
To learn more about ADO.NET, see the Programming with ADO.NET Classes ReferenecePoint.