Thursday, March 17, 2011

Check QueryString for Null

If you need to determine whether the Request.QueryString is null and take some action depending on the result, it is not enough to check for null. QueryString is an HttpValueCollection type, so even if the Request.Url does not have a query string appended to it, the value of the collection is not going to be null. The proper way to check if the query string contains anything is to check for null and the Count property as well:

if(Request.QueryString!= null && Request.QueryString.Count>0){
     //some piece of code.
}



5 comments: