Home Dashboard Directory Help

NullReferenceException in DataPager control by Richard Deeming


 as Fixed Help for as Fixed

Sign in
to vote
Type: Bug
ID: 357344
Opened: 7/22/2008 1:06:17 PM
Access Restriction: Public
User(s) can reproduce this bug


A DataPager control with the QueryStringField property set throws a NullReferenceException when the query-string contains either an empty element (eg: "?&foo=bar") or an element with no value (eg: "?foo").

The exception is thrown from the internal GetQueryStringNavigateUrl method of the DataPager class. In some circumstances, the key returned from the QueryString.AllKeys collection is null, but the method fails to check for a null value.
Sign in to post a comment.
Posted by Microsoft on 7/22/2008 at 10:25 PM
Thanks for your feedback. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Posted by Richard Deeming on 7/22/2008 at 1:24 PM
The problem is in the GetQueryStringNavigateUrl method:

foreach (string key in request.QueryString.AllKeys)
    if (... && !key.Equals(queryStringField, StringComparison.OrdinalIgnoreCase))

When the query-string collection contains a null key, this will throw a NullReferenceException. The code should read:

if (... && !string.Equals(key, queryStringField, ...))

Since the Equals method includes checks for null values, the problem would be solved.
Posted by Richard Deeming on 7/22/2008 at 1:20 PM
I have uploaded a demonstration. Extract the files to a new folder, and create an application in IIS (or run Casini on the folder). The two links at the top of the page - "Error 1" and "Error 2" - will demonstrate the problem.
Sign in to post a workaround.
Posted by mysterious.e on 8/25/2008 at 4:41 PM
I have just implemeneted a path rewrite implementation and it seems to work fine. I am creting a custom control inheriting from DataPager and overriding the OnInit portion. I am then testing the query values. If there is a null query value I am rewriting the path with a clean querystring. Note: this could also be done at the user control, page, or module level.

protected override void OnInit(EventArgs e)
            Boolean IsDirtyPath = false;
            var CleanValues = new Dictionary<String, String>();

            //identify key problems
            foreach (string key in Context.Request.QueryString.AllKeys)
                if (key == null)
                    IsDirtyPath = true;
                    CleanValues.Add(key, Context.Request.QueryString[key]);

            if (IsDirtyPath)
                //get the path localpath
                UriBuilder Builder = new UriBuilder(Context.Request.Url);
                Builder.Query = String.Join("&", CleanValues.Select(p => p.Key + "=" + p.Value).ToArray());
                String Rewrite = Builder.Uri.PathAndQuery;