List Records
Add Records First Name: Last Name: Age: Picture - (Default.aspx) AddRecords method - Hide Code protected void AddRecords(object sender, EventArgs e) { //Lets validate the page first if (!Page.IsValid) return; int intResult = 0; // Page is valid, lets go ahead and insert records // Instantiate BAL object PersonBAL pBAL = new PersonBAL(); // Instantiate the object we have to deal with Person person = new Person(); // set the properties of the object person.FirstName = txtFirstName.Text; person.LastName = txtLastName.Text; person.Age = Int32.Parse(txtAge.Text); try { intResult = pBAL.Insert(person); if (intResult > 0) lblMessage.Text = "New record inserted successfully."; else lblMessage.Text = "FirstName ["+ txtFirstName.Text +"] alredy exists, try another name"; } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { person = null; pBAL = null; } } In the above method, I am doing following things mainly: Instantiating BAL object Instantiating BO object Settinng properties of BO object by the textbox values Calling Insert method of the BAL object and passing BO object as parameter [pBAL.Insert(person)] in try block Checking for number of records affected, If the number is more than zero, I am writing Success message otherwise Duplicate records found If any layer will throw any error, I am catching it and displaying to the user in throw block Whatever objects I had instantiated, I am specifying their values to null to let the GC know that I am no more going to use them User Interface - [UI]-List.aspx In this page, I am going to use a GridView to List, Modify, Sort and Delete records from the database Create an aspx page in the same 4-Tier folder named List.aspx (Picture - 3) Following is the code for the GridView that will data manipulation for us - Hide CodeAdd Record
Picture - (List.aspx) On the OnRowEditing, OnRowUpdating, OnRowCancelEdit, OnSorting and OnRowDeleting events we are calling respective methods to data manipulation Following are codes to bind the GridView and methods that will fire on GridView events - Hide Code protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) BindGrid(); } /// /// Fired when Cancel button is clicked /// /// /// protected void CancelRecord(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; BindGrid(); } /// /// Fires when Edit button is clicked /// /// /// protected void EditRecord(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; BindGrid(); } /// /// Fires when Update button is clicked /// /// /// protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) { int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); int intResult = 0; GridViewRow row = GridView1.Rows[e.RowIndex]; TextBox tFN = (TextBox) row.FindControl("txtFName"); TextBox tLN = (TextBox)row.FindControl("txtLName"); TextBox tAge = (TextBox)row.FindControl("txtAge"); // instantiate BAL PersonBAL pBAL = new PersonBAL(); Person person = new Person(); try { person.PersonID = personID; person.FirstName = tFN.Text; person.LastName = tLN.Text; person.Age = Int32.Parse(tAge.Text); intResult = pBAL.Update(person); if (intResult > 0) lblMessage.Text = "Record Updated Successfully."; else lblMessage.Text = "Record couldn't updated"; } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { person = null; pBAL = null; } GridView1.EditIndex = -1; // Refresh the list BindGrid(); } /// /// fires when Delete button is clicked /// /// /// protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) { int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); // instantiate BAL PersonBAL pBAL = new PersonBAL(); Person person = new Person(); try { person.PersonID = personID; pBAL.Delete(person); lblMessage.Text = "Record Deleted Successfully."; } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { person = null; pBAL = null; } GridView1.EditIndex = -1; // Refresh the list BindGrid(); } /// /// Fires when page links are clicked /// /// /// protected void ChangePage(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; // Refresh the list BindGrid(); } /// /// Fires when Columns heading are clicked /// /// /// protected void SortRecords(object sender, GridViewSortEventArgs e) { DataTable dataTable = GridDataSource(); if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = GetSortExpression(e); GridView1.DataSource = dataView; GridView1.DataBind(); } } #region Private Methods /// /// Bind the gridview /// private void BindGrid() { GridView1.DataSource = GridDataSource(); GridView1.DataBind(); } /// /// Get GridView DataSource /// private DataTable GridDataSource() { PersonBAL p = new PersonBAL(); DataTable dTable = new DataTable(); try { dTable = p.Load(); } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { p = null; } return dTable; } /// /// Get sort expression for the gridview /// /// /// private string GetSortExpression(GridViewSortEventArgs e) { string sortDirection = string.Empty; // if clicked on the same column twice then let it toggle the sort order, else reset to ascending if (ViewState["SortExpression"] != null) { if (!ViewState["SortExpression"].ToString().Equals(e.SortExpression.ToLower())) { ViewState["SortDirection"] = null; } } if (ViewState["SortDirection"] != null) { if (ViewState["SortDirection"].ToString().Equals("ASC")) { sortDirection = "DESC"; ViewState["SortDirection"] = "DESC"; } else { sortDirection = "ASC"; ViewState["SortDirection"] = "ASC"; } } else { ViewState["SortDirection"] = "ASC"; } ViewState["SortExpression"] = e.SortExpression.ToLower(); return e.SortExpression + " " + sortDirection; } #endregion Private Methods Thats it!!! Now we have done all our home work, Its time to enjoy now Just build the application by pressing Ctrl+Shift+B, once it shows Build Succeeded in the status bar at the bottom, you are ready to enjoy your 4-Tier architecture application Browse it as http://localhost/4-Tier (Provided you have created "4-Tier" virtual directory in your IIS or you can choose your own virtual directory name), you should see your screens as shown above (default.aspx and list.aspx pages) Try manipulating data and see how effectively its happening This was a simple application, based on this architecture, you can create larger application by adding separate objects and their corresponding tiers In order to use namespace in different tiers, you should define your namespace in respective tiers objects and use those namespace by using statement whereever you need ... ee.Message.ToString(); } finally { person = null; pBAL = null; } } In the above method, I am doing following things mainly: Instantiating BAL object Instantiating BO object Settinng properties... it - Hide Code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;... using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using