WEB460 Lab 4 of 7: Create a Business Layer Class and Use an XML File

$ 15

WEB460 Lab 4 of 7: Create a Business Layer Class and Use an XML File

Scenario/Summary
This week, we add the business layer to our application. This layer handles business rules and data validation based on those rules. It also handles communication with external websites and applications, so this layer is where we implement creating, reading, and writing an XML file for data transfer.
If you haven’t noticed, pgCheckOut.aspx.cs has become fairly large, making it difficult to maintain. The reason for this is that we have the pgCheckOut class doing much of the work that belongs in the business layer and data layer. Next week, we deal with this situation and refactor our code to better distribute the workload among the classes in our application.
Refactoring does not change the external functionality of software, but it changes the internal structure by reorganizing it. After refactoring our application, it should look the same to users but execute more efficiently and be easier to maintain. More on this next week. For now, let’s add some GridView controls and XML interaction.

Here is an overview of the steps in this lab:
Step A: Set up a new website with a copy of the Week 3 Lab.
Step B: In the data layer, fill a DataSet with data on all the customers. We’ll later bind this DataSet to a GridView.
Step C: Add control to pgCheckOUt.aspx. We will add two GridViews and two labels for the GridViews and a Button to update an XML file.
Step D: Bind one of the GridViews to the DataSet listing all customers in the database. We have it automatically update when the page is loaded and when customers are updated or added.
Step F: We build our business layer and have it read from and write to the XML file.
Step G: We bind the second GridView in pgCheckOut.aspx to the parsed data in the XML file and connect the Click event of btnUpdateXML to a method that updates the XML file.
This diagram shows the major components we will be working with in this lab and how they interact.
We have a lot to accomplish, so let’s get started!

Lab Steps
STEP A: Set Up a New Web Site
1. Create a new Empty Web Site project.
2. Copy all the files from last week’s Lab into the website folder for this week.
3. Test that your application functions and runs the same as it did last week.

STEP B: In the Data Layer, Fill a DataSet With all Customers
In our previous lab, we filled a DataSet with data on a single customer from the database. In this lab, we want to show the user a list of all customers, so we need to add a method to our data layer that fetches all the customers and fills a DataSet with this list.
In clsDataLayer.cs, add the following code for the GetAllCustomers method:
GetAllCustomers Method in clsDataLayer.cs
public dsAccounts GetAllCustomers()
{
//Add your comments here
OleDbDataAdapter sqlDataAdapter = new OleDbDataAdapter ( “select * from tblCustomers;”, dbConnection );
//Add your comments here
dsAccounts myStoreDataSet = new dsAccounts();
sqlDataAdapter.Fill(myStoreDataSet.blCustomers);
// Add your comments here
return myStoreDataSet;
}

STEP C: Add Controls to pgCheckOut.aspx
In this step, we add two Labels, two GridViews, and a Button to pgCheckOut.aspx. One GridView will be bound to the database and the other to an XML file. The labels will label the GridView so that the user knows which each is. The Button is used to update the XML file after changes have been made to the database.
We do not bind the GridViews to a data source at this time! This will be done later in our C# code.
The first GridView, gvCustomerList, will be bound to a DataSet with records from the database.
The second GridView, gvXML, will be bound to a DataSet comprised of record from an XMLfile that we can use to transmit customer information to another application.
1. Add the two Label and GridView controls by adding the following statements after the lines for the Phone Number textbox on the left column of the web page:
<asp:Label ID=”lblGVDataBase” runat=”server” Text=”Customers in Database:”></asp:Label><br/>
<asp:GridView ID=”gvCustomerList” runat=”server” Font-Size=”9″></asp:GridView><br/>
<asp:Label ID=”lblGVXML” runat=”server” Text=”Customers in XML File:”></asp:Label><br/>
<asp:GridView ID=”gvXML” runat=”server” Font-Size=”9″></asp:GridView>
2. On the right column of the web page, add the following line to place a Button for updating the XML file below the Submit Order button:
<br/><br/>
<asp:Button ID=”btnUpdateXML” runat=”server” Text=”Update XML File”/>
3.When you have completed this step, the design view of pgCheckOut.aspx should be similar to this image:

STEP D: Bind the First GridView inpgCheckOut.aspx.cs
Now that we have a method in our data layer that returns all the customers in a DataSetobject, we can bind that DataSet to our GridView control.
1. The private method BindCustomerGridView calls the data layer to fetch a list of all customers and then binds that to the GridView. Additionally, it stores the DataSet in the cache. Later, we’ll use the cache to update the XML file.
Add the following to pgCheckOut.aspx.cs:
BindCustomerGridView Method for pgCheckOut.aspx.cs
private dsAccounts BindCustomerGridView()
{
// Add your comments here
// Depending on where you placed your Access database,
// one of the following lines may work better:
// tempPath = Server.MapPath(“Accounts.mdb”)
// tempPath = Server.MapPath(“~/FPDB/Accounts.mdb”)
string tempPath = Server.MapPath(“~/App_Data/Accounts.mdb”);
clsDataLayer myDataLayer=new clsDataLayer(tempPath);
// Add your comments here
dsAccounts customerListing=myDataLayer.GetAllCustomers();
// Add your comments here
gvCustomerList.DataSource=customerListing.tblCustomers;
// Add your comments here
gvCustomerList.DataBind();
Cache.Insert(“CustomerDataSet”,customerListing);
return customerListing;
}
2. We must now call this method so the GridView, gvCustomerList, is updated to reflect the customers in the database. There are three times we must call this method if we want the GridView to update without user interaction:
In the method Page_Load: When the page first loads, we perform the initial binding of the GridView.
In the method btnAdd_Click: After creating a new customer record, we ensure that it shows up in the GridView.
In the method btnUpdate_Click: After updating customer information, we ensure that the new information shows up in the GridView.
Add the following statements to the end of the Page_Load, btnUpdate_Click and btnAdd_Click methods in pgCheckOut.aspx.cs.
// Add your comments here
BindCustomerGridView();
3. Test your to code to ensure the gvCustomerList is properly bound and updated.

STEP E: Implementing the Business Layer
The business layer is implemented in C# class clsBusinessLayer. For this lab, there are three methods and two data fields that we need to create in clsBusinessLayer. In our next lab, we will refactor our application and add more functionality to this class.
The business layer functionality we need in this lab is the ability to read and write the XML file customers.xml. This is handled in the business layer because it does not directly involve the database and is part of the communication our application does with other applications. The XML file we create can be read from the disk by another application on our server or transmitted to a remote application.
1. Add a new class to your application, name it clsBusinessLayer.cs, and add the statement:
using System.Data;
2. Add two data fields to your class.
dataPath is the path on the server to the App_Data directory or whichever directory the user wants the XML file stored. This will be passed as an argument to the clsBusinessLayer constructor and initialized there.
The data field myDataLayer represents an instance of the data layer. We use this for our database interaction. Like all data fields, it is initialized in the constructor.
Data Fields for the Class clsBusinessLayer
// Add your comments here
string dataPath;
// Add your comments here
clsDataLayer myDataLayer;
3. Our clsBusinessClass constructor serves to initialize all our data fields (a programming best practice).
Constructor for clsBusinessLayer
public clsBusinessLayer(string serverMappedPath)
{
// Add your comments here
dataPath = serverMappedPath;
myDataLayer = new clsDataLayer(dataPath + “Accounts.mdb”);
}
4. The next functionality to add to our business layer is the ability to read (or get) the XML file with customer data ( customers.xml). This method creates a DataSet to hold the customer records parsed from the XML file. It then attempts to read the XML file. If the file is not found, we catch the exception and fix the situation by creating a fresh XML file. We can read the new file and place it in the DataSet object.
Add this code to clsBusinessLayer.cs:
GetCustomerXMLFile Method in clsBusinessLayer.cs
public DataSet GetCustomerXMLFile()
{
// Add your comments here
DataSet xmlDataSet = new DataSet();
try
{
// Add your comments here
xmlDataSet.ReadXml(dataPath+”customers.xml”);
}catch(System.IO.FileNotFoundException error)
{
// Add your comments here
dsAccounts customerListing = myDataLayer.GetAllCustomers();
customerListing.tblCustomers.WriteXml(dataPath+”customers.xml”);
xmlDataSet.ReadXml(dataPath+”customers.xml”);
}
// Add your comments here
return xmlDataSet;
}
5. Our last task with clsBusinessLayer.cs is to add a function that writes the XML data file, customers.xml. To do this, we will read the current list of customers from the Cache object. If you remember, previously, we saved the updated list of customer to the cache as a way to save returning to the database to read customer data. We can read from the cache, which is a much quicker operation.
Add this code to clsBusinessLayer.cs:
WriteCustomerXMLFile Method in clsBusinessLayer.cs
public DataSet WriteCustomerXMLFile(System.Web.Caching.Cache appCache)
{
// Add your comments here
DataSet xmlDataSet = (DataSet) appCache[“CustomerDataSet”];
// Add your comments here
xmlDataSet.WriteXml(dataPath+”customers.xml”);
// Add your comments here
return xmlDataSet;
}
We’ll be able to test this code after we complete the next step.

