iEntry 10th Anniversary RSS Newsletter Advertising
Join the WebProWorld Forum!
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook
Tuesday, January 30, 2007

ASP.NET: Working with Query Strings

In many cases we write our ASP.NET logic around query strings in order to show the right product page or what not.

The first thing we do is to check if the query string exists in the first place before we start using it. It could look like this:

if (Request.QueryString["id"] != null)
{
  // Do something with the querystring
}

The only problem with the above check to see if the query string is null, is that we don't take into consideration if the query string is filled or not.

That could lead to unhandled exceptions in the code. Instead we should check for query strings like this:

if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
  // Do something with the querystring
}

Then there is the data type of the query string.

Our code might need an integer of 5 digits to get the right information from the database, so if we pass it a string we could end up with a data type mismatch exception.

So we do the check again more thoroughly:

if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 5)
{
  // Do something with the querystring
}

Now we know that we get a query string suitable for further processing.

You can then do more precise data type checks using the TryParse method of most value types or by some other logic.

Comments

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

ASP.NET

 

Nice one man,
Keep it up.
 
Regards,
Nisheeth

Thanks

Thank you!

It did help me :)

Publish A Comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
SEARCH
Popular WPN Business Resources












Subscribe to WebProNews


Send me relevant info