iLab 4 of 7: Create a Business Layer Class and Use an XML File
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!
Submit your assignment to the Dropbox, located at the top of this page. For instructions on how to use the Dropbox, read these step-by-step instructions.
(See the Syllabus section “Due Dates for Assignments & Exams” for due dates.)
Please zip and submit the entire web project folder to the assignment Dropbox.
Visual Studio 2012
Set up the Visual Studio development environment and test the web server on your machine. More information on how you can get the most recent version of VS is found in Course Home.
Access the software at https://lab.devry.edu.
Microsoft Access is required for this lab.
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 iLab 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:
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 XML file 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:
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:
1. When you have completed this step, the design view of pgCheckOut.aspx should be similar to this image:
STEP D: Bind the First GridView in pgCheckOut.aspx.cs
Now that we have a method in our data layer that returns all the customers in a DataSet object, 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:
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_Clickmethods in pgCheckOut.aspx.cs.
1. 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 filecustomers.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:
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.
Our clsBusinessClass constructor serves to initialize all our data fields (a programming best practice).
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:
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:
1. We’ll be able test this code after we complete the next step.
STEP F: Calling the Business Layer and Binding a GridView
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:
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 we bind to the GridView.
3. Add the following code to pgCheckOut.aspx.cs:
4. 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:
5. For this lab, we want to update gvXML manually (as opposed to automatically as we do withgvCustomerList). The following click method will be called from the Button btnUpdateXML. It updates the XML from the Cache object and then binds gvXML.
6. Finally, we need to set the Click event for btnUpdateXML. In the ASP file pgCheckOut.aspx, add the following to thedefinition for btnUpdateXML:
7. At this point, you should be able to test the entire application’s functionality.
STEP G: Finalize the iLab.
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 to the assignment Dropbox.
* You can also use 2CO option if you want to purchase through Credit Cards/Paypal but make sure you put the correct billing information otherwise you wont be able to receive any download link.
* Your paypal has to be pre-loaded in order to complete the purchase or otherwise please discuss it with us at firstname.lastname@example.org.
* As soon as the payment is received, download link of the solution will automatically be sent to the address used in selected payment method.
* Please check your junk mails as the download link email might go there and please be patient for the download link email. Sometimes, due to server congestion, you may receive download link with a delay.
* All the contents are compressed in one zip folder.
* In case if you get stuck at any point during the payment process, please immediately contact us at email@example.com and we will fix it with you.
* We try our best to reach back to you on immediate basis. However, please wait for atleast 8 hours for a response from our side. Afterall, we are humans.
* Comments/Feedbacks are truely welcomed and there might be some incentives for you for the next lab/quiz/assignment.
* In case of any query, please donot hesitate to contact us at firstname.lastname@example.org.
* MOST IMPORTANT Please use the tutorials as a guide and they need NOT to be used for any submission. Just take help from the material.
******************************************** Good Luck ***************************************************
Any personal information received will only be used to fill your order. We will not sell or redistribute your information to anyone.
We will try our best to resolve the issue and if still persists we can discuss for a refund in case its required.