STEP F: Calling the Business Layer and Binding aGridView
The last task we have is to bind the GridView, gvXML, to the data parsed from the XML file and update the XML file based on data stored in the Cache object.
1. Because our presentation layer class, pgCheckOut, will need to access the business layer frequently, let’s create a data field of type clsBusinessLayer and initialize it in our Page_Load method. This activity is similar to how we handled the clsDataLayer object in our business layer class. Add the following data field to pgCheckOut.aspx.cs:
clsBusinessLayer myBusinessLayer;
Then in the Page_Load, add the following to initialize the data field:
//Add your comments here
// Ensure the argument to MapPath reflects the path
// To where the database is stored. This is also where the
// XML file will be saved. Don’t forget the closing ‘/’
// If you are using Citrix, the directory should be ~/FPDB/
myBusinessLayer = new clsBusinessLayer(Server.MapPath(“~/App_Data/”));
2.We can easily bind the GridView to the data in the XML file by asking the business layer to process the file and handle any errors. We can expect back a DataSet that webind to the GridView. Add the following code to pgCheckOut.aspx.cs:
BindXMLGridView Method in pgCheckOut.aspx.cs
public void BindXMLGridView()
{
// Add your comments here
gvXML.DataSource = myBusinessLayer.GetCustomerXMLFile();
gvXML.DataBind();
}
3. This method needs to be called when the page first loads so that gvXML displays data. Add the following statement to the end of the Page_Load method:
BindXMLGridView();
4. For this lab, we want to update gvXML manually (as opposed to automatically as we do with gvCustomerList). The following click method will be called from the Button btnUpdateXML. It updates the XML from the Cache object and then binds gvXML.
btnUpdateXML_Click Method in pgCheckOut.aspx.cs
public void btnUpdateXML_Click(object sender, EventArgs e)
{
// Add your comments here
gvXML.DataSource = myBusinessLayer.WriteCustomerXMLFile(Cache);
gvXML.DataBind();
}
5. Finally, we need to set the Click event for btnUpdateXML. In the ASP file pgCheckOut.aspx , add the following to the definition for btnUpdateXML :
OnClick=”btnUpdateXML_Click”
6. At this point, you should be able to test the entire application’s functionality.

STEP G: Finalize the Lab.
1. Save your work!
2. Test it! Make changes as appropriate until it works. Try updating or adding new records to the database.
3. Remember to add comments for each step being performed.
4. Please zip and submit the entire web project folder.

992 in stock

Description

WEB460 Lab 4 of 7: Create a Business Layer Class and Use an XML File

Scenario/Summary
This week, we add the business layer to our application. This layer handles business rules and data validation based on those rules. It also handles communication with external websites and applications, so this layer is where we implement creating, reading, and writing an XML file for data transfer.
If you haven’t noticed, pgCheckOut.aspx.cs has become fairly large, making it difficult to maintain. The reason for this is that we have the pgCheckOut class doing much of the work that belongs in the business layer and data layer. Next week, we deal with this situation and refactor our code to better distribute the workload among the classes in our application.
Refactoring does not change the external functionality of software, but it changes the internal structure by reorganizing it. After refactoring our application, it should look the same to users but execute more efficiently and be easier to maintain. More on this next week. For now, let’s add some GridView controls and XML interaction.

Here is an overview of the steps in this lab:
Step A: Set up a new website with a copy of the Week 3 Lab.
Step B: In the data layer, fill a DataSet with data on all the customers. We’ll later bind this DataSet to a GridView.
Step C: Add control to pgCheckOUt.aspx. We will add two GridViews and two labels for the GridViews and a Button to update an XML file.
Step D: Bind one of the GridViews to the DataSet listing all customers in the database. We have it automatically update when the page is loaded and when customers are updated or added.
Step F: We build our business layer and have it read from and write to the XML file.
Step G: We bind the second GridView in pgCheckOut.aspx to the parsed data in the XML file and connect the Click event of btnUpdateXML to a method that updates the XML file.
This diagram shows the major components we will be working with in this lab and how they interact.
We have a lot to accomplish, so let’s get started!

Lab Steps
STEP A: Set Up a New Web Site
1. Create a new Empty Web Site project.
2. Copy all the files from last week’s Lab into the website folder for this week.
3. Test that your application functions and runs the same as it did last week.

