Introducing Microsoft Enterprise Library in ASP.Net - Part 2

Background
 
Our last article explained how to do some operations with the data using the Enterprise Library. In that article, we performed insert and retrieval for the application. We used the Enterprise Library Data Access Application Block and found it extremely easy and simple to integrate and use. We called a web method using jQuery.
 
Please download College Tracker Project to continue the implementation with the last project.
 
Getting Started
 
In this article, we will learn to perform more operations with the data using the Enterprise Library. We will extend the library also to perform the complete CRUD and we will see that with this application we will upgrade the last application with less amount of code.
 
Step 1
 
Extract the Zipped file.
 
 
Step 2
 
Now open Visual Studio and locate the extracted file path to load the project.
 
 
 
Database Structure
 
In this section, we will create the following SQL Table and Stored Procedure.
 
Step 1
 
Create Table
  1. CREATE TABLE CollegeTracker  
  2. (  
  3. CollegeID int IDENTITY(1, 1) NOT NULL,  
  4. CollegeName varchar(50) NULL,  
  5. CollegeAddress varchar(50) NULL,  
  6. CollegePhone bigint NULL,  
  7. CollegeEmailID varchar(50) NULL,  
  8. ContactPerson varchar(50) NULL,  
  9. State_Name varchar(50) NULL,  
  10. City_Name varchar(50) NULL  
  11. )  
Step 2
 
Stored Procedures
  1. CREATE PROCEDURE [dbo].[CT_CollegeDetails_DELETE] @CollegeID int AS Begin  
  2. DELETE FROM  
  3. CollegeTracker  
  4. WHERE  
  5. CollegeTracker.CollegeID = @CollegeID End CREATE PROCEDURE [dbo].[CT_CollegeDetails_INSERT] @CName varchar(50),  
  6. @CAddress varchar(50),  
  7. @CPhone bigint,  
  8. @CEmailID varchar(50),  
  9. @CPerson varchar(50),  
  10. @SName varchar(50),  
  11. @C_Name varchar(50),  
  12. @Status int output AS Begin INSERT INTO CollegeTracker(  
  13. CollegeName, CollegeAddress, CollegePhone,  
  14. CollegeEmailID, ContactPerson, State_Name,  
  15. City_Name  
  16. )  
  17. VALUES  
  18. (  
  19. @CName, @CAddress, @CPhone, @CEmailID,  
  20. @CPerson, @SName, @C_Name  
  21. )  
  22. SET  
  23. @Status = 1 RETURN @Status End  
  1. CREATE PROCEDURE [dbo].[CT_CollegeDetails_Select]  
  2. AS  
  3. Begin  
  4. SELECT CollegeTracker.CollegeID,   
  5. CollegeTracker.CollegeName,   
  6. CollegeTracker.CollegeAddress,  
  7. CollegeTracker.CollegePhone,  
  8. CollegeTracker.CollegeEmailID,  
  9. CollegeTracker.ContactPerson,  
  10. CollegeTracker.State_Name,  
  11. CollegeTracker.City_Name  
  12. FROM CollegeTracker   
  13. End  
  1. CREATE PROCEDURE [dbo].[CT_CollegeDetails_UPDATE]  
  2. @CollegeId int,  
  3. @CName varchar(50),  
  4. @CAddress varchar(50),  
  5. @CPhone bigint,  
  6. @CEmailID varchar(50),  
  7. @CPerson varchar(50),  
  8. @SName varchar(50),  
  9. @C_Name varchar(50),  
  10. @Status int output  
  11. AS  
  12. Begin  
  13. UPDATE CollegeTracker  
  14. SET   
  15. CollegeName=@CName,  
  16. CollegeAddress=@CAddress,  
  17. CollegePhone=@CPhone,  
  18. CollegeEmailID=@CEmailID,  
  19. ContactPerson=@CPerson,  
  20. State_Name=@SName,  
  21. City_Name=@C_Name  
  22. WHERE CollegeID=@CollegeId  
  23. RETURN @Status  
  24. End 
  1. Create PROCEDURE [dbo].[CT_EditCollegeDetails_Select]  
  2. @CollegeId int  
  3. AS  
  4. Begin  
  5. SELECT * FROM CollegeTracker Where CollegeTracker.CollegeID=@CollegeId  
  6. End  
Implement Library
 
Now, in this section, we will implement the class library and add a reference of the Enterprise Library to perform some operation on the database and will rebuild the Library. Begin with the following procedure.
 
Step 1
 
Implement CollegeDAL.cs
  1. using CollegeTrackerLibrary.MODEL;  
  2. using Microsoft.Practices.EnterpriseLibrary.Data;  
  3. using Microsoft.Practices.EnterpriseLibrary.Data.Sql;  
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Configuration;  
  7. using System.Data;  
  8. using System.Data.Common;  
  9. using System.Data.SqlClient;  
  10. using System.Linq;  
  11. using System.Text;  
  12. using System.Threading.Tasks;  
  13.   
  14. namespace CollegeTrackerLibrary.DAL {  
  15.     public class CollegeDAL {#region Variable  
  16.         /// <summary>  
  17.         /// Specify the Database variable  
  18.         /// </summary>  
  19.         Database objDB;  
  20.         /// <summary>  
  21.         /// Specify the static variable  
  22.         /// </summary>  
  23.         static string ConnectionString;#endregion  
  24.  
  25.         #region Constructor  
  26.         /// <summary>  
  27.         /// This constructor is used to get the connectionstring from the config file  
  28.         /// </summary>  
  29.         public CollegeDAL() {  
  30.             ConnectionString = ConfigurationManager.ConnectionStrings["CollegeTrackerConnectionString"].ToString();  
  31.         }#endregion  
  32.  
  33.         #region College  
  34.         /// <summary>  
  35.         /// This method is used to get the college data  
  36.         /// </summary>  
  37.         /// <returns></returns>  
  38.         public DataSet GetCollegeDetails() {  
  39.             objDB = new SqlDatabase(ConnectionString);  
  40.             using(DbCommand objcmd = objDB.GetStoredProcCommand("CT_CollegeDetails_Select")) {  
  41.                 try {  
  42.                     return objDB.ExecuteDataSet(objcmd);  
  43.                 } catch (Exception ex) {  
  44.                     throw ex;  
  45.                 }  
  46.             }  
  47.         }  
  48.   
  49.         /// <summary>  
  50.         /// This method is used to insert the college data into the database  
  51.         /// </summary>  
  52.         /// <param name="collegeDetails"></param>  
  53.         /// <returns></returns>  
  54.         public bool InsertCollegeDetails(CollegeDetails collegeDetails) {  
  55.             bool result = false;  
  56.             objDB = new SqlDatabase(ConnectionString);  
  57.             using(DbCommand objCMD = objDB.GetStoredProcCommand("CT_CollegeDetails_INSERT")) {  
  58.                 //objDB.AddInParameter(objCMD, "@CID", DbType.Int16, 2);  
  59.                 objDB.AddInParameter(objCMD, "@CName", DbType.String, collegeDetails.CollegeName);  
  60.                 objDB.AddInParameter(objCMD, "@CAddress", DbType.String, collegeDetails.CollegeAddress);  
  61.                 objDB.AddInParameter(objCMD, "@CPhone", DbType.Int64, collegeDetails.CollegePhone);  
  62.                 objDB.AddInParameter(objCMD, "@CEmailID", DbType.String, collegeDetails.CollegeEmailID);  
  63.                 objDB.AddInParameter(objCMD, "@CPerson", DbType.String, collegeDetails.ContactPerson);  
  64.                 objDB.AddInParameter(objCMD, "@SName", DbType.String, collegeDetails.State_Name);  
  65.                 objDB.AddInParameter(objCMD, "@C_Name", DbType.String, collegeDetails.City_Name);  
  66.                 objDB.AddInParameter(objCMD, "@Status", DbType.Int16, 0);  
  67.                 try {  
  68.                     objDB.ExecuteNonQuery(objCMD);  
  69.                     result = Convert.ToBoolean(objDB.GetParameterValue(objCMD, "@Status"));  
  70.                 } catch (Exception) {  
  71.                     throw;  
  72.                 }  
  73.             }  
  74.             return result;  
  75.         }  
  76.         public void DeleteCollegeDetails(int id) {  
  77.             objDB = new SqlDatabase(ConnectionString);  
  78.             using(DbCommand objCMD = objDB.GetStoredProcCommand("CT_CollegeDetails_DELETE")) {  
  79.                 objDB.AddInParameter(objCMD, "@CollegeID", DbType.Int32, id);  
  80.   
  81.                 try {  
  82.                     objDB.ExecuteNonQuery(objCMD);  
  83.                 } catch (Exception) {  
  84.                     throw;  
  85.                 }  
  86.             }  
  87.   
  88.         }  
  89.   
  90.         public DataSet GetEditCollegeDetails(int id) {  
  91.             objDB = new SqlDatabase(ConnectionString);  
  92.             using(DbCommand objcmd = objDB.GetStoredProcCommand("CT_EditCollegeDetails_Select")) {  
  93.                 objDB.AddInParameter(objcmd, "@CollegeId", DbType.Int32, id);  
  94.                 try {  
  95.   
  96.                     return objDB.ExecuteDataSet(objcmd);  
  97.                 } catch (Exception ex) {  
  98.                     throw ex;  
  99.                 }  
  100.             }  
  101.         }  
  102.         public bool UpdateCollegeDetails(CollegeDetails collegeDetails) {  
  103.             bool result = false;  
  104.             objDB = new SqlDatabase(ConnectionString);  
  105.             using(DbCommand objCMD = objDB.GetStoredProcCommand("CT_CollegeDetails_UPDATE")) {  
  106.                 objDB.AddInParameter(objCMD, "@CollegeId", DbType.Int32, collegeDetails.CollegeID);  
  107.                 objDB.AddInParameter(objCMD, "@CName", DbType.String, collegeDetails.CollegeName);  
  108.                 objDB.AddInParameter(objCMD, "@CAddress", DbType.String, collegeDetails.CollegeAddress);  
  109.                 objDB.AddInParameter(objCMD, "@CPhone", DbType.Int64, collegeDetails.CollegePhone);  
  110.                 objDB.AddInParameter(objCMD, "@CEmailID", DbType.String, collegeDetails.CollegeEmailID);  
  111.                 objDB.AddInParameter(objCMD, "@CPerson", DbType.String, collegeDetails.ContactPerson);  
  112.                 objDB.AddInParameter(objCMD, "@SName", DbType.String, collegeDetails.State_Name);  
  113.                 objDB.AddInParameter(objCMD, "@C_Name", DbType.String, collegeDetails.City_Name);  
  114.                 objDB.AddInParameter(objCMD, "@Status", DbType.Int16, 0);  
  115.                 try {  
  116.                     objDB.ExecuteNonQuery(objCMD);  
  117.                     result = Convert.ToBoolean(objDB.GetParameterValue(objCMD, "@Status"));  
  118.                 } catch (Exception) {  
  119.                     throw;  
  120.                 }  
  121.             }  
  122.             return result;  
  123.         }#endregion  
  124.     }  
  125. }  
