Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.
Recent comments

In order to get the value of your QueryString parameters from the URL all you need is this line of code:

int id = HtmlPage.Document.QueryString["ID"];

And you need to add Windows.Browser namespace for HtmlPage class:

using System.Windows.Browser;

However, there are always two situations which could break your application:

  1. if there are no “ID” parameters in the url
  2. if there is an “ID” parameter in the url, but it has a string value instead of an integer number.

To avoid breaking the code and causing runtime error we need to add a few conditions and also parse the value to integer instead of assigning it right away. We can use Int32.TryParse() method to test the value which we are about to assign to an integer variable before we actually do so, this way we will only assign it if it really is an integer:

int id = -1;
if (HtmlPage.Document.QueryString.ContainsKey("ID"))
    string queryStringValue = HtmlPage.Document.QueryString["ID"];
    if (Int32.TryParse(queryStringValue, out id))
        // the result was successful and
        // the correct ID will be inserted to id
        // the result was not successful                   

Using above method, you ensure the id gets assigned a value only if the ID parameter in the url has a valid integer value, or it will remain as -1 if the value was not an integer.

Posted by Damon Serji on 30. September 2009 22:30 under: Basic, Intermediate
 with 3 Comments


James Morrison:

Can I do the same with a timestamp or Guid? Any idea? I really found this useful but I need to also pass a timestamp and guid as a querystring!

Posted 9/28/2009 11:17:35 AM


Hi James and welcome to Silverlight Tips.

I have done a quick research and couldn't find any way to pars Guid using a Silverlight built-in methods. I will do a new post on this subject to create a new extension method for validating Guid and possibly one for timestamp.

I already have done a post about Extension methods ( I will also create a new project example to validate all Id, Guild and timestamp along with a few more parameters and publish it live for download.

I will do above tasks by end of this week, so keep yourself updated..

Posted 10/3/2009 8:42:42 PM

Richard Kavanagh:

Not so sure of using the value of the integer as a flag to denote that an integer has been passed. What if -1 was the value of the integer passed?

Posted 11/29/2009 1:12:10 PM

Add comment

(Will show your Gravatar icon)

  • Comment
  • Preview