STEP B: In the Data Layer, Fill a DataSet With all Customers
In our previous lab, we filled a DataSet with data on a single customer from the database. In this lab, we want to show the user a list of all customers, so we need to add a method to our data layer that fetches all the customers and fills a DataSet with this list.
In clsDataLayer.cs, add the following code for the GetAllCustomers method:
GetAllCustomers Method in clsDataLayer.cs
public dsAccounts GetAllCustomers()
{
//Add your comments here
OleDbDataAdapter sqlDataAdapter = new OleDbDataAdapter ( “select * from tblCustomers;”, dbConnection );
//Add your comments here
dsAccounts myStoreDataSet = new dsAccounts();
sqlDataAdapter.Fill(myStoreDataSet.blCustomers);
// Add your comments here
return myStoreDataSet;
}

STEP C: Add Controls to pgCheckOut.aspx
In this step, we add two Labels, two GridViews, and a Button to pgCheckOut.aspx. One GridView will be bound to the database and the other to an XML file. The labels will label the GridView so that the user knows which each is. The Button is used to update the XML file after changes have been made to the database.
We do not bind the GridViews to a data source at this time! This will be done later in our C# code.
The first GridView, gvCustomerList, will be bound to a DataSet with records from the database.
The second GridView, gvXML, will be bound to a DataSet comprised of record from an XMLfile that we can use to transmit customer information to another application.
1. Add the two Label and GridView controls by adding the following statements after the lines for the Phone Number textbox on the left column of the web page:

2. On the right column of the web page, add the following line to place a Button for updating the XML file below the Submit Order button:

3.When you have completed this step, the design view of pgCheckOut.aspx should be similar to this image:

STEP D: Bind the First GridView inpgCheckOut.aspx.cs
Now that we have a method in our data layer that returns all the customers in a DataSetobject, we can bind that DataSet to our GridView control.
1. The private method BindCustomerGridView calls the data layer to fetch a list of all customers and then binds that to the GridView. Additionally, it stores the DataSet in the cache. Later, we’ll use the cache to update the XML file.
Add the following to pgCheckOut.aspx.cs:
BindCustomerGridView Method for pgCheckOut.aspx.cs
private dsAccounts BindCustomerGridView()
{
// Add your comments here
// Depending on where you placed your Access database,
// one of the following lines may work better:
// tempPath = Server.MapPath(“Accounts.mdb”)
// tempPath = Server.MapPath(“~/FPDB/Accounts.mdb”)
string tempPath = Server.MapPath(“~/App_Data/Accounts.mdb”);
clsDataLayer myDataLayer=new clsDataLayer(tempPath);
// Add your comments here
dsAccounts customerListing=myDataLayer.GetAllCustomers();
// Add your comments here
gvCustomerList.DataSource=customerListing.tblCustomers;
// Add your comments here
gvCustomerList.DataBind();
Cache.Insert(“CustomerDataSet”,customerListing);
return customerListing;
}
2. We must now call this method so the GridView, gvCustomerList, is updated to reflect the customers in the database. There are three times we must call this method if we want the GridView to update without user interaction:
In the method Page_Load: When the page first loads, we perform the initial binding of the GridView.
In the method btnAdd_Click: After creating a new customer record, we ensure that it shows up in the GridView.
In the method btnUpdate_Click: After updating customer information, we ensure that the new information shows up in the GridView.
Add the following statements to the end of the Page_Load, btnUpdate_Click and btnAdd_Click methods in pgCheckOut.aspx.cs.
// Add your comments here
BindCustomerGridView();
3. Test your to code to ensure the gvCustomerList is properly bound and updated.