Build
 
Implement Application
 
In this section, we will add a reference of the library to our web application to make it work by the CollegeDAL and CollegeDetails class to perform some operation on the database.
 
Step 1
 
Right-click on the references in the Web Folder to add a Reference.
 
 
Step 2
 
Select the library available in the project.
 
 
Step 3
 
We will now add a new form to do the edit operations. To add a new form right-click on the solution in the Web folder and select Add New Item.
 
 
Step 4
 
Add a Web Form and provide it a meaningful name (EditCollege.aspx).
 
 
Step 5
 
Now implement the web forms as follows.
 
In this form, we can add new records.
 
AddNewCollege.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddNewCollege.aspx.cs" Inherits="CollegeTracker.AddNewCollege" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>   
  8.     <link href="Content/Site.css" rel="stylesheet" />  
  9.     <script src="Scripts/jquery-2.1.3.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function(){  
  12.             $('#BtnSubmit').click(function () {  
  13.                 var collegeDetails = {};  
  14.                 collegeDetails.CollegeName = $('#TxtCollegeName').val();  
  15.                 collegeDetails.CollegeAddress = $('#TxtCollegeAddress').val();  
  16.                 collegeDetails.CollegePhone = $('#TxtCollegePhone').val();  
  17.                 collegeDetails.CollegeEmailID = $('#TxtCollegeEmailID').val();  
  18.                 collegeDetails.ContactPerson = $('#TxtContactPerson').val();  
  19.                 collegeDetails.State_Name = $('#TxtCollegeState').val();  
  20.                 collegeDetails.City_Name = $('#TxtCollegeCity').val();  
  21.                 var pageUrl = '<%=ResolveUrl("~/AddNewCollege.aspx/CreateCollegeData")%>';  
  22.                 $.ajax({  
  23.                     type: 'POST',  
  24.                     url: 'AddNewCollege.aspx/CreateCollegeData',  
  25.                     data: "{collegeDetails:" + JSON.stringify(collegeDetails) + "}",  
  26.                     dataType: 'json',  
  27.                     contentType: 'application/json; charset=utf-8',  
  28.                     success: function (response) {  
  29.                         $('#lblResult').html('Inserted Successfully');  
  30.                         $('#TxtCollegeName').val('');  
  31.                         $('#TxtCollegeAddress').val('');  
  32.                         $('#TxtCollegePhone').val('');  
  33.                         $('#TxtCollegeEmailID').val('');  
  34.                         $('#TxtContactPerson').val('');  
  35.                         $('#TxtCollegeState').val('');  
  36.                         $('#TxtCollegeCity').val('');  
  37.                     },  
  38.                     error: function () {  
  39.                         alert("An error occurred.");  
  40.                     }  
  41.                 });  
  42.             });  
  43.         });  
  44.     </script>  
  45. </head>  
  46.   
  47. <body>  
  48.     <form id="form1" runat="server">  
  49.         <div id="AddNewCollegeDiv">  
  50.             <ul id="AddNewCollege">  
  51.                 <li>  
  52.                     <label id="lblCollegeName">College Name</label>  
  53.                     <input type="text" id="TxtCollegeName" />  
  54.                 </li>  
  55.                 <li>  
  56.                     <label id="lblCollegeAddress">College Address</label>  
  57.                     <input type="text" id="TxtCollegeAddress" />  
  58.                 </li>  
  59.                 <li>  
  60.                     <label id="lblCollegePhone">College Phone</label>  
  61.                     <input type="text" id="TxtCollegePhone" />  
  62.                 </li>  
  63.                 <li>  
  64.                     <label id="lblCollegeEmailID">College EmailID</label>  
  65.                     <input type="text" id="TxtCollegeEmailID" />  
  66.                 </li>  
  67.                 <li>  
  68.                     <label id="lblContactPerson">Contact Person</label>  
  69.                     <input type="text" id="TxtContactPerson" />  
  70.                 </li>  
  71.                 <li>  
  72.                     <label id="lblCollegeState">State</label>  
  73.                     <input type="text" id="TxtCollegeState" />  
  74.                 </li>  
  75.                 <li>  
  76.                     <label id="lblCollegeCity">City</label>  
  77.                     <input type="text" id="TxtCollegeCity" />  
  78.                 </li>  
  79.                 <li>  
  80.                     <input type="button" id="BtnSubmit" value="Submit" />  
  81.                     <label id="lblResult" />  
  82.                 </li>  
  83.                 <li>  
  84.                     <a href="CollegeDetailsForm.aspx">College Details</a>  
  85.                 </li>  
  86.             </ul>  
  87.         </div>  
  88.     </form>  
  89. </body>  
  90. </html>  
