1 / 14

MIS 3200 – Unit 6.2

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. Query Strings – What are they?. Query strings: are found at the end of the URL. fox example:

summer
Download Presentation

MIS 3200 – Unit 6.2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • userNamewhich is assigned a value of “Shannon Prescott” • subTotalwhich 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”); Response.Redirect(“Unit6L22.aspx?UserName=“ + txtName.Text + “&subTotal=“ + lblSubTotal.Text);

  5. How are query strings read? lblName.Text = Request.Querystring[“UserName”].ToString(); • 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 SubTotalkey 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 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? intintValue = 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 messageswith 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 messageswith 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 • Create 2 new files in your Unit 6 folder lastnameU6L22_1.aspx and lastnameU6L22_2.aspx • Update the heading to be Unit 6 L22_1 – Creating Query Strings(in the 1st file) and Unit 6 L22_2 – Reading Query Strings(in the 2nd file) • In U6L22_1.aspx, add 3 text boxes and a button • Set the button text to Pass the values to the next page

  12. L2.2 #2 • 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 • In U6L22_2.aspx, • add a label (which will display the contents of the query string) • add a button with the Text set to Change the values and in the button click method, redirect the user back to the 1st page • When the 2nd 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 • 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

  13. L2.2 #3 • Test the L2.2functionality by typing in 2.0 in the 1st textbox, dog in the 2nd textbox and 4 into the 3rd textboxit 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 • Add appropriate comments to explain what the the methods are doing • 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.

More Related