Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Cis 407 i lab 5 of 7


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Cis 407 i lab 5 of 7

  1. 1. HELPIDO.COMCLICK HERE TO GET THE SOLUTION !!!!!!CIS 407 A – ILAB 5 OF 7STEP 1: Modify the clsDataLayer to use a two-step process (10 points)1. Open Microsoft Visual Studio.NET 2008.2. Click the ASP.NET project called PayrollSystem to open it.3. Open the clsDataLayer class.4. Modify the SavePersonnel() function so that instead of just doing a single SQL INSERT operationwith all the personnel data, it does an INSERT with only the FirstName and LastName, followedby an UPDATE to save the PayRate, StartDate, and EndDate into the new record. (This two-stepapproach is not really necessary here because we are dealing with only one table, tblPersonnel,but we are doing it to simulate a case with more complex processing requirements, where wewould need to insert or update data in more than one table or maybe even more than onedatabase.) Find the following existing code in the SavePersonnel() function:// Add your comments herestrSQL = "Insert into tblPersonnel " +"(FirstName, LastName, PayRate, StartDate, EndDate) values (" +FirstName + ", " + LastName + ", " + PayRate + ", " + StartDate +", " + EndDate + ")";// Add your comments herecommand.CommandType = CommandType.Text;command.CommandText = strSQL;// Add your comments herecommand.ExecuteNonQuery();5.Modify it so that it reads as follows:// Add your comments herestrSQL = "Insert into tblPersonnel " +"(FirstName, LastName) values (" +FirstName + ", " + LastName + ")";// Add your comments herecommand.CommandType = CommandType.Text;
  2. 2. command.CommandText = strSQL;// Add your comments herecommand.ExecuteNonQuery();// Add your comments herestrSQL = "Update tblPersonnel " +"Set PayRate=" + PayRate + ", " +"StartDate=" + StartDate + ", " +"EndDate=" + EndDate + " " +"Where ID=(Select Max(ID) From tblPersonnel)";// Add your comments herecommand.CommandType = CommandType.Text;command.CommandText = strSQL;// Add your comments herecommand.ExecuteNonQuery();6. Set frmMain as the startup form and run the PayrollSystem Web application to test the changes.When valid data values are entered for a new employee, things should work exactly as they didbefore. To test this, enter valid data for a new employee in frmPersonnel and click Submit. ThefrmPersonnelVerified form should be displayed with the entered data values and a message thatthe record was saved successfully. Click the View Personnel button and check that the newpersonnel record was indeed saved to the database and that all the entered data values,including the PayRate, StartDate, and EndDate, were stored correctly. Close the browserwindow.7. Now run the PayrollSystem Web application again, but this time enter some invalid data (anonnumeric value) in the PayRate field to cause an error, like this:Click on image toenlarge.frmPersonnel WithBad Data8.Click here for text description of this image.9. Now when you click Submit, the frmPersonnelVerified form should display a message indicatingthe record was not saved:Click on image toenlarge.frmPersonnel VerifiedWith Error Message
  3. 3. 10.Click here for text description of this image.11. However, when you click on the View Personnel button to display the personnel records, youshould see that an incomplete personnel record was in fact created, with missing values for thePayRate, StartDate and EndDate fields:Click on image toenlarge.Incomplete PersonnelRecord12.Click here for text description of this image.13. This occurred because the Insert statement succeeded but the following Update statement didnot. We do not want to allow this to happen because we end up with incomplete or incorrect datain the database. If the Update statement fails, we want the Insert statement to be rolled back, orundone, so that we end up with no record at all. We will fix this by adding transaction code in thenext step.STEP 2: Add transaction code (10 points)7. In the clsDataLayer.cls class file, add code to the SavePersonnel() function to create atransaction object. Begin the transaction, commit the transaction if all database operations aresuccessful, and roll back the transaction if any database operation fails. The following listingshows the complete SavePersonnel() function; the lines you will need to add are marked with **NEW ** in the preceding comment and are shown in bold.// This function saves the personnel datapublic static boolSavePersonnel(string Database, string FirstName, string LastName,string PayRate, string StartDate, string EndDate){boolrecordSaved;// ** NEW ** Add your comments hereOleDbTransactionmyTransaction = null;try{// Add your comments hereOleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" + Database);conn.Open();OleDbCommand command = conn.CreateCommand();string strSQL;// ** NEW ** Add your comments heremyTransaction = conn.BeginTransaction();
  4. 4. command.Transaction = myTransaction;// Add your comments herestrSQL = "Insert into tblPersonnel " +"(FirstName, LastName) values (" +FirstName + ", " + LastName + ")";// Add your comments herecommand.CommandType = CommandType.Text;command.CommandText = strSQL;// Add your comments herecommand.ExecuteNonQuery();// Add your comments herestrSQL = "Update tblPersonnel " +"Set PayRate=" + PayRate + ", " +"StartDate=" + StartDate + ", " +"EndDate=" + EndDate + " " +"Where ID=(Select Max(ID) From tblPersonnel)";// Add your comments herecommand.CommandType = CommandType.Text;command.CommandText = strSQL;// Add your comments herecommand.ExecuteNonQuery();// ** NEW ** Add your comments heremyTransaction.Commit();// Add your comments hereconn.Close();recordSaved = true;}catch (Exception ex){// ** NEW ** Add your comments heremyTransaction.Rollback();recordSaved = false;}return recordSaved;}
  5. 5. 8. Run your Web application. First, enter valid data in all the fields of frmPersonnel. When you pressthe Submit button in frmPersonnel, a record should be saved in the tblPersonnel table containingthe FirstName, LastName, PayRate, StartDate, and EndDate. With valid data entered in all theitems, the "successfully saved" message should appear indicating that the transaction wascommitted.Click on image toenlarge.frmPersonnelVerifiedAfter Commit9.Click here for text description of this image.10. Click the View Personnel button and verify that the new record was in fact added to the databasetable correctly.Click on image toenlarge.frmViewPersonnelAfter Commit11.Click here for text description of this image.12. Now close the browser, run the Web application again, and this time test that the transaction willroll back after entering incorrect information. On the frmPersonnel form, enter invalid data forPayRate and click Submit. The "not saved" message should appear, which indicates that thetransaction was rolled back.Click on image toenlarge.frmPersonnel VerifiedAfter Rollback13.Click here for text description of this image.14. Click the View Personnel button and verify that this time, as desired, an incomplete record wasnot added to the database table.Click on image toenlarge.
  6. 6. frmViewPersonnelAfter Rollback15.Click here for text description of this image.16. You have seen how we used the try/catch block to catch an unexpected error. You may havenoticed that if you enter bad data for the dates, an exception is thrown. Go back to the validationcode you added in the frmPersonnel code and add a try/catch with logic to prevent an invalid datefrom causing a server error.17. In the Week 3 and Week 5 labs, you learned how to validate code once the page was postedback to the server. There is some validation that must be done on the server because it requiresserver resources such as the database. Some validation can also be done on the client. If youcan do validation on the client it saves a round trip to the server, which will improve performance.In this approach, we will check values before the page is submitted to the server for processing.Normally, there is a combination of server and client validation used in a web application.ASP.Net includes validation controls which will use JavaScript on the client to perform validation.You will find these controls in the Validation group in the toolbox.18. Add validation controls to the frmPersonnel form as follows: For the first and last name, makesure each field has data in it. Use the RequiredFieldValidator for this. Add the control to the rightof the text box you are validating. The location of the validator control is where the error message(if there is one) will appear for the control you link the validator to. You will be adding onevalidator control for each text box you want to validate. Remember to set the ControlToValidateand ErrorMessage properties on the validator control. Making this change eliminates the need forthe server-side check you were doing previously. Use a regular expression validator to check thatthe start and end date are in the correct format.Click on image toenlarge.frmPersonnelValidation Controls19.Click here for text description of this image.20. Remove the View Personnel and Cancel buttons from the frmPersonnel form as they will cause aPostback and invoke the client-side editing you just added. The user is able to get to the ViewPersonnel from the main form and from the personnel verification screen, so there is no need forthese buttons now.21. Because you have entered data in this lab that is invalid and those partial records are in thedatabase, you will need to add the ability to remove or update data. Add a new main form optioncalled Edit Employees. Add the link and image for this. This option will take the user to a newform called frmEditPersonnel.Click on image toenlarge.
  7. 7. frmMain with linksadded22.Click here for text description of this image.23. Add the new form frmEditPersonnel. On frmEditPersonnel, add the CoolBiz log at the top of theform. Add a label that says "Edit Employees." Add a GridView control with an ID ofgrdEditPersonnel.24. You will now add a SQLDataSource to the page. You will be using a databound grid for this formunlike the previous grids, in which you added as unbound (in the designer).25. Add a new SQLDataSource control to the frmEditPersonnel in the design view. This is not avisible control; that is, it will only appear in design view but the user will never see it. Note: If youchange the folder name or location of your database, you will need to reconfigure the data source(right-click on the data source control and select the "Configure Data Source" option.26. There is a small > indicator in the design view of the SQL Data Source control you added if theconfiguration menu is collapsed (press it to open the menu), or there is a < with the menudisplayed. From the data source menu, select "Configure Data Source."27. Press the New Connection button and select the database.28. Press the Next button.29. When asked if you want to save the connection in the application configuration file, check the Yescheck box and press Next.30. Select the tblPersonnel table.31. Select all columns (you can use the * for this).32. Press the Advanced button and check the Generate Insert, Update, and Delete option and pressthe OK button.33. Press the Next button.34. Press the Test Query button and make sure everything works as it is supposed to. If it does notrepeat the above steps to make sure you did everything properly. Press the Finish button.7. Click on the grid you added in the design view and expand the properties menu (the little > in theupper right of the control). Choose the data source you just added. On the GridView tasks menu,select Edit columns. Add an Edit, Update, and Cancel Command field. Add a Delete Commandfield. Press OK. You can now test the grid, which is a fully functioning Update and Delete grid. Tryit out!Click on image toenlarge.Edit Employees8.Click here for text description of this image.9. Hints:Make sure you reestablish your database connection if you copied the files from a previous lab.In order to keep the validation controls from causing wrapping, you may want to increase thePanel width.
  8. 8. A regular expression for mm/dd/yyyy is this:^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)dd$Experiment with the editable grid and command buttons for different display styles.STEP 3: Test and submit (10 points)29. Once you have verified that everything works as it is supposed to, save your project, zip up allfiles, and submit in the Dropbox.NOTE: Make sure you include comments in the code provided where specified (where the " //Your comments here" is mentioned) and for any code you write, or else a five point deduction peritem (form, class, function) will be made.CLICK HERE TO GET THE SOLUTION !!!!!!