This is the ninth and final part of the solution for Papa Bob's Mega Challenge. In this part, we're going to add the OrderManagement page and display the Orders via a GridView.
The requirements for this challenge specify that a page should be added that allows the employees of Papa Bob's to see the uncompleted orders in a GridView. In order to do this, we'll need to add a separate Web Form in the Presentation layer called OrderManagement:
Inside OrderManagement.aspx, we'll need to add a header and a GridView control called 'ordersGridView':
This GridView will need to be populated with a list of orders passed from the Domain layer. To create this functionality, navigate to OrderManager.cs and create a new method called GetOrders(). Because the orders themselves are stored in the Persistence layer, this method will return the result of a GetOrders() method in the Persistence layer:
Generate a method stub for GetOrders() in the Persistence layer and navigate to it. Inside, write the following code to get a reference to the orders stored in the database:
Now that we have a reference to the original set of orders, we need to convert them to a list of OrderDTO orders. To do that, create a new helper method called convertToDTO() that returns this list:
Because a List<\Order> is supplied, we can use a foreach() loop to iterate through that collection and convert each individual entry to a DTO object:
Inside this loop, create a new DTO.OrderDTO object and populate its properties with the properties of the original order, then and add it to the ordersDTO collection:
Finally, return ordersDTO at the end of the method. Return to the GetOrders() method and create a new variable called ordersDTO. Assign that variable the returned value of the convertToDTO() method:
Part of the requirement of this challenge is to create a button inside the GridVie that the workers of Papa Bob's can press to hide (but not delete) the orders that have been completed. These orders remain in the database, but are no longer displayed on the GridView. To create this functionality, we'll add a new property to the Orders table. Call this property 'Completed', and give it a bit value type defaulted to 0:
Update the database to commit the changes. Then, navigate to PapaBobsEntities.edmx in the Persistence layer. Right-click anywhere and select 'Update Model from Database' and choose to refresh the Orders Table:
This will add the Completed property to the Entity Model of Order. We'll need to also add this property to the OrderDTO class:
Return to the GetOrders() method in the OrderRepository and make the following changes to filter the orders that will be returned:
This simply tells the compiler only to gather the orders that have a Completed value set to false. This way, no completed orders will make it to the GridView in OrderManagement.
Navigate to OrderManagement.aspx.cs and write the following code to create a variable called orders, set to the returned value of the Domain layer's GetOrders() method. Then, set that variable as the GridView's data source:
Run your application, submit some orders, then navigate to OrderManagement.aspx to see the following result:
The final step in this challenge is to create a hyperlink button that, when clicked, completes the order, hiding it from the GridView. To begin, go to OrderManagement.aspx and in the Design view, click on the GridView and select 'Edit Columns...':
From the resulting dialog, add a ButtonField with its text property set to "Complete":
Now, in the Properties window of the GridView, double click on RowCommand to generate a new event:
Navigate to the generated RowCommand event and type in the following code to determine which row's button was clicked:
This code converts the event argument (which row number) to an integer and stores the value in an index. Then, that row from the ordersGridView is assigned to a new GridViewRow object so we can reference it directly.
Next, we need to determine the OrderID of the order belonging to that row so we can pass it to the database. We'll do this by accessing the second cell of that row (which has the OrderId) and assigning that to a new variable:
We'll now parse this value back into a GUID and pass it to the Domain layer in a new method called CompleteOrder():
Generate a method stub for CompleteOrder() and locate it within the OrderManager class. Within that method, make a call to a new method in the OrderRepository called CompleteOrder() passing in the orderId:
Generate a method stub once again and locate it within the OrderRepository. This method will create a new instance of PapaBobsDbEntities and locate the order located in the Orders table with the matching orderId:
Once the order is located, it will set its Completed value to true and save the changes to the database:
Now, return to OrderManagement.aspx.cs to ensure that the page refreshes after the button has been pressed. To do this, cut and paste the code from the Page_Load into a new method called refreshGridView():
Finally, call this method in both the RowCommand and Page_Load to get the GridView data the first time the page loads and each time an order is completed:
Save and run your project, add some orders and navigate to OrderManagement to see the working result:
This concludes the entire solution of Papa Bob's Mega Challenge. This was by far the longest and most detailed of all the challenges in this course. If you had difficulty completing this challenge, come back to it later and try to implement the concepts applied in this solution. Make it your own and do your best. Continue struggling with it until you can successfully complete it. You'll learn a lot in the process. Upon completing this challenge, you've successfully completed the C# Fundamentals Via ASP.NET Web Forms course! Congratulations on creating your very own website! Continue using these concepts and build upon them to continue learning new skills as a web developer. You've done a great job in this course, keep it up!
Papa Bob's Mega Solution Part 9 - Creating the Order Management Page