AddNewCollege.aspx.cs
  1. using CollegeTrackerLibrary.DAL;  
  2. using CollegeTrackerLibrary.MODEL;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Services;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. namespace CollegeTracker  
  12. {  
  13.     public partial class AddNewCollege : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         [WebMethod]  
  21.         public static string CreateCollegeData(CollegeDetails collegeDetails)  
  22.         {  
  23.             CollegeDAL obj = new CollegeDAL();  
  24.             bool b = obj.InsertCollegeDetails(collegeDetails);  
  25.             return "success";  
  26.         }  
  27.     }  
  28. }  
In this form, all the records will load in the Grid View.
 
CollegeDetailsForm.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CollegeDetailsForm.aspx.cs" Inherits="CollegeTracker.CollegeDetailsForm" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <script src="Scripts/jquery-1.7.1.min.js"></script>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div>  
  13.             <a href="AddNewCollege.aspx">Add New College</a>  
  14.         </div>  
  15.     <div>  
  16.         <asp:GridView ID="CollegeGridView" runat="server"   
  17.             BackColor="White" BorderColor="White"   
  18.             BorderStyle="Ridge" BorderWidth="2px"   
  19.             CellPadding="3" CellSpacing="1"   
  20.             GridLines="None" Height="215px"   
  21.             Width="363px" AutoGenerateColumns="false"  
  22.             DataKeyNames="CollegeID"  
  23.             OnRowCommand="CollegeGridView_RowCommand"  
  24.             OnRowDataBound="CollegeGridView_RowDataBound"   
  25.             OnRowDeleting="CollegeGridView_RowDeleting">  
  26.             <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />  
  27.             <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />  
  28.             <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />  
  29.             <RowStyle BackColor="#DEDFDE" ForeColor="Black" />  
  30.             <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />  
  31.             <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  32.             <SortedAscendingHeaderStyle BackColor="#594B9C" />  
  33.             <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  34.             <SortedDescendingHeaderStyle BackColor="#33276A" />  
  35.             <Columns>  
  36.                 <asp:BoundField DataField="CollegeID" HeaderText="College ID" Visible="false" />  
  37.                 <asp:BoundField DataField="CollegeName" HeaderText="CollegeName" />  
  38.                 <asp:BoundField DataField="CollegeAddress" HeaderText="College Address" />  
  39.                 <asp:BoundField DataField="CollegePhone" HeaderText="Phone" />  
  40.                 <asp:BoundField DataField="CollegeEmailID" HeaderText="Email Id" />  
  41.                 <asp:BoundField DataField="ContactPerson" HeaderText="Contact Person" />  
  42.                 <asp:BoundField DataField="State_Name" HeaderText="State" />  
  43.                 <asp:BoundField DataField="City_Name" HeaderText="City" />  
  44.                 <asp:HyperLinkField DataNavigateUrlFields="CollegeID" HeaderText="Links" DataNavigateUrlFormatString="EditCollege.aspx?CollegeID={0}" Text="Edit" />  
  45.                 <asp:TemplateField HeaderText="Delete">  
  46.                 <ItemTemplate>  
  47.                     <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CollegeID") %>' CommandName="Delete" runat="server"> Delete</asp:LinkButton>  
  48.                 </ItemTemplate>  
  49.               </asp:TemplateField>  
  50.             </Columns>  
  51.         </asp:GridView>      
  52.     </div>  
  53.     </form>  
  54. </body>  
  55. </html>  
CollegeDetailsForm.aspx.cs
  1. using CollegeTrackerLibrary.DAL;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Services;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9.   
  10. namespace CollegeTracker  
  11. {  
  12.     public partial class CollegeDetailsForm : System.Web.UI.Page  
  13.     {  
  14.         CollegeDAL objCollege = new CollegeDAL();  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.             if (!IsPostBack)  
  18.             {  
  19.                 GetCollegeData();  
  20.             }  
  21.         }  
  22.         public void GetCollegeData()  
  23.         {  
  24.             CollegeGridView.DataSource = objCollege.GetCollegeDetails();  
  25.             CollegeGridView.DataBind();  
  26.         }  
  27.         protected void CollegeGridView_RowDataBound(object sender, GridViewRowEventArgs e)  
  28.         {  
  29.             if (e.Row.RowType == DataControlRowType.DataRow)  
  30.             {  
  31.                    
  32.                     LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");  
  33.                     l.Attributes.Add("onclick""javascript:return " +  
  34.                     "confirm('Are you sure you want to delete')");  
  35.             }  
  36.         }  
  37.   
  38.         protected void CollegeGridView_RowCommand(object sender, GridViewCommandEventArgs e)  
  39.         {  
  40.             if (e.CommandName == "Delete")  
  41.             {  
  42.               int categoryID = Convert.ToInt32(e.CommandArgument);  
  43.                 
  44.             }  
  45.         }  
  46.         protected void CollegeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  47.         {  
  48.   
  49.             GridViewRow row = (GridViewRow)CollegeGridView.Rows[e.RowIndex];  
  50.             Label lbldeleteid = (Label)row.FindControl("lblID");  
  51.             int id = Convert.ToInt32(CollegeGridView.DataKeys[e.RowIndex].Value.ToString());  
  52.             CollegeDAL obj = new CollegeDAL();  
  53.             obj.DeleteCollegeDetails(id);  
  54.             GetCollegeData();  
  55.         }  
  56.   
  57.     }  
  58. }  
