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:
– can be used to pass data from one page to another
(Any other ways?)
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 (+)
How are query strings created?
• Query strings are manually built by the developer and used with
• 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:
Response.Redirect(“Unit6L22.aspx?UserName=“ + txtName.Text + “&subTotal=“ + lblSubTotal.Text);
• 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
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:
lblName.Text = Request.Querystring[“UserName”].ToString();
• 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:
lblWecomeString.Text = “Hello” + Request.Querystring[“UserName”].ToString();
lblWecomeString.Text += “Your sub-total is $“ + Request.Querystring[“SubTotal”].ToString();
• Another example, but this time we have to convert our query string since
we want to perform math on it:
decimal intValue = 0m;
decValue = Convert.ToDecimal(Request.QueryString[“SubTotal”]);
• 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
Error messages!
• What happens if we try to read a key that does not exist?
int intValue = 0m;
intValue = Convert.ToDecimal(Request.QueryString[“key3”]);
• When this code executes, and if key3 does not exist we will get
an error message:
• Why does this happen?
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
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”
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.
Controlling error messages
with Try – Catch #2
• So, in the previous key3 example we could modify the code to
look like this:
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 1st file) and
Unit 6 L22_2 – Reading Query Strings (in the 2nd file)
3. In U6L22_1.aspx, add 3 text boxes and a button
Set the button text to Pass the values to the next page
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,
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:
L2.2 #3
6. Test the L2.2 functionality by typing in 2.0 in the 1st textbox,
dog in the 2nd textbox and 4 into the 3rd textbox
it should look similar to this example:
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.
Think About It!
• Why is try – catch critical to usability of
• 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.

similar documents