HomeASP.NET Developing ASP.NET 2.0 Applications with t...
Developing ASP.NET 2.0 Applications with the Microsoft Data Access Application Block
This is the first article in a series focusing on developing applications using ASP.NET 2.0 and the Data Access Application Block available in the Microsoft Enterprise Application Block Library for .NET 2.0. In this article, I introduce you to the concept of application blocks and develop a simple application using a data access application block with ASP.NET 2.0
A downloadable file for this article is available here.
The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition together with Microsoft SQL Server 2005 Developer Edition and Microsoft Enterprise Library for .NET Framework 2.0 (January 2006 version). I didn’t really test the solution with any other/previous editions. If you have any problems in executing the solution, please post in the discussion area.
What exactly are application blocks?
A simple definition for an application block would be "a collection of reusable, configurable and extensible units of code for application development." Let us go through a detailed explanation of the above.
Any developer who works with ADO.NET would certainly define his own data access layer (DAL). This DAL would generally work with one or more .NET data providers and provide the necessary interfaces to deal with the data existing in databases. The most commonly used interfacing within DAL would be as follows:
Execute a SQL command.
Return a data table/data set based on the SELECT statement given.
Update datasets.
Execute stored procedures.
Return a row/value based on the select statement given.
Logging.
If you ever develop something like the above, you have already started developing your own "application block!" Before developing an application block, you really need to design it carefully with lots of analysis. You can call your DAL library as a full-fledged application block, if you make it configurable, reusable and extensible.
Once an application block is developed, you can use it for any number of applications regardless of any domain or the type of application. You can even develop your own application blocks concentrating on only web development, smart client development, etc. It all depends on the analysis of its reusability among several applications.
You can develop these types of application blocks together with .NET enterprise services to make them more robust. .NET enterprise services include COM+, remoting, web services, MSMQ (Message Queuing), WMI, Windows services, and so on.
If you are new to developing simple data access layers/helpers, you can refer to my series “Developing a Data Access Layer for Sybase using ADO.NET.” If you are interested in developing data access helpers using COM+, the following contributions of mine may help you:
Extending the definition from the previous section, we can define an Enterprise Library (or Enterprise Application Blocks) as "a collection of application blocks and guidance documents that together provide functionality common to enterprise applications." The library consists of several applications blocks which are very commonly used while developing enterprise scale applications. The entire library is free to download, develop and deploy.
At the time of this writing, Microsoft released the “Enterprise Library for .NET Framework 2.0” (January 2006 version). It has been updated to take advantage of the features available in .NET Framework 2.0 and to work with Visual Studio 2005. If you are working with Visual Studio.NET 2003, you need to work with the "Enterprise Library for .NET Framework 1.1" (July 2005 version).
At the time of this writing, the Enterprise Library for .NET Framework 2.0 contains the following application blocks:
Caching Application Block.
Cryptography Application Block.
Data Access Application Block.
Exception Handling Application Block.
Logging Application Block.
Security Application Block.
We need not use every application block in every application. It all depends on what we need our application to do. Microsoft recommends following its "Practices & Patterns" to develop enterprise scale applications, and gives this enterprise library (along with its source code) as an example.
In this contribution, I mainly focus on working with the Data Access Application Block available in the Enterprise Library For .NET Framework 2.0 together with a simple ASP.NET application.
Please note that you should select the "compile library" option when you install the library. This will lessen future complications in working with the samples given in this contribution.
In this section, I shall develop a small ASP.NET application which works with the Data Access Application Block existing in the Enterprise Library for .NET Framework 2.0. Let us proceed through the following steps.
Open Microsoft Visual Studio 2005.
Go to File -> New -> Web Site.
Select “ASP.NET Web site” as the template and provide your own location. In this scenario, I selected "FileSystem" for the "Location" and named the solution "SampleWebSite."
Within the solution explorer, right click on the project (SampleWebSite) and select "Add Reference" (Fig 01).
Within the "Add Reference" dialog, select the "Browse" tab. Go to the "bin" folder of the Microsoft Enterprise Library January 2006 which you installed. (Fig 02)
Select "Microsoft.Practices.EnterpriseLibrary.Common.dll" and "Microsoft.Practices.EnterpriseLibrary.Data.dll" and click "OK." This should add three "dll" files and three "xml" files to your "bin" folder. (Fig 03)
In general, we must embed a database connection string within the "web.config" file belonging to the solution. We will do that here as well, but we do it using the Enterprise Library Configuration.
Once you have completed all the steps in the previous section, work through the following steps to configure the database connection string using the Enterprise Library Configuration:
Within the solution explorer, right click on "web.config" and go to the "open with" option. You will be presented with the "Open With" dialog box.
Within the same dialog box, click on "Add" and provide the "Program Name" of "D:Program FilesMicrosoft Enterprise Library January 2006binEntLibConfig.exe" (please modify it according to your installation).
Provide a Friendly name of "Enterprise Library Configuration Editor." (Fig 04)
Click "OK" twice. It will automatically open the Enterprise Library Configuration window.
If you can already see the Data Access Application Block, you need not create another one. However, if you don’t see it, you need to create it by simply right clicking on the path and selecting New -> Data Access Application Block.
Now, right click on "Connection Strings" and go to New -> Connection String. (Fig 05)
Provide the name of the connection string, "AdventureWorks." (Fig 06)
Provide the name of the database, "AdventureWorks." (Fig 07)
Similarly, provide the name of server and integrated security. If you want to use SQL Server security, you can right click on the connection string AdventureWorks, go to new -> connection string parameter and provide any number of other connection string parameters as required.
Once the connection string is configured, click on save and close the Enterprise Library Configuration window.
Once the above steps are completed, you should see something like the following in your web.config file.
Up until now, we configured the enterprise library to work with our application. Now, it is time to write some code and access the data in the database. To make this demonstration simple, I am going to work with the GridView control. Let us walk through the steps now:
Drag the GridView control from the toolbox and drop it within a "div" pair. The code should look something like the following:
Now, go to the code window (by pressing F7) and modify the code in such a way that it looks like the following:
ImportsSystem.Data
ImportsMicrosoft.Practices.EnterpriseLibrary.Data
PartialClass _Default
Inherits System.Web.UI.Page
ProtectedSub form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) Handles form1.Load
Dim db As Database
db = DatabaseFactory.CreateDatabase("AdventureWorks")
Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text, "select * from HumanResources.department").Tables(0)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
EndSub
EndClass
You must note that in the above code, I am importing the enterprise library at the top. The statements containing words like "database," "databasefactory," "createdatabase" and "executedataset" are nothing but the existing classes/routines available in the enterprise library. These are not at all related to ADO.NET. You can simply consider them to be user-defined classes/routines within the enterprise library. For a full list of classes/members, I suggest you to go through the documentation related to the enterprise library.
Finally press F5 to execute the application and to give you the list of all departments existing in the AdventureWorks database.
In my upcoming contributions, we shall look into the most used routines in the Data Access Application Block (along with accessing stored procedures). Don’t forget to check back or sign up for a newsletter to notify you!
Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.