Introducing Microsoft Enterprise Library in ASP.Net

Overview

Microsoft provides many libraries for developers for development purposes. The Patterns and Practices Group has the responsibility to develop application blocks that are open-source libraries for solving the usual tasks. Application blocks are designed to reduce the cost of development that provides flexibility for development. It is because the use of application blocks in the project reduce the project development time that would otherwise be required to build the functionality. The application blocks are proven and well-tested because thousands of developers use this for the project development.

The Enterprise Library is very useful for developers developing enterprise-level applications. It provides the reliability, security and better performance for the application development.

Introduction

The Microsoft Enterprise Library is the collection of application blocks that are reusable for the common development cross-cutting concerns such as logging, validation, data access, exception handling and many others.

There are two well-known application blocks named Data Access Application Block (DAAB) and Exception Management Application Block (EMAB). The DAAB provides classes for accessing the data from the SQL Server Database that reduce the boring amount of code that is required. The EMAB provides classes for exception handling. To improve the develoment of common tasks, Microsoft provided the Enterprise Library in January 2005 that includes various application blocks.

There are various types of versions available, given below:

  • Enterprise Library 1.0 (January 2005)
  • Enterprise Library 2.0 (January 2006)
  • Enterprise Library 3.0 (April 2007)
  • Enterprise Library 4.0 (May 2008)
  • Enterprise Library 5.0 (April 2010)
  • Enterprise Library 6.0 (April 2013)

Getting Started

Let's get started and use the Microsoft Enterprise Library in the sample application. This article uses the Enterprise Library in a sample application and we will learn to fetch and insert the data using the Enterprise Library.

So, let's start with the following procedure:

  • Creating Application
  • Creating App Library
  • Adding Enterprise Library
  • Working with Application

Creating Application

In this section we will create the web application using an empty project template in Visual Studio 2013. So begin with the following procedure.

Step 1: Open Visual Studio then select New Project and then select Web Application and name it CollegeTracker.

web application

Step 2: Select the Empty Project Template to create the empty web application project.

empty web application

Step 3: Right-click on the solution and add a new folder named Web.

add a new folder

Step 4: Move the CollegeTracker project into the Web folder.

This is your web project. We will work on it later. Let's move to the next section.

Creating App Library

Now, in this section we will create the class library and add a reference of the Enterprise Library to perform some operation on the database. Begin with the following procedure.

Step 1: Right-click on the solution and add a new folder named Library.

Library

Step 2: Right-click on the folder and add a new project of class library named CollegeTrackerLibrary.

Adding New Project-

Adding Class Library

Note: Delete the default generated class from the library project. Let's move to the next section.

Adding Enterprise Library

In this section we will install the Enterprise Library in the library project from the Nuget Gallery. So, begin with the following procedure.

Step 1: Right-click on References and choose the Manage Nuget Packages and search for the Enterprise Library and install it on the project.

Installing Enterprise Library-

Step 2: Now add two new folders named DAL and Model to the library project.

Step 3: Right-click on the Model folder and add a new class named CollegeDetails.

Adding Class

Step 4: Replace the code with the following code:

  1. namespace CollegeTrackerLibrary.MODEL  
  2. {  
  3.     public class CollegeDetails  
  4.     {  
  5.         public Int16 CollegeID { getset; }  
  6.         public string CollegeName { getset; }  
  7.         public string CollegeAddress { getset; }  
  8.         public Int64 CollegePhone { getset; }  
  9.         public string CollegeEmailID { getset; }  
  10.         public string ContactPerson { getset; }  
  11.         public string State_Name { getset; }  
  12.         public string City_Name { getset; }  
  13.     }  
  14. }  
Step 5: Right-click on the DAL folder and add a new class named CollegeDAL and replace the code with the following code:
  1. using CollegeTrackerLibrary.MODEL;  
  2. using Microsoft.Practices.EnterpriseLibrary.Data;  
  3. using Microsoft.Practices.EnterpriseLibrary.Data.Sql;  
  4. using System;  
  5. using System.Configuration;  
  6. using System.Data;  
  7. using System.Data.Common;  
  8.   
  9. namespace CollegeTrackerLibrary.DAL  
  10. {  
  11.     public class CollegeDAL  
  12.     {  
  13.         #region Variable  
  14.         /// <summary>  
  15.         /// Specify the Database variable  
  16.         /// </summary>  
  17.         Database objDB;  
  18.         /// <summary>  
  19.         /// Specify the static variable  
  20.         /// </summary>  
  21.         static string ConnectionString;  
  22.         #endregion  
  23.  
  24.         #region Constructor  
  25.         /// <summary>  
  26.         /// This constructor is used to get the connectionstring from the config file  
  27.         /// </summary>  
  28.         public CollegeDAL()  
  29.         {  
  30.             ConnectionString = ConfigurationManager.ConnectionStrings["CollegeTrackerConnectionString"].ToString();  
  31.         }  
  32.         #endregion  
  33.  
  34.         #region College  
  35.         /// <summary>  
  36.         /// This method is used to get the college data  
  37.         /// </summary>  
  38.         /// <returns></returns>  
  39.         public DataSet GetCollegeDetails()  
  40.         {  
  41.             objDB = new SqlDatabase(ConnectionString);  
  42.             using (DbCommand objcmd = objDB.GetStoredProcCommand("CT_CollegeDetails_Select"))  
  43.             {  
  44.                 try  
  45.                 {  
  46.                     return objDB.ExecuteDataSet(objcmd);  
  47.                 }  
  48.                 catch (Exception ex)  
  49.                 {  
  50.                     throw ex;  
  51.                     return null;  
  52.                 }  
  53.             }             
  54.         }  
  55.   
  56.         /// <summary>  
  57.         /// This method is used to insert the college data into the database  
  58.         /// </summary>  
  59.         /// <param name="collegeDetails"></param>  
  60.         /// <returns></returns>  
  61.         public bool InsertCollegeDetails(CollegeDetails collegeDetails)  
  62.         {  
  63.             bool result = false;  
  64.             objDB = new SqlDatabase(ConnectionString);  
  65.             using (DbCommand objCMD = objDB.GetStoredProcCommand("CT_CollegeDetails_INSERT"))  
  66.             {  
  67.                 objDB.AddInParameter(objCMD, "@CollegeID", DbType.Int16, 0);  
  68.                 objDB.AddInParameter(objCMD, "@CollegeName", DbType.String, collegeDetails.CollegeName);  
  69.                 objDB.AddInParameter(objCMD, "@CollegeAddress", DbType.String, collegeDetails.CollegeAddress);  
  70.                 objDB.AddInParameter(objCMD, "@CollegePhone", DbType.Int64, collegeDetails.CollegePhone);  
  71.                 objDB.AddInParameter(objCMD, "@CollegeEmailID", DbType.String, collegeDetails.CollegeEmailID);  
  72.                 objDB.AddInParameter(objCMD, "@ContactPerson", DbType.String, collegeDetails.ContactPerson);  
  73.                 objDB.AddInParameter(objCMD, "@State", DbType.String, collegeDetails.State_Name);  
  74.                 objDB.AddInParameter(objCMD, "@City", DbType.String, collegeDetails.City_Name);  
  75.                 objDB.AddOutParameter(objCMD, "@Status", DbType.Int16, 0);  
  76.                 try  
  77.                 {  
  78.                     objDB.ExecuteNonQuery(objCMD);  
  79.                     result = Convert.ToBoolean(objDB.GetParameterValue(objCMD,"@Status"));  
  80.                 }  
  81.                 catch (Exception)  
  82.                 {                      
  83.                     throw;  
  84.                 }  
  85.             }  
  86.             return result;  
  87.         }  
  88.         #endregion          
  89.     }  
  90. }  
Step 6: Build the library project so that the DLL file can created and we can use it in the Web project as a reference.

Working with Application

In this section we will add a web form to retrieve the data from the library. We can use any project template here for displaying and performing database operations. I am using the empty project template to design and represent the data.

So, let's start with the following procedure.

Step 1: Right-click on the web project and go to Add Reference.

Adding Reference

Step 2: Add the library from the solution.

Add Reference from the solution-

Step 3: Right-click on the web project and add a Web Form named CollegeDetailsForm.

Step 4: Add the following code to the webform:
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div>  
  4.             <a href="AddNewCollege.aspx">Add New College</a>  
  5.         </div>  
  6.     <div>  
  7.         <asp:GridView ID="CollegeGridView" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Height="215px" Width="363px">  
  8.             <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />  
  9.             <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />  
  10.             <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />  
  11.             <RowStyle BackColor="#DEDFDE" ForeColor="Black" />  
  12.             <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />  
  13.             <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  14.             <SortedAscendingHeaderStyle BackColor="#594B9C" />  
  15.             <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  16.             <SortedDescendingHeaderStyle BackColor="#33276A" />  
  17.         </asp:GridView>      
  18.     </div>  
  19.     </form>  
  20. </body>  
Step 5: In the code behind paste the following code to access the data from the library:
  1. using CollegeTrackerLibrary.DAL;  
  2. using System;  
  3.   
  4. namespace CollegeTracker  
  5. {  
  6.     public partial class CollegeDetailsForm : System.Web.UI.Page  
  7.     {  
  8.         CollegeDAL objCollege = new CollegeDAL();  
  9.         protected void Page_Load(object sender, EventArgs e)  
  10.         {  
  11.             if (!IsPostBack)  
  12.             {  
  13.                 GetCollegeData();  
  14.             }  
  15.         }  
  16.         public void GetCollegeData()  
  17.         {  
  18.             CollegeGridView.DataSource = objCollege.GetCollegeDetails();  
  19.             CollegeGridView.DataBind();  
  20.         }  
  21.     }  
  22. }  
Step 6: Now, run the application and you can see the data in the GridView.

Displaying College Data

Note: I have formatted the GridView.

Step 7: Now add another webform named AddNewCollege and add the following code to the page:
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>   
  4.     <link href="Content/Site.css" rel="stylesheet" />  
  5.     <script src="Scripts/jquery-2.1.3.js"></script>  
  6.     <script type="text/javascript">  
  7.         $(document).ready(function(){  
  8.             $('#BtnSubmit').click(function () {  
  9.                 var collegeDetails = {};  
  10.                 collegeDetails.CollegeName = $('#TxtCollegeName').val();  
  11.                 collegeDetails.CollegeAddress = $('#TxtCollegeAddress').val();  
  12.                 collegeDetails.CollegePhone = $('#TxtCollegePhone').val();  
  13.                 collegeDetails.CollegeEmailID = $('#TxtCollegeEmailID').val();  
  14.                 collegeDetails.ContactPerson = $('#TxtContactPerson').val();  
  15.                 collegeDetails.State_Name = $('#TxtCollegeState').val();  
  16.                 collegeDetails.City_Name = $('#TxtCollegeCity').val();  
  17.                  
  18.                 $.ajax({  
  19.                     type: 'POST',  
  20.                     url: 'AddNewCollege.aspx/CreateCollegeData',  
  21.                     data: "{collegeDetails:" + JSON.stringify(collegeDetails) + "}",  
  22.                     dataType: 'json',  
  23.                     contentType: 'application/json; charset=utf-8',  
  24.                     success: function (response) {  
  25.                         $('#lblResult').html('Inserted Successfully');  
  26.                         $('#TxtCollegeName').val('');  
  27.                         $('#TxtCollegeAddress').val('');  
  28.                         $('#TxtCollegePhone').val('');  
  29.                         $('#TxtCollegeEmailID').val('');  
  30.                         $('#TxtContactPerson').val('');  
  31.                         $('#TxtCollegeState').val('');  
  32.                         $('#TxtCollegeCity').val('');  
  33.                     },  
  34.                     error: function () {  
  35.                         alert("An error occurred.");  
  36.                     }  
  37.                 });  
  38.             });  
  39.         });  
  40.     </script>  
  41. </head>  
  42.   
  43. <body>  
  44.     <form id="form1" runat="server">  
  45.         <div id="AddNewCollegeDiv">  
  46.             <ul id="AddNewCollege">  
  47.                 <li>  
  48.                     <label id="lblCollegeName">College Name</label>  
  49.                     <input type="text" id="TxtCollegeName" />  
  50.                 </li>  
  51.                 <li>  
  52.                     <label id="lblCollegeAddress">College Address</label>  
  53.                     <input type="text" id="TxtCollegeAddress" />  
  54.                 </li>  
  55.                 <li>  
  56.                     <label id="lblCollegePhone">College Phone</label>  
  57.                     <input type="text" id="TxtCollegePhone" />  
  58.                 </li>  
  59.                 <li>  
  60.                     <label id="lblCollegeEmailID">College EmailID</label>  
  61.                     <input type="text" id="TxtCollegeEmailID" />  
  62.                 </li>  
  63.                 <li>  
  64.                     <label id="lblContactPerson">Contact Person</label>  
  65.                     <input type="text" id="TxtContactPerson" />  
  66.                 </li>  
  67.                 <li>  
  68.                     <label id="lblCollegeState">State</label>  
  69.                     <input type="text" id="TxtCollegeState" />  
  70.                 </li>  
  71.                 <li>  
  72.                     <label id="lblCollegeCity">City</label>  
  73.                     <input type="text" id="TxtCollegeCity" />  
  74.                 </li>  
  75.                 <li>  
  76.                     <input type="button" id="BtnSubmit" value="Submit" />  
  77.                     <label id="lblResult" />  
  78.                 </li>  
  79.             </ul>  
  80.         </div>  
  81.     </form>  
  82. </body>  
  83. </html>  
Step 8: In the code behind replace the code with the following code:
  1. using CollegeTrackerLibrary.DAL;  
  2. using CollegeTrackerLibrary.MODEL;  
  3. using System;  
  4. using System.Web.Services;  
  5.   
  6. namespace CollegeTracker  
  7. {  
  8.     public partial class AddNewCollege : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.   
  13.         }  
  14.   
  15.         [WebMethod]  
  16.         public static string CreateCollegeData(CollegeDetails collegeDetails)  
  17.         {  
  18.             CollegeDAL obj = new CollegeDAL();  
  19.             bool b = obj.InsertCollegeDetails(collegeDetails);  
  20.             return "success";  
  21.         }  
  22.     }  
  23. }  
Note: I am using the System.WebServices.WebMethod and calling this method using jQuery.

Step 9: Run the application and click on the Add New College link and fill in the details:


Inserting Record

In the front page you can see the inserted record.

Inserted Record

That's it.

Summary

This article described the use of the Enterprise Library that is proven for application development from Microsoft. We learned how to do some database operations on the application and also called the WebMethod using jQuery. Thanks for reading.