After pressing the Edit Button in the Grid View this form will load with the Grid View row to update the record.
 
EditCollege.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditCollege.aspx.cs" Inherits="CollegeTracker.EditCollege" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>   
  8.     <link href="Content/Site.css" rel="stylesheet" />  
  9.     <script src="Scripts/jquery-2.1.3.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function(){  
  12.             $('#BtnSubmit').click(function () {  
  13.                   
  14.                 var collegeDetails = {};  
  15.                 collegeDetails.CollegeID = $('#TxtCollegeID').val();  
  16.                 collegeDetails.CollegeName = $('#TxtCollegeName').val();  
  17.                 collegeDetails.CollegeAddress = $('#TxtCollegeAddress').val();  
  18.                 collegeDetails.CollegePhone = $('#TxtCollegePhone').val();  
  19.                 collegeDetails.CollegeEmailID = $('#TxtCollegeEmailID').val();  
  20.                 collegeDetails.ContactPerson = $('#TxtContactPerson').val();  
  21.                 collegeDetails.State_Name = $('#TxtCollegeState').val();  
  22.                 collegeDetails.City_Name = $('#TxtCollegeCity').val();  
  23.                 var pageUrl = '<%=ResolveUrl("~/EditCollege.aspx/UpdtCollegeDetails")%>';  
  24.                 $.ajax({  
  25.                     type: 'POST',  
  26.                     url: 'EditCollege.aspx/UpdtCollegeDetails',  
  27.                     data: "{collegeDetails:" + JSON.stringify(collegeDetails) + "}",  
  28.                     dataType: 'json',  
  29.                     contentType: 'application/json; charset=utf-8',  
  30.                     success: function (response) {  
  31.                         $('#lblResult').html('Updated Successfully');  
  32.                         //$('#TxtCollegeName').val('');  
  33.                         //$('#TxtCollegeAddress').val('');  
  34.                         //$('#TxtCollegePhone').val('');  
  35.                         //$('#TxtCollegeEmailID').val('');  
  36.                         //$('#TxtContactPerson').val('');  
  37.                         //$('#TxtCollegeState').val('');  
  38.                         //$('#TxtCollegeCity').val('');  
  39.                     },  
  40.                     error: function () {  
  41.                         alert("An error occurred.");  
  42.                     }  
  43.                 });  
  44.             });  
  45.         });  
  46.     </script>  
  47.       
  48. </head>  
  49.   
  50. <body>  
  51.        
  52.     <form id="form1" runat="server">  
  53.          
  54.         <div id="AddNewCollegeDiv">  
  55.             <ul id="AddNewCollege">  
  56.                 <li>  
  57.                     <label id="lblCollegeName" runat="server">College Name</label>  
  58.                     <input type="text" id="TxtCollegeName" runat="server" />  
  59.                 </li>  
  60.                 <li>  
  61.                     <label id="lblCollegeAddress" runat="server">College Address</label>  
  62.                     <input type="text" id="TxtCollegeAddress" runat="server" />  
  63.                 </li>  
  64.                 <li>  
  65.                     <label id="lblCollegePhone">College Phone</label>  
  66.                     <input type="text" id="TxtCollegePhone" runat="server" />  
  67.                 </li>  
  68.                 <li>  
  69.                     <label id="lblCollegeEmailID">College EmailID</label>  
  70.                     <input type="text" id="TxtCollegeEmailID" runat="server" />  
  71.                 </li>  
  72.                 <li>  
  73.                     <label id="lblContactPerson">Contact Person</label>  
  74.                     <input type="text" id="TxtContactPerson" runat="server" />  
  75.                 </li>  
  76.                 <li>  
  77.                     <label id="lblCollegeState">State</label>  
  78.                     <input type="text" id="TxtCollegeState" runat="server" />  
  79.                 </li>  
  80.                 <li>  
  81.                     <label id="lblCollegeCity">City</label>  
  82.                     <input type="text" id="TxtCollegeCity" runat="server" />  
  83.                 </li>  
  84.                 <li>  
  85.                     <input type="button" id="BtnSubmit" value="Submit" />  
  86.                     <label id="lblResult" />  
  87.                 </li>  
  88.                 <li>  
  89.                     <a href="CollegeDetailsForm.aspx">Back</a>  
  90.                     <input type="text" id="TxtCollegeID" runat="server" />  
  91.                 </li>  
  92.   
  93.              </ul>  
  94.         </div>  
  95.       </form>  
  96. </body>  
  97. </html>  
EditCollege.aspx.cs
  1. using CollegeTrackerLibrary.DAL;  
  2. using CollegeTrackerLibrary.MODEL;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Services;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11.   
  12. namespace CollegeTracker  
  13. {  
  14.     public partial class EditCollege : System.Web.UI.Page  
  15.     {  
  16.           
  17.           
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.               
  21.             if (!IsPostBack)  
  22.             {  
  23.                 TxtCollegeID.Value = Request.QueryString["CollegeID"];  
  24.                 //lblid = Request.QueryString["CollegeID"].ToString();  
  25.                 int id = Convert.ToInt32(TxtCollegeID.Value);  
  26.                  
  27.                 CollegeDAL obj = new CollegeDAL();  
  28.   
  29.                TxtCollegeName.Value =obj.GetEditCollegeDetails(id).Tables[0].Rows[0][1].ToString();  
  30.                TxtCollegeAddress.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][2].ToString();  
  31.                TxtCollegePhone.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][3].ToString();  
  32.                TxtCollegeEmailID.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][4].ToString();  
  33.                TxtContactPerson.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][5].ToString();  
  34.                TxtCollegeState.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][6].ToString();  
  35.                TxtCollegeCity.Value = obj.GetEditCollegeDetails(id).Tables[0].Rows[0][7].ToString();  
  36.   
  37.             }  
  38.   
  39.         }  
  40.         [WebMethod]  
  41.         public static string UpdtCollegeDetails(CollegeDetails collegeDetails)  
  42.         {  
  43.             CollegeDAL obj = new CollegeDAL();  
  44.             //int id = Convert.ToInt32(lblid.InnerText);  
  45.             bool b = obj.UpdateCollegeDetails(collegeDetails);  
  46.             return "Update success";  
  47.         }  
  48.    }  
  49. }  
Run Application
 
Now our web application is ready to run, right-click on the solution available in the Web folder and click on Set As Startup Project. Press F5/Run.
 
Step 1
 
In this form, we can see the list of data stored in the tables using the grid view, and now we are able to edit and delete the data.
 
 
Step 2
 
In this form, we can add new data.
 
 
Step 3
 
If we will do a delete then it will give a prompt message to the user before deleting.
 
Confirm
 
Step 4
 
In this form, we will edit the records.
 
 

Summary

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


Recommended Free Ebook
Similar Articles