STEP E: Implementing the Business Layer
The business layer is implemented in C# class clsBusinessLayer. For this lab, there are three methods and two data fields that we need to create in clsBusinessLayer. In our next lab, we will refactor our application and add more functionality to this class.
The business layer functionality we need in this lab is the ability to read and write the XML file customers.xml. This is handled in the business layer because it does not directly involve the database and is part of the communication our application does with other applications. The XML file we create can be read from the disk by another application on our server or transmitted to a remote application.
1. Add a new class to your application, name it clsBusinessLayer.cs, and add the statement:
using System.Data;
2. Add two data fields to your class.
dataPath is the path on the server to the App_Data directory or whichever directory the user wants the XML file stored. This will be passed as an argument to the clsBusinessLayer constructor and initialized there.
The data field myDataLayer represents an instance of the data layer. We use this for our database interaction. Like all data fields, it is initialized in the constructor.
Data Fields for the Class clsBusinessLayer
// Add your comments here
string dataPath;
// Add your comments here
clsDataLayer myDataLayer;
3. Our clsBusinessClass constructor serves to initialize all our data fields (a programming best practice).
Constructor for clsBusinessLayer
public clsBusinessLayer(string serverMappedPath)
{
// Add your comments here
dataPath = serverMappedPath;
myDataLayer = new clsDataLayer(dataPath + “Accounts.mdb”);
}
4. The next functionality to add to our business layer is the ability to read (or get) the XML file with customer data ( customers.xml). This method creates a DataSet to hold the customer records parsed from the XML file. It then attempts to read the XML file. If the file is not found, we catch the exception and fix the situation by creating a fresh XML file. We can read the new file and place it in the DataSet object.
Add this code to clsBusinessLayer.cs:
GetCustomerXMLFile Method in clsBusinessLayer.cs
public DataSet GetCustomerXMLFile()
{
// Add your comments here
DataSet xmlDataSet = new DataSet();
try
{
// Add your comments here
xmlDataSet.ReadXml(dataPath+”customers.xml”);
}catch(System.IO.FileNotFoundException error)
{
// Add your comments here
dsAccounts customerListing = myDataLayer.GetAllCustomers();
customerListing.tblCustomers.WriteXml(dataPath+”customers.xml”);
xmlDataSet.ReadXml(dataPath+”customers.xml”);
}
// Add your comments here
return xmlDataSet;
}
5. Our last task with clsBusinessLayer.cs is to add a function that writes the XML data file, customers.xml. To do this, we will read the current list of customers from the Cache object. If you remember, previously, we saved the updated list of customer to the cache as a way to save returning to the database to read customer data. We can read from the cache, which is a much quicker operation.
Add this code to clsBusinessLayer.cs:
WriteCustomerXMLFile Method in clsBusinessLayer.cs
public DataSet WriteCustomerXMLFile(System.Web.Caching.Cache appCache)
{
// Add your comments here
DataSet xmlDataSet = (DataSet) appCache[“CustomerDataSet”];
// Add your comments here
xmlDataSet.WriteXml(dataPath+”customers.xml”);
// Add your comments here
return xmlDataSet;
}
We’ll be able to test this code after we complete the next step.

STEP F: Calling the Business Layer and Binding aGridView
The last task we have is to bind the GridView, gvXML, to the data parsed from the XML file and update the XML file based on data stored in the Cache object.
1. Because our presentation layer class, pgCheckOut, will need to access the business layer frequently, let’s create a data field of type clsBusinessLayer and initialize it in our Page_Load method. This activity is similar to how we handled the clsDataLayer object in our business layer class. Add the following data field to pgCheckOut.aspx.cs:
clsBusinessLayer myBusinessLayer;
Then in the Page_Load, add the following to initialize the data field:
//Add your comments here
// Ensure the argument to MapPath reflects the path
// To where the database is stored. This is also where the
// XML file will be saved. Don’t forget the closing ‘/’
// If you are using Citrix, the directory should be ~/FPDB/
myBusinessLayer = new clsBusinessLayer(Server.MapPath(“~/App_Data/”));
2.We can easily bind the GridView to the data in the XML file by asking the business layer to process the file and handle any errors. We can expect back a DataSet that webind to the GridView. Add the following code to pgCheckOut.aspx.cs:
BindXMLGridView Method in pgCheckOut.aspx.cs
public void BindXMLGridView()
{
// Add your comments here
gvXML.DataSource = myBusinessLayer.GetCustomerXMLFile();
gvXML.DataBind();
}
3. This method needs to be called when the page first loads so that gvXML displays data. Add the following statement to the end of the Page_Load method:
BindXMLGridView();
4. For this lab, we want to update gvXML manually (as opposed to automatically as we do with gvCustomerList). The following click method will be called from the Button btnUpdateXML. It updates the XML from the Cache object and then binds gvXML.
btnUpdateXML_Click Method in pgCheckOut.aspx.cs
public void btnUpdateXML_Click(object sender, EventArgs e)
{
// Add your comments here
gvXML.DataSource = myBusinessLayer.WriteCustomerXMLFile(Cache);
gvXML.DataBind();
}
5. Finally, we need to set the Click event for btnUpdateXML. In the ASP file pgCheckOut.aspx , add the following to the definition for btnUpdateXML :
OnClick=”btnUpdateXML_Click”
6. At this point, you should be able to test the entire application’s functionality.

STEP G: Finalize the Lab.
1. Save your work!
2. Test it! Make changes as appropriate until it works. Try updating or adding new records to the database.
3. Remember to add comments for each step being performed.
4. Please zip and submit the entire web project folder.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.