23.05.2017 Views

CIS 407 DeVry iLab 5 of 7 Latest

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>CIS</strong> <strong>407</strong> <strong>DeVry</strong> <strong>iLab</strong> 5 <strong>of</strong> 7 latest<br />

Downloading is very simple, you can download this Course here:<br />

http://mindsblow.us/question_des/<strong>CIS</strong><strong>407</strong><strong>DeVry</strong><strong>iLab</strong>5<strong>of</strong>7<strong>Latest</strong>/4605<br />

Or<br />

Contact us at:<br />

help@mindblows.us<br />

<strong>CIS</strong> <strong>407</strong> <strong>DeVry</strong> <strong>iLab</strong> 5 <strong>of</strong> 7 <strong>Latest</strong><br />

<strong>CIS</strong><strong>407</strong><br />

<strong>CIS</strong> <strong>407</strong> <strong>DeVry</strong> <strong>iLab</strong> 5 <strong>of</strong> 7 <strong>Latest</strong><br />

<strong>iLab</strong> 5 <strong>of</strong> 7: Transaction Processing (30 Points)<br />

Submit your assignment to the Dropbox located on the silver tab at the top <strong>of</strong> this page.<br />

(See “Due Dates for Assignments & Exams” in the Syllabus for due dates.)<br />

i L A B O V E R V I E W<br />

Scenario/Summary<br />

This week, we will use the .NET OleDbTransaction functions to either commit a set <strong>of</strong> changes to the database, if all <strong>of</strong><br />

them were done correctly, or to roll back all <strong>of</strong> the changes if there was an error in any one <strong>of</strong> them. We will first modify<br />

the code we created last week so that it saves personnel data in the database in two steps; first by inserting a personnel<br />

record for a new employee, and then by updating that record to fill in the start and end dates. This two-step approach<br />

is not really needed in this simple case, but we will use it to simulate a more complex database transaction that would<br />

have to be done in multiple steps, such as one involving more than one table or even more than one database. We will<br />

then see what happens when there is an error in the second operation (the update), allowing a record to be created<br />

containing incomplete information–not a good result! We will fix the problem by wrapping both operations (the insert<br />

and the update) into a single transaction that will be committed (made permanent) only if both operations succeed or<br />

that will be rolled back (undone) if either operation fails. We will also add client-side validation using the ASP.Net<br />

validation controls, and we will allow the user an easy way to edit all employees.<br />

Instructions for Week 5 <strong>iLab</strong>: “Transaction Processing”<br />

Click on the link above to view the tutorial.<br />

Please watch this tutorial before beginning the <strong>iLab</strong>.<br />

The tutorial has audio.<br />

Deliverables<br />

All files are located in the subdirectory <strong>of</strong> the project. The project should function as specified: When you press the<br />

Submit button in frmPersonnel, a record should be saved in the tblPersonnel table containing the FirstName, LastName,<br />

PayRate, StartDate, and EndDate you entered. Test that the transaction will rollback by entering invalid information in<br />

one or more items, such as Hello for a StartDate. Check tht client-side validation works: The ability to edit employees<br />

in a grid is working. Once you have verified that it works, save your website, zip up all files, and submit them to the<br />

Dropbox.<br />

i L A B S T E P S<br />

STEP 1: Modify the clsDataLayer to use a two-step process (10 points)<br />

1. Open Micros<strong>of</strong>t Visual Studio.NET 2008.


2. Click the ASP.NET project called PayrollSystem to open it.<br />

3. Open the clsDataLayer class.<br />

4. Modify the SavePersonnel() function so that instead <strong>of</strong> just doing a single SQL INSERT operation with all the<br />

personnel data, it does an INSERT with only the FirstName and LastName, followed by an UPDATE to save the<br />

PayRate, StartDate, and EndDate into the new record. (This two-step approach is not really necessary here because<br />

we are dealing with only one table, tblPersonnel, but we are doing it to simulate a case with more complex processing<br />

requirements, where we would need to insert or update data in more than one table or maybe even more than one<br />

database.) Find the following existing code in the SavePersonnel() function:<br />

// Add your comments here<br />

strSQL = “Insert into tblPersonnel ” +<br />

“(FirstName, LastName, PayRate, StartDate, EndDate) values (‘” +<br />

FirstName + “‘, ‘” + LastName + “‘, ” + PayRate + “, ‘” + StartDate +<br />

“‘, ‘” + EndDate + “‘)”;<br />

// Add your comments here<br />

command.CommandType = CommandType.Text;<br />

command.CommandText = strSQL;<br />

// Add your comments here<br />

command.ExecuteNonQuery();<br />

5. Modify it so that it reads as follows:<br />

// Add your comments here<br />

strSQL = “Insert into tblPersonnel ” +<br />

“(FirstName, LastName) values (‘” +<br />

FirstName + “‘, ‘” + LastName + “‘)”;<br />

// Add your comments here<br />

command.CommandType = CommandType.Text;<br />

command.CommandText = strSQL;<br />

// Add your comments here<br />

command.ExecuteNonQuery();<br />

// Add your comments here<br />

strSQL = “Update tblPersonnel ” +<br />

“Set PayRate=” + PayRate + “, ” +<br />

“StartDate=’” + StartDate + “‘, ” +<br />

“EndDate=’” + EndDate + “‘ ” +<br />

“Where ID=(Select Max(ID) From tblPersonnel)”;<br />

// Add your comments here<br />

command.CommandType = CommandType.Text;<br />

command.CommandText = strSQL;<br />

// Add your comments here


command.ExecuteNonQuery();<br />

6. Set frmMain as the startup form and run the PayrollSystem Web application to test the changes. When valid data<br />

values are entered for a new employee, things should work exactly as they did before. To test this, enter valid data for<br />

a new employee in frmPersonnel and click Submit. The frmPersonnelVerified form should be displayed with the entered<br />

data values and a message that the record was saved successfully. Click the View Personnel button and check that<br />

the new personnel record was indeed saved to the database and that all the entered data values, including the PayRate,<br />

StartDate, and EndDate, were stored correctly. Close the browser window.<br />

7. Now run the PayrollSystem Web application again, but this time enter some invalid data (a nonnumeric value) in<br />

the PayRate field to cause an error, like this:<br />

Click on image to enlarge.<br />

8. Click here for text description <strong>of</strong> this image.<br />

9. Now when you click Submit, the frmPersonnelVerified form should display a message indicating the record was<br />

not saved:<br />

Click on image to enlarge.<br />

10. Click here for text description <strong>of</strong> this image.<br />

11. However, when you click on the View Personnel button to display the personnel records, you should see that an<br />

incomplete personnel record was in fact created, with missing values for the PayRate, StartDate and EndDate fields:<br />

Click on image to enlarge.<br />

12. Click here for text description <strong>of</strong> this image.<br />

13. This occurred because the Insert statement succeeded but the following Update statement did not. We do not<br />

want to allow this to happen because we end up with incomplete or incorrect data in the database. If the Update<br />

statement fails, we want the Insert statement to be rolled back, or undone, so that we end up with no record at all. We<br />

will fix this by adding transaction code in the next step.<br />

STEP 2: Add transaction code (10 points)<br />

7. In the clsDataLayer.cls class file, add code to the SavePersonnel() function to create a transaction object. Begin<br />

the transaction, commit the transaction if all database operations are successful, and roll back the transaction if any<br />

database operation fails. The following listing shows the complete SavePersonnel() function; the lines you will need to<br />

add are marked with ** NEW ** in the preceding comment and are shown in bold.<br />

// This function saves the personnel data<br />

public static bool SavePersonnel(string Database, string FirstName, string LastName,<br />

string PayRate, string StartDate, string EndDate)<br />

{ bool recordSaved;<br />

// ** NEW ** Add your comments here<br />

OleDbTransaction myTransaction = null;<br />

try<br />

{ // Add your comments here<br />

OleDbConnection conn = new OleDbConnection(“PROVIDER=Micros<strong>of</strong>t.Jet.OLEDB.4.0;” +<br />

“Data Source=” + Database);<br />

conn.Open();<br />

OleDbCommand command = conn.CreateCommand();<br />

string strSQL;<br />

// ** NEW ** Add your comments here


myTransaction = conn.BeginTransaction();<br />

command.Transaction = myTransaction;<br />

// Add your comments here<br />

strSQL = “Insert into tblPersonnel ” +<br />

“(FirstName, LastName) values (‘” +<br />

FirstName + “‘, ‘” + LastName + “‘)”;<br />

// Add your comments here<br />

command.CommandType = CommandType.Text;<br />

command.CommandText = strSQL;<br />

// Add your comments here<br />

command.ExecuteNonQuery();<br />

// Add your comments here<br />

strSQL = “Update tblPersonnel ” +<br />

“Set PayRate=” + PayRate + “, ” +<br />

“StartDate=’” + StartDate + “‘, ” +<br />

“EndDate=’” + EndDate + “‘ ” +<br />

“Where ID=(Select Max(ID) From tblPersonnel)”;<br />

// Add your comments here<br />

command.CommandType = CommandType.Text;<br />

command.CommandText = strSQL;<br />

// Add your comments here<br />

command.ExecuteNonQuery();<br />

// ** NEW ** Add your comments here<br />

myTransaction.Commit();<br />

// Add your comments here<br />

conn.Close();<br />

recordSaved = true;}<br />

catch (Exception ex)<br />

{// ** NEW ** Add your comments here<br />

myTransaction.Rollback();<br />

recordSaved = false;}<br />

return recordSaved;}<br />

8. Run your Web application. First, enter valid data in all the fields <strong>of</strong> frmPersonnel. When you press the Submit<br />

button in frmPersonnel, a record should be saved in the tblPersonnel table containing the FirstName, LastName,<br />

PayRate, StartDate, and EndDate. With valid data entered in all the items, the “successfully saved” message should<br />

appear indicating that the transaction was committed.<br />

Click on image to enlarge.


9.Click here for text description <strong>of</strong> this image.<br />

10. Click the View Personnel button and verify that the new record was in fact added to the database table correctly.<br />

Click on image to enlarge.<br />

11.Click here for text description <strong>of</strong> this image.<br />

12. Now close the browser, run the Web application again, and this time test that the transaction will roll back after<br />

entering incorrect information. On the frmPersonnel form, enter invalid data for PayRate and click Submit. The “not<br />

saved” message should appear, which indicates that the transaction was rolled back.<br />

Click on image to enlarge.<br />

13. Click here for text description <strong>of</strong> this image.<br />

14. Click the View Personnel button and verify that this time, as desired, an incomplete record was not added to the<br />

database table.<br />

Click on image to enlarge.<br />

15. Click here for text description <strong>of</strong> this image.<br />

16. You have seen how we used the try/catch block to catch an unexpected error. You may have noticed that if you<br />

enter bad data for the dates, an exception is thrown. Go back to the validation code you added in the frmPersonnel<br />

code and add a try/catch with logic to prevent an invalid date from causing a server error.<br />

17. In the Week 3 and Week 5 labs, you learned how to validate code once the page was posted back to the server.<br />

There is some validation that must be done on the server because it requires server resources such as the database.<br />

Some validation can also be done on the client. If you can do validation on the client it saves a round trip to the server,<br />

which will improve performance. In this approach, we will check values before the page is submitted to the server for<br />

processing. Normally, there is a combination <strong>of</strong> server and client validation used in a web application. ASP.Net includes<br />

validation controls which will use JavaScript on the client to perform validation. You will find these controls in the<br />

Validation group in the toolbox.<br />

18. Add validation controls to the frmPersonnel form as follows: For the first and last name, make sure each field has<br />

data in it. Use the RequiredFieldValidator for this. Add the control to the right <strong>of</strong> the text box you are validating. The<br />

location <strong>of</strong> the validator control is where the error message (if there is one) will appear for the control you link the<br />

validator to. You will be adding one validator control for each text box you want to validate. Remember to set the<br />

ControlToValidate and ErrorMessage properties on the validator control. Making this change eliminates the need for<br />

the server-side check you were doing previously. Use a regular expression validator to check that the start and end<br />

date are in the correct format.<br />

Click on image to enlarge.<br />

19. Click here for text description <strong>of</strong> this image.<br />

20. Remove the View Personnel and Cancel buttons from the frmPersonnel form as they will cause a Postback and<br />

invoke the client-side editing you just added. The user is able to get to the View Personnel from the main form and from<br />

the personnel verification screen, so there is no need for these buttons now.<br />

21. Because you have entered data in this lab that is invalid and those partial records are in the database, you will<br />

need to add the ability to remove or update data. Add a new main form option called Edit Employees. Add the link and<br />

image for this. This option will take the user to a new form called frmEditPersonnel.<br />

frmMain with links added<br />

22. Click here for text description <strong>of</strong> this image.<br />

23. Add the new form frmEditPersonnel. On frmEditPersonnel, add the CoolBiz log at the top <strong>of</strong> the form. Add a label<br />

that says “Edit Employees.” Add a GridView control with an ID <strong>of</strong> grdEditPersonnel.<br />

24. You will now add a SQLDataSource to the page. You will be using a databound grid for this form unlike the<br />

previous grids, in which you added as unbound (in the designer).<br />

25. Add a new SQLDataSource control to the frmEditPersonnel in the design view. This is not a visible control; that<br />

is, it will only appear in design view but the user will never see it. Note: If you change the folder name or location <strong>of</strong>


your database, you will need to reconfigure the data source (right-click on the data source control and select the<br />

“Configure Data Source” option.<br />

26. There is a small > indicator in the design view <strong>of</strong> the SQL Data Source control you added if the configuration<br />

menu is collapsed (press it to open the menu), or there is a < with the menu displayed. From the data source menu,<br />

select "Configure Data Source."<br />

27. Press the New Connection button and select the database.<br />

28. Press the Next button.<br />

29. When asked if you want to save the connection in the application configuration file, check the Yes check box and<br />

press Next.<br />

30. Select the tblPersonnel table.<br />

31. Select all columns (you can use the * for this).<br />

32. Press the Advanced button and check the Generate Insert, Update, and Delete option and press the OK button.<br />

33. Press the Next button.<br />

34. Press the Test Query button and make sure everything works as it is supposed to. If it does not repeat the above<br />

steps to make sure you did everything properly. Press the Finish button.<br />

7. Click on the grid you added in the design view and expand the properties menu (the little > in the upper right <strong>of</strong> the<br />

control). Choose the data source you just added. On the GridView tasks menu, select Edit columns. Add an Edit,<br />

Update, and Cancel Command field. Add a Delete Command field. Press OK. You can now test the grid, which is a<br />

fully functioning Update and Delete grid. Try it out!<br />

Click on image to enlarge.<br />

8.<br />

Click here for text description <strong>of</strong> this image.<br />

9. Hints:<br />

Make sure you reestablish your database connection if you copied the files from a previous lab.<br />

In order to keep the validation controls from causing wrapping, you may want to increase the Panel width.<br />

A regular expression for mm/dd/yyyy is this:<br />

^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)dd$<br />

Experiment with the editable grid and command buttons for different display styles.<br />

STEP 3: Test and submit (10 points)<br />

29. Once you have verified that everything works as it is supposed to, save your project, zip up all files, and submit<br />

in the Dropbox.<br />

NOTE: Make sure you include comments in the code provided where specified (where the ” // Your comments here” is<br />

mentioned) and for any code you write, or else a five point deduction per item (form, class, function) will be made.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!