Modify the clsdatalayer to use a two-step process

Assignment Help Database Management System
Reference no: EM13765111

Transaction Processing Assignment description:

STEP 1: Modify the clsDataLayer to use a two-step process

Open Microsoft Visual Studio.NET 2008.

Click the ASP.NET project called PayrollSystem to open it.

Open the clsDataLayer class.

Modify the SavePersonnel() function so that instead of just doing a single SQL INSERT operation with all the personnel data, it does an INSERT with only the FirstName and LastName, followed by an UPDATE to save the PayRate, StartDate, and EndDate into the new record. (This two-step approach 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 we would need to insert or update data in more than one table or maybe even more than one database.) Find the following existing code in the SavePersonnel() function:

// Add your comments here

strSQL = "Insert into tblPersonnel " +

"(FirstName, LastName, PayRate, StartDate, EndDate) values ('" +

FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate +

"', '" + EndDate + "')";

// Add your comments here

command.CommandType = CommandType.Text;

command.CommandText = strSQL;

// Add your comments here

command.ExecuteNonQuery();

Modify it so that it reads as follows:

// Add your comments here

strSQL = "Insert into tblPersonnel " +

"(FirstName, LastName) values ('" +

FirstName + "', '" + LastName + "')";

// Add your comments here

command.CommandType = CommandType.Text;

command.CommandText = strSQL;

// Add your comments here

command.ExecuteNonQuery();

// Add your comments here

strSQL = "Update tblPersonnel " +

"Set PayRate=" + PayRate + ", " +

"StartDate='" + StartDate + "', " +

"EndDate='" + EndDate + "' " +

"Where ID=(Select Max(ID) From tblPersonnel)";

// Add your comments here

command.CommandType = CommandType.Text;

command.CommandText = strSQL;

// Add your comments here

command.ExecuteNonQuery();

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 did before. To test this, enter valid data for a new employee in frmPersonnel and click Submit. The frmPersonnelVerified form should be displayed with the entered data values and a message that the record was saved successfully. Click the View Personnel button and check that the new personnel 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 browser window.
Now run the PayrollSystem Web application again, but this time enter some invalid data (a nonnumeric value) in the PayRate field to cause an error, like this:

Now when you click Submit, the frmPersonnelVerified form should display a message indicating the record was not saved:

However, when you click on the View Personnel button to display the personnel records, you should see that an incomplete personnel record was in fact created, with missing values for the PayRate, StartDate and EndDate fields:

This occurred because the Insert statement succeeded but the following Update statement did not. We do not want to allow this to happen because we end up with incomplete or incorrect data in the database. If the Update statement fails, we want the Insert statement to be rolled back, or undone, so that we end up with no record at all. We will fix this by adding transaction code in the next step.

STEP 2: Add transaction code

In the clsDataLayer.cls class file, add code to the SavePersonnel() function to create a transaction object. Begin the transaction, commit the transaction if all database operations are successful, and roll back the transaction if any database operation fails. The following listing shows 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 data

public static bool SavePersonnel(string Database, string FirstName, string LastName,

string PayRate, string StartDate, string EndDate)

{

bool recordSaved;

// ** NEW ** Add your comments here

OleDbTransaction myTransaction = null;

try

{

// Add your comments here

OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=" + Database);

conn.Open();

OleDbCommand command = conn.CreateCommand();

string strSQL;

// ** NEW ** Add your comments here

myTransaction = conn.BeginTransaction();

command.Transaction = myTransaction;

// Add your comments here

strSQL = "Insert into tblPersonnel " +

"(FirstName, LastName) values ('" +

FirstName + "', '" + LastName + "')";

// Add your comments here

command.CommandType = CommandType.Text;

command.CommandText = strSQL;

// Add your comments here

command.ExecuteNonQuery();

// Add your comments here

strSQL = "Update tblPersonnel " +

"Set PayRate=" + PayRate + ", " +

"StartDate='" + StartDate + "', " +

"EndDate='" + EndDate + "' " +

"Where ID=(Select Max(ID) From tblPersonnel)";

// Add your comments here

command.CommandType = CommandType.Text;

command.CommandText = strSQL;

// Add your comments here

command.ExecuteNonQuery();

// ** NEW ** Add your comments here

myTransaction.Commit();

// Add your comments here

conn.Close();

recordSaved = true;

}

catch (Exception ex)

{

// ** NEW ** Add your comments here

myTransaction.Rollback();

recordSaved = false;

}

return recordSaved;

}

Run your Web application. First, enter valid data in all the fields of frmPersonnel. When you press the Submit button in frmPersonnel, a record should be saved in the tblPersonnel table containing the FirstName, LastName, PayRate, StartDate, and EndDate. With valid data entered in all the items, the "successfully saved" message should appear indicating that the transaction was committed.

Click the View Personnel button and verify that the new record was in fact added to the database table correctly.

Now close the browser, run the Web application again, and this time test that the transaction will roll back after entering incorrect information.

On the frmPersonnel form, enter invalid data for PayRate and click Submit. The "not saved" message should appear, which indicates that the transaction was rolled back.

Click the View Personnel button and verify that this time, as desired, an incomplete record was not added to the database table.

You have seen how we used the try/catch block to catch an unexpected error. You may have noticed that if you enter bad data for the dates, an exception is thrown. Go back to the validation code you added in the frmPersonnel code and add a try/catch with logic to prevent an invalid date from causing a server error.

In the Week 3 and Week 5 labs, you learned how to validate code once the page was posted back to the server. There is some validation that must be done on the server because it requires server resources such as the database. 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, 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.

Add validation controls to the frmPersonnel form as follows: For the first and last name, make sure each field has data in it. Use the RequiredFieldValidator for this. Add the control to the right of 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 one validator control for each text box you want to validate. Remember to set the ControlToValidate and ErrorMessage properties on the validator control. Making this change eliminates the need for the server-side check you were doing previously. Use a regular expression validator to check that the start and end date are in the correct format.

Remove the View Personnel and Cancel buttons from the frmPersonnel form as they will cause a Postback and invoke the client-side editing you just added. The user is able to get to the View Personnel from the main form and from the personnel verification screen, so there is no need for these buttons now.

Because you have entered data in this lab that is invalid and those partial records are in the database, you will need to add the ability to remove or update data. Add a new main form option called Edit Employees. Add the link and image for this. This option will take the user to a new form called frmEditPersonnel.

Add the new form frmEditPersonnel. On frmEditPersonnel, add the CoolBiz log at the top of the form. Add a label that says "Edit Employees."

Add a GridView control with an ID of grdEditPersonnel.

You will now add a SQLDataSource to the page. You will be using a databound grid for this form unlike the previous grids, in which you added as unbound (in the designer).

Add a new SQLDataSource control to the frmEditPersonnel in the design view. This is not a visible control; that is, it will only appear in design view but the user will never see it. Note: If you change 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.

There is a small > indicator in the design view of the SQL Data Source control you added if the configuration menu is collapsed (press it to open the menu), or there is a < with the menu displayed. From the data source menu, select "Configure Data Source."

Press the New Connection button and select the database.

Press the Next button.

When asked if you want to save the connection in the application configuration file, check the Yes check box and press Next.

Select the tblPersonnel table.

Select all columns (you can use the * for this).

Press the Advanced button and check the Generate Insert, Update, and Delete option and press the OK button.

Press the Next button.

Press the Test Query button and make sure everything works as it is supposed to. If it does not repeat the above steps to make sure you did everything properly. Press the Finish button.

Click on the grid you added in the design view and expand the properties menu (the little > in the upper 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 Command field. Press OK. You can now test the grid, which is a fully functioning Update and Delete grid. Try it out!

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 the Panel width.

Reference no: EM13765111

Questions Cloud

Performance appraisal methods : Compare and contrast the following methods of job evaluation: ranking, classification, factor comparison, and point method. Which of these do you feel is most useful and why
Explain the top five reasons private companies : Identify and explain the top five reasons private companies go public. Explain information the firm is required to provide to the investor with complete transparency.
Evaluating the constitutional safeguards : Write a 1,400- to 1,750-word paper identifying and evaluating the constitutional safeguards provided by the 4th, 5th, and 6th Amendments to the United States Constitution as they apply to both adult and juvenile court proceedings
What reason do gramps father give sal for going on the trip : In the book Walk Two Moons by Sharon Creech, what reason do Gramps and Sal's father give Sal for going on the trip? Accourding to Sal, what are the real reasons? Why might the reason have been left unspoken?
Modify the clsdatalayer to use a two-step process : Modify the SavePersonnel() function so that instead of just doing a single SQL INSERT operation with all the personnel data, it does an INSERT with only the FirstName and LastName, followed by an UPDATE to save the PayRate, StartDate, and EndDate ..
Did the tool recover the deleted files : Did the tool recover the deleted files? How does data recovery differ from computer forensics
Charts and conditional formatting : Explain what you believe to be the main business purpose of charts in Excel. Next, discuss whether or not you believe Excel would still be a useful tool if the charting features were removed from the application. Justify your response.
Challenge computer forensic examiners on the integrity : Hashing is very important in computer forensics. Defense experts often challenge computer forensic examiners on the integrity of their evidence
Predict the break even point for prodcut : a. Predict the break even point for Prodcut A in terms of [a] units and b dollars of sales. b. Prepare an income statement showing sales, fixed costs, and variable costs for product A at the break even point.

Reviews

Write a Review

Database Management System Questions & Answers

  Research paper on the turing and von neumann models

Write a two page research paper on the Turing and von Neumann models.

  Build an entity relationship model for the above scenario

Draw a DFD (Context and Level 1) for placing an order based on the E-R diagram shown here.

  Explain how you would formulate an sql injection attack

Explain how you would formulate an SQL injection attack against an e-Commerce server such as Amazon

  Imagine that you have been assigned as a project manager to

imagine that you have been assigned as a project manager to manage your companys e-commerce website where customers can

  Display customer id from the order table

write a query to display customer id from the order table associated with order id in given database.

  Process the weather data with data mining techniques

Process the weather data for Auckland and Invercargill in the given dataset (monthly readings) and-focusing in particular on temperature related data-experiment with various data mining techniques

  What messages are sent if not well-formed

Create new DOM documents for each of your .xml file and .xsl file. Use these to generate output to the browser.

  Find name and membership number of members

Find the name and membership number of members who have borrowed more than five different books of that publisher.

  Advantages and disadvantages of downloading software

Write a 700- to 1,050-word paper describing the steps involved in downloading files and programs from the Internet.

  In your opinion what are the three biggest challenges in

in your opinion what are the three biggest challenges in planning and designing a solution for a programming

  Define security threats as it relate to the operating system

Discuss security threats as it relate to the Operating Systems and Memory Management. Consider a fixed partitioning scheme with equal-size partitions of 2 16 bytes and a total main memory size of 2^24 bytes. A process table is maintained that inc..

  Draw the er diagram above scenario by illustrating the

Consider the following requirements for an airline ticketsbooking system. The system is composed of manyairlines. Each airline is identified by a code, name and headquarter’s address. An airline has different mileprograms, identified by a type- code,..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd