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.
Step 2: Select the Empty Project Template to create the empty web application project.
Step 3: Right-click on the solution and add a new folder named Web.
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.
Step 2: Right-click on the folder and add a new project of class library named CollegeTrackerLibrary.
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.
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.
Step 4: Replace the code with the following code:
- namespace CollegeTrackerLibrary.MODEL
- {
- public class CollegeDetails
- {
- public Int16 CollegeID { get; set; }
- public string CollegeName { get; set; }
- public string CollegeAddress { get; set; }
- public Int64 CollegePhone { get; set; }
- public string CollegeEmailID { get; set; }
- public string ContactPerson { get; set; }
- public string State_Name { get; set; }
- public string City_Name { get; set; }
- }
- }
Step 5: Right-click on the DAL folder and add a new class named CollegeDAL and replace the code with the following code:
- using CollegeTrackerLibrary.MODEL;
- using Microsoft.Practices.EnterpriseLibrary.Data;
- using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.Common;
-
- namespace CollegeTrackerLibrary.DAL
- {
- public class CollegeDAL
- {
- #region Variable
-
-
-
- Database objDB;
-
-
-
- static string ConnectionString;
- #endregion
-
- #region Constructor
-
-
-
- public CollegeDAL()
- {
- ConnectionString = ConfigurationManager.ConnectionStrings["CollegeTrackerConnectionString"].ToString();
- }
- #endregion
-
- #region College
-
-
-
-
- public DataSet GetCollegeDetails()
- {
- objDB = new SqlDatabase(ConnectionString);
- using (DbCommand objcmd = objDB.GetStoredProcCommand("CT_CollegeDetails_Select"))
- {
- try
- {
- return objDB.ExecuteDataSet(objcmd);
- }
- catch (Exception ex)
- {
- throw ex;
- return null;
- }
- }
- }
-
-
-
-
-
-
- public bool InsertCollegeDetails(CollegeDetails collegeDetails)
- {
- bool result = false;
- objDB = new SqlDatabase(ConnectionString);
- using (DbCommand objCMD = objDB.GetStoredProcCommand("CT_CollegeDetails_INSERT"))
- {
- objDB.AddInParameter(objCMD, "@CollegeID", DbType.Int16, 0);
- objDB.AddInParameter(objCMD, "@CollegeName", DbType.String, collegeDetails.CollegeName);
- objDB.AddInParameter(objCMD, "@CollegeAddress", DbType.String, collegeDetails.CollegeAddress);
- objDB.AddInParameter(objCMD, "@CollegePhone", DbType.Int64, collegeDetails.CollegePhone);
- objDB.AddInParameter(objCMD, "@CollegeEmailID", DbType.String, collegeDetails.CollegeEmailID);
- objDB.AddInParameter(objCMD, "@ContactPerson", DbType.String, collegeDetails.ContactPerson);
- objDB.AddInParameter(objCMD, "@State", DbType.String, collegeDetails.State_Name);
- objDB.AddInParameter(objCMD, "@City", DbType.String, collegeDetails.City_Name);
- objDB.AddOutParameter(objCMD, "@Status", DbType.Int16, 0);
- try
- {
- objDB.ExecuteNonQuery(objCMD);
- result = Convert.ToBoolean(objDB.GetParameterValue(objCMD,"@Status"));
- }
- catch (Exception)
- {
- throw;
- }
- }
- return result;
- }
- #endregion
- }
- }
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.
Step 2: Add the library 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:
- <body>
- <form id="form1" runat="server">
- <div>
- <a href="AddNewCollege.aspx">Add New College</a>
- </div>
- <div>
- <asp:GridView ID="CollegeGridView" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Height="215px" Width="363px">
- <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
- <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
- <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
- <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
- <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#F1F1F1" />
- <SortedAscendingHeaderStyle BackColor="#594B9C" />
- <SortedDescendingCellStyle BackColor="#CAC9C9" />
- <SortedDescendingHeaderStyle BackColor="#33276A" />
- </asp:GridView>
- </div>
- </form>
- </body>
Step 5: In the code behind paste the following code to access the data from the library:
- using CollegeTrackerLibrary.DAL;
- using System;
-
- namespace CollegeTracker
- {
- public partial class CollegeDetailsForm : System.Web.UI.Page
- {
- CollegeDAL objCollege = new CollegeDAL();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- GetCollegeData();
- }
- }
- public void GetCollegeData()
- {
- CollegeGridView.DataSource = objCollege.GetCollegeDetails();
- CollegeGridView.DataBind();
- }
- }
- }
Step 6: Now, run the application and you can see the data in the GridView.
Note: I have formatted the GridView.
Step 7: Now add another webform named AddNewCollege and add the following code to the page:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="Content/Site.css" rel="stylesheet" />
- <script src="Scripts/jquery-2.1.3.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $('#BtnSubmit').click(function () {
- var collegeDetails = {};
- collegeDetails.CollegeName = $('#TxtCollegeName').val();
- collegeDetails.CollegeAddress = $('#TxtCollegeAddress').val();
- collegeDetails.CollegePhone = $('#TxtCollegePhone').val();
- collegeDetails.CollegeEmailID = $('#TxtCollegeEmailID').val();
- collegeDetails.ContactPerson = $('#TxtContactPerson').val();
- collegeDetails.State_Name = $('#TxtCollegeState').val();
- collegeDetails.City_Name = $('#TxtCollegeCity').val();
-
- $.ajax({
- type: 'POST',
- url: 'AddNewCollege.aspx/CreateCollegeData',
- data: "{collegeDetails:" + JSON.stringify(collegeDetails) + "}",
- dataType: 'json',
- contentType: 'application/json; charset=utf-8',
- success: function (response) {
- $('#lblResult').html('Inserted Successfully');
- $('#TxtCollegeName').val('');
- $('#TxtCollegeAddress').val('');
- $('#TxtCollegePhone').val('');
- $('#TxtCollegeEmailID').val('');
- $('#TxtContactPerson').val('');
- $('#TxtCollegeState').val('');
- $('#TxtCollegeCity').val('');
- },
- error: function () {
- alert("An error occurred.");
- }
- });
- });
- });
- </script>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div id="AddNewCollegeDiv">
- <ul id="AddNewCollege">
- <li>
- <label id="lblCollegeName">College Name</label>
- <input type="text" id="TxtCollegeName" />
- </li>
- <li>
- <label id="lblCollegeAddress">College Address</label>
- <input type="text" id="TxtCollegeAddress" />
- </li>
- <li>
- <label id="lblCollegePhone">College Phone</label>
- <input type="text" id="TxtCollegePhone" />
- </li>
- <li>
- <label id="lblCollegeEmailID">College EmailID</label>
- <input type="text" id="TxtCollegeEmailID" />
- </li>
- <li>
- <label id="lblContactPerson">Contact Person</label>
- <input type="text" id="TxtContactPerson" />
- </li>
- <li>
- <label id="lblCollegeState">State</label>
- <input type="text" id="TxtCollegeState" />
- </li>
- <li>
- <label id="lblCollegeCity">City</label>
- <input type="text" id="TxtCollegeCity" />
- </li>
- <li>
- <input type="button" id="BtnSubmit" value="Submit" />
- <label id="lblResult" />
- </li>
- </ul>
- </div>
- </form>
- </body>
- </html>
Step 8: In the code behind replace the code with the following code:
- using CollegeTrackerLibrary.DAL;
- using CollegeTrackerLibrary.MODEL;
- using System;
- using System.Web.Services;
-
- namespace CollegeTracker
- {
- public partial class AddNewCollege : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- [WebMethod]
- public static string CreateCollegeData(CollegeDetails collegeDetails)
- {
- CollegeDAL obj = new CollegeDAL();
- bool b = obj.InsertCollegeDetails(collegeDetails);
- return "success";
- }
- }
- }
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:
In the front page you can see the inserted record.
That's it.
SummaryThis 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.