Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.

Similar presentations


Presentation on theme: "MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch."— Presentation transcript:

1 MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch

2 Query Strings – What are they? Query strings: – are found at the end of the URL. fox example: – can be used to pass data from one page to another (Any other ways?)

3 Query String: Characteristics Query strings: – are started with a question mark (?) – can contain any number of “keys” (i.e. name – value pairs) – Different Keys which are separated with an ampersand (&) – Words within the values are separated by a plus sign ( + ) http://aspnet.cob.ohio.edu/MIS3200/ASPPUB/MIS3200/Unit6/ Unit6L22.aspx?userName=Shannon+Prescott&subTotal=270.35;

4 How are query strings created? Query strings are manually built by the developer and used with Response.Redirect When this code executes, the user will be sent to the target and will pass along two keys via the query string: – userName which is assigned a value of “Shannon Prescott” – subTotal which is assigned a value of “270.35” Begin the page redirection Query strings can be built like any other string you have worked with. Using concatenation we can construct a query string that will use values from TextBoxes on a page, for example: When this code executes, the user will be sent to Unit6L22.aspx and will pass along two values via the query string: – So effectively, the key, UserName= txtName.Text – and the key, subTotal = lblSubTotal.Text Response.Redirect(“Unit6L22.aspx?UserName=“ + txtName.Text + “&subTotal=“ + lblSubTotal.Text); Response.Redirect(“Unit6L22.aspx”);

5 How are query strings read? Query strings are typically read on the target pages Request.Querystring allows retreieval of the key and its value, for example: When this code executes, the value assigned to the key will be read from the query string, converted to string so that it can be placed in the Text property of a label on our page. When reading the query string keys, you must use brackets [] around the name of the key: Another example, but this time we have to convert our query string since we want to perform math on it: When this code executes, the value assigned to SubTotal key will be read from the query string, converted to a decimal, and assigned to decValue. Based on the previous slide, the value is 270.35 lblName.Text = Request.Querystring[“UserName”].ToString(); lblWecomeString.Text = “Hello” + Request.Querystring[“UserName”].ToString(); lblWecomeString.Text += “Your sub-total is $“ + Request.Querystring[“SubTotal”].ToString(); lblWecomeString.Text = “Hello” + Request.Querystring[“UserName”].ToString(); lblWecomeString.Text += “Your sub-total is $“ + Request.Querystring[“SubTotal”].ToString(); decimal intValue = 0m; decValue = Convert.ToDecimal(Request.QueryString[“SubTotal”]);

6 Error messages! What happens if we try to read a key that does not exist? When this code executes, and if key3 does not exist we will get an error message: Why does this happen? int intValue = 0m; intValue = Convert.ToDecimal(Request.QueryString[“key3”]);

7 Prevent errors – clever code One way to address the problem is to see if the key exists by using a conditional test: When this code executes, and if key3 does not exist, intValue is still set to 0

8 Issues with preventing errors But, wait! What if key3 exists, but has a value set to cat? The conditional test will pass, but we will run into an error if we try to convert “cat” to an integer? ERROR! >>> “Input string was not in a correct format”

9 Controlling error messages with Try – Catch Using a Try – Catch statement allows us to “try” to execute some code and if there is an error (an Exception) it will “catch” the error and let you decide what to do with it.

10 Controlling error messages with Try – Catch #2 So, in the previous key3 example we could modify the code to look like this:

11 Hands On – Unit 6 L22 Creating and reading query strings 1.Create 2 new files in your Unit 6 folder lastnameU6L22_1.aspx and lastnameU6L22_2.aspx 2.Update the heading to be Unit 6 L22_1 – Creating Query Strings (in the 1 st file) and Unit 6 L22_2 – Reading Query Strings (in the 2 nd file) 3.In U6L22_1.aspx, add 3 text boxes and a button a.Set the button text to Pass the values to the next page

12 L2.2 #2 4.In the button click method of the Pass the values to the next page create a query string which passes the values of the 3 text boxes to the U6L22_2.aspx page 5.In U6L22_2.aspx, a.add a label (which will display the contents of the query string) b.add a button with the Text set to Change the values and in the button click method, redirect the user back to the 1 st page c.When the 2 nd page loads, you should display the contents of the all keys passed in the query string in the label. Additionally, if a key can be converted into a decimal (e.g. somebody type in 2.0 into one of the text boxes), display a message that the key is a decimal; otherwise, display a message the key is NOT a decimal d.For an example of what the page should be, see the link below: http://aspnet.cob.ohiou.edu/mis3200/asppub/MIS3200/Unit6/BobcatU6L22_1.aspx http://aspnet.cob.ohiou.edu/mis3200/asppub/MIS3200/Unit6/BobcatU6L22_1.aspx

13 L2.2 #3 6.Test the L2.2 functionality by typing in 2.0 in the 1 st textbox, dog in the 2 nd textbox and 4 into the 3 rd textbox it should look similar to this example: http://aspnet.cob.ohiou.edu/mis3200/asppub/MIS3200/Unit6/BobcatU6L22_2.aspx?key1=2.0&key2=dog&key3=4 http://aspnet.cob.ohiou.edu/mis3200/asppub/MIS3200/Unit6/BobcatU6L22_2.aspx?key1=2.0&key2=dog&key3=4 7.Add appropriate comments to explain what the the methods are doing 8.Create a link to your U6L22_1.aspx page from your MIS3200 page and copy everything to ASPNET and submit your MIS Portfolio URL to the drop box.

14 Think About It! Why is try – catch critical to usability of applications? When should query strings be used instead of session variables? When should you NOT use query strings? From this point forward your assignments should NEVER crash – ALWAYS use try/catch to protect any code that might crash AND always provide meaningful messages when an Exception is caught.


Download ppt "MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch."

Similar presentations


Ads by Google