+ All Categories
Home > Documents > Lab Steps - f01.justanswer.com

Lab Steps - f01.justanswer.com

Date post: 24-Mar-2022
Category:
Upload: others
View: 14 times
Download: 0 times
Share this document with a friend
17
1/21/2016 CIS407A Week 3 iLab (SEP15) http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 4/20 Lab Steps 1. Open Microsoft Visual Studio.NET. 2. Open the PayrollSystem website by clicking on it in the Recent Projects list, or by pulling down the File menu, selecting Open Website, navigating to the folder where you previously saved the PayrollSystem, and clicking Open. 3. Download the PayrollSystem_DB.accdb file from Doc Sharing and save it on your local computer. (Note: your operating system may lock or block the file. Once you have copied it locally, right click on the file and select Properties and then Unblock if available). Then add it to the PayrollSystem website as follows: In Visual Studio, in the Solution Explorer click Website, Add Existing Item, then navigate to the PayrollSystem_DB.accdb file you downloaded, and click the Add button. Make sure you select file types, which include *.accdb, *.accdb, etc. Otherwise, you will not be able to see the database file to select. 4. Now we need to create a new connection to the PayrollSystem_DB.accdb. To begin, click View Server Explorer. 5. When the Server Explorer toolbox appears, click the Connect to Database button. STEP 1: Step Title
Transcript
Page 1: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 4/20

L a b S t e p s

1. Open Microsoft Visual Studio.NET.

2. Open the PayrollSystem website by clicking on it in the RecentProjects list, or by pulling down the File menu, selecting OpenWebsite, navigating to the folder where you previously saved thePayrollSystem, and clicking Open.

3. Download the PayrollSystem_DB.accdb file from Doc Sharingand save it on your local computer. (Note: your operating systemmay lock or block the file. Once you have copied it locally, rightclick on the file and select Properties and then Unblock ifavailable). Then add it to the PayrollSystem website as follows: InVisual Studio, in the Solution Explorer click Website, Add ExistingItem, then navigate to the PayrollSystem_DB.accdb file youdownloaded, and click the Add button.

Make sure you select file types, which include *.accdb, *.accdb,etc. Otherwise, you will not be able to see the database file toselect.

4. Now we need to create a new connection to thePayrollSystem_DB.accdb. To begin, click View Server Explorer.

5. When the Server Explorer toolbox appears, click the Connect toDatabase button.

STEP 1: Step Title

Page 2: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 5/20

6. When the Add Connection dialog appears, click the Changebutton. In the Change Data Source dialog, select MS AccessDatabase File; Uncheck Always use this Selection; then click OK.

Press Continue to get the following screen.

Page 3: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 6/20

7. Click the Browse button to navigate to thePayrollSystem_DB.accdb file in your website folder, then clickOpen. (NOTE: Be sure you select the PayrollSystem_DB.accdb filein your PayrollSystem website folder, not the one you originallydownloaded from Doc Sharing!) Click Test Connection. You shouldreceive a message that the test connection succeeded. Click OK toacknowledge the message, then click OK again to close the AddConnection dialog.

Page 4: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 7/20

8. The PayrollSystemDB.accdb should be added to the ServerExplorer. Expand the database, then expand the Tables entryunder the database until you see tblUserActivity. Leave the ServerExplorer window open for now as you will be returning to it in amoment.

9. Create a new dataset by selecting Website­> Add New Item.Under Templates, select the Dataset item. Enter dsUserActivity.xsdfor the name. Click Add.

10. If the following message appears, select Yes. You want tomake this dataset available to your entire website.

Page 5: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 8/20

11. If the TableAdapter Configuration Wizard dialog appears, clickCancel. (We will be configuring a Data Adapter for this datasetlater in C# code, so we do not need to run this wizard.)

12. Drag­and­drop the tblUserActivity table from the ServerExplorer window into the dsUserActivity dataset in the editorwindow.

NOTE: If you see a message that says your connection uses alocal data file that is not in the current project, that indicates youdid not select the correct PayrollSystem_DB.accdb file when youcreated your data connection. To fix this problem, click No, thenright­click on PayrollSystemDB.accdb in the Server Explorerwindow and choose Modify Connection. Click the Browse button,navigate to the PayrollSystemDB.accdb file that is in yourPayrollSystem website folder, and click Open. Test the connection,then click OK.

Page 6: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=12353311… 9/20

Click the Save icon on the toolbar to save the dsUserActivity.xsddataset.

(You can now close the Server Explorer window if you wish.)

13. Create a new class to contain the C# code that will access thisdataset. To do so, click Website, Add New Item. In the Add NewItem dialog, select the Class template, and enter clsDataLayer forthe name. Make sure the Language is set to Visual C#. Click Add.

14. If the following message appears, select Yes. You want tomake this class available to everything in your solution.

Page 7: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 10/20

15. Add the following to the top of your class, below any otherusing statements created for you by Visual Studio.

Add to top of class

// Add your comments here using System.Data.OleDb; using System.Net; using System.Data;

16. Add the following three functions inside the squiggly braces forthe public class clsDataLayer class, above the beginning of thepublic clsDataLayer() constructor and save the class.

Class

// This function gets the user activity from the tblUserActivity public static dsUserActivity GetUserActivity(string Database) // Add your comments here dsUserActivity DS; OleDbConnection sqlConn; OleDbDataAdapter sqlDA; // Add your comments here sqlConn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database); // Add your comments here sqlDA = new OleDbDataAdapter("select * from tblUserActivity", sqlConn); // Add your comments here DS = new dsUserActivity();

Page 8: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 11/20

// Add your comments here sqlDA.Fill(DS.tblUserActivity); // Add your comments here return DS; // This function saves the user activity public static void SaveUserActivity(string Database, string FormAccessed) // Add your comments here OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database); conn.Open(); OleDbCommand command = conn.CreateCommand(); string strSQL; strSQL = "Insert into tblUserActivity (UserIP, FormAccessed) values ('" + GetIP4Address() + "', '" + FormAccessed + "')"; command.CommandType = CommandType.Text; command.CommandText = strSQL; command.ExecuteNonQuery(); conn.Close(); // This function gets the IP Address public static string GetIP4Address() string IP4Address = string.Empty ; foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress)) if (IPA.AddressFamily.ToString() == "InterNetwork") IP4Address = IPA.ToString(); break; if (IP4Address != string.Empty) return IP4Address; foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName())) if (IPA.AddressFamily.ToString() ==

Page 9: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 12/20

"InterNetwork") IP4Address = IPA.ToString(); break; return IP4Address;

17. Create a new web form called frmUserActivity. Switch toDesign Mode and add the ACIT logo to the page as anImageButton and link it back to frmMain. Below the image buttonadd a panel. To the panel, add a Label and GridView (found underthe Toolbox, Data tab) having the following properties.

Property Value

Label – Text User Activity

GridView – (ID) grdUserActivity

18. Go to the Page_Load method by double clicking an emptyspace on the page and add the following code.

STEP 2: frmUserActivity, frmPersonnel, frmMain

Page 10: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 13/20

Page_Load method for frmUserActivity.aspx

if (!Page.IsPostBack) // Declares the DataSet dsUserActivity myDataSet = new dsUserActivity(); // Fill the dataset with what is returned from the function myDataSet = clsDataLayer.GetUserActivity(Server.MapPath("PayrollSystem_DB.accdb")); // Sets the DataGrid to the DataSource based on the table grdUserActivity.DataSource = myDataSet.Tables["tblUserActivity"]; // Binds the DataGrid grdUserActivity.DataBind();

19. Open the frmMain form, add a new link button and imagebutton to point to the new frmUserActivity. Find an image to use forthe image button and add the new option as View User Activity.

20. Go to the frmMain Page_Load and add the following code.

frmMain.aspx Page_Load code

// Add your comments here clsDataLayer.SaveUserActivity(Server.MapPath("PayrollSystem_DB.accdb"), "frmPersonnel");

21. In the Solution Explorer, right click on the frmMain.aspx formand select Set As Start Page. Run your project. When you openthe project, a record should be saved in the tblUserActivity tablewith the IP address, form name accessed (frmPersonnel), and thedate accessed. When you click the View Activity button, you shouldsee at least one record with this information.

23. You will now add server side validation code to thefrmPersonnel page. Currently, when the Submit button is pressed,the frmPersonnelVerified page is displayed. This is because thefrmPersonnelVerified page is set as the Submit button'sPostBackUrl property. Instead of having the page go directly to thefrmPersonnelVerified page when the Submit button is pressed, we

Page 11: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 14/20

want to do some server side validation. If any of the validationrules fail, we will redisplay the frmPersonnel page with the fields inquestion highlighted in yellow with an error message displayed.

First, it is important to understand what is currently happeningwhen the submit button is pressed. This is causing a postback ofthe form to the frmPersonnelVerified form. When this postbackhappens, all of the data in the fields on the frmPersonnel form aresent to the frmPersonnelVerified form as name value pairs. In thePage_Load code of frmPersonnelVerified these values are pickedup from the Request object and displayed. Each name value pairwill be in the Request object as the ID of the control containing thevalue and the value itself. We can pass data between pages byusing Session state instead. In order to do validation on the valuesbut still have the values visible on the frmPersonnelVerified page,we will need to change not only the PostBack URL of thefrmPersonnel page but also how the frmPersonnelVerified form isgetting the data—it will need to get it from Session state ratherthan from the Request object.

In order to do this, we will make the following changes.

1. Clear the Submit button PostBackURL Property on thefrmPersonnel form. Remove the value inthe PostBackUrl that is highlighted.

2. In the btnSubmit_Click event handler get each value fromthe data entry fields and set Session state items for each.(instructions below)

3. Change the frmPersonnelVerified code behind to get thevalues from the Session state items you created in theprevious step. (instructions below)

When you are done with these steps, you should be able to enterdata on the frmPersonnel data entry form and then click theSubmit button. The frmPersonnelVerified page should then bedisplayed with the values that were in the data entry fieldson frmPersonnel.

23. Add a label to the frmPersonnel form with an ID of lblError. Donot place the label to the right or left of any of the controls on theform. Add it below the controls or above the controls. The textproperty of this label should be set to an empty string.

Page 12: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 15/20

24. Add code to perform server side validation in response to thesubmit button being clicked. Here are the business rules we wantto enforce (remember this will be server C# code in thefrmPersonnel code behind): Fields may not be empty or filled withspaces. If any field is empty, turn that field background color toyellow and add to/create an error message to be shown in theerror label. The end date must be greater than the start date. If theend date is less than the start date, turn both date fields yellow andadd to/create an error message to be shown in the error label. If allfields validate properly then the session state items should be setproperly and the user should see the frmPersonnelVerified formwith all the values displayed.

frmPersonnel.aspx Lab Hints

1. The server side validation should be in the Submit button'sevent handler. There is a Trim method on the string object that willautomatically remove spaces from the beginning and end of astring. To test if txtFirstName is empty or filled with spaces, usethe following code.

if (Request["txtFirstName"].ToString().Trim() == "")

2. To set the background color of the txtFirstName field, use thefollowing code.

txtFirstName.BackColor = System.Drawing.Color.Yellow;

3. To set a value in session state and redirect the response to thefrmPersonnelVerified.aspx do the following. txtFirstName is the key

Page 13: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 16/20

and txtFirstName.Text is the value.

Session["txtFirstName"] = txtFirstName.Text; //Need to set session variables for all text boxes Response.Redirect("frmPersonnelVerified.aspx");

4. You may want to create variables to work with for validationrather than using the Request item objects directly.

To turn a string into a DateTime object you can use the DateTimemethod Parse. If you had a date value stored in a string calledstrDate, you could turn it into a DateTime object like this.

DateTime myDateTimeObject = DateTime.Parse(strDate);

You can compare two DateTime objects by using theDateTime.Compare method. If you had two DateTime objectscalled dt1 and dt2 you can check to see if dt1 is greater than dt2by doing this.

if (DateTime.Compare(dt1,dt2) > 0)

DateTime.Compare will return a 0 if the two dates are equal, a 1 ifdt1 is greater than dt2, and a ­1 if dt1 is less than dt2.

If you put in an invalid date for either of the date fields, you will getan exception/server error when trying to parse the values. We willaddress this in a later lab—for now make sure you enter validdates (valid meaning a date in the form of mm/dd/yyyy).

5. An example of the code you might want to use to test if the enddate is after the start date follows.

DateTime startDate = DateTime.Parse(Request["txtStartDate"]);

Page 14: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 17/20

DateTime endDate = DateTime.Parse(Request["txtEndDate"]); if (DateTime.Compare(startDate, endDate) > 0) txtStartDate.BackColor = System.Drawing.Color.Yellow; txtEndDate.BackColor = System.Drawing.Color.Yellow; Msg = Msg + "The end date must be a later date than the start date."; //The Msg text will be displayed in lblError.Text after all the error messages are concatenated validatedState= false; //Boolean value ‐ test each textbox to see if the data entered is valid, if not set validState=false. //If after testing each validation rule, the validatedState value is true, then submit to frmPersonnelVerified.aspx, if not, then display error message else txtStartDate.BackColor = System.Drawing.Color.White; txtEndDate.BackColor = System.Drawing.Color.White;

Remember to clear the PostBackURL property of the Submitbutton!

frmPersonnelVerified.aspx Lab Hints

When using the Session state in frmPersonnel.aspx fortxtFirstName, you used the following code: Session["txtFirstName"]= txtFirstName.Text;

To get this same value back from the session we use the key andthe Session object in the Page_Load of frmPersonnellVerified.aspx(instead of using Request, use Session) as follows.

Page 15: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 18/20

Session["txtLastName"].ToString()

23. View the video above on what functions your lab should haveso far.

24. Run your project. When you open the project and go to themain menu form a record should be saved inthe tblUserActivity table with the IP address, form name accessed(frmPersonnel), and the date accessed. When you click the ViewActivity button you should see at least one record with thisinformation. The validation and error display should work forentering data. All navigation and hyperlinks should work.

Once you have verified that it works, save your project, zip up allfiles, and submit in the Dropbox.

STEP 3: Verify and Submit

Page 16: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 19/20

Page 17: Lab Steps - f01.justanswer.com

1/21/2016 CIS407A Week 3 iLab (SEP15)

http://frameset.next.ecollege.com/(NEXT(3a81adf077be42cabcb9b68adc1d16db))/Main/AllMode/FramesetHybrid/FramesetFromPSHView.ed?ihm=1235331… 20/20

NOTE: Make sure you include comments in the code providedwhere specified (where the " //Your comments here" is mentioned)and for any code you write, or else a five­point deduction per item(form, class, function) will be made. You basically put two forwardslashes, which start the comment; anything after the // on that lineis disregarded by the compiler. Then type a brief statementdescribing what is happening in the following code. Commentsshow professionalism and are a must in systems. As a professionaldeveloper, comments will set you apart from others and make yourlife much easier if maintenance and debugging are needed.


Recommended