tags:

views:

319

answers:

2

I have a repeater using a LinqDataSource as a data source. When a query string is entered, I'd like to filter the results, but ONLY when a query string is entered. If there is no query string, the results should not be filtered, all results should be returned.

I'm trying to add a WhereParameter to my LinqDataSource in the Selecting event of the LinqDataSource, but it's not working. Here's my code:

protected void ldsImages_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    if (Request.QueryString["id"] != null)
    {
        e.WhereParameters.Add("ImageTypeID", Request.QueryString["id"]);
    }
}
A: 

I've never used a LinqDataSource, but could you not query your results as shown here? (Code extract from the site):

 protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        var cities = from city in citiesArray
                     where city.CompareTo("B") > 0
                     select city;
        e.Result = cities;

    }

Therefore, you'd have something like:

 protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        if(Request.QueryString["id"] != null)
        {
               var myImage = from img in imageSource
                             where img.ID == Request.QueryString["id"]
                             select img;
               e.Result = myImage;
        }
        else
        {
               e.Result = imageSource;
        }


    }

A few things to note. First, this is untested :D. Second, if your ID is an integer, don't forget to cast the querystring as an integer. Finally, you should be sanitizing the querystring before using it. Although it may not be a major problem here, it's good practise.

keyboardP
A: 

Are you sure the Request.QueryString["id"] value is not null?

Are you setting the AutoGenerateWhereClause property to "true" on your LinqDataSource?

Read the Remarks section of the WhereParameters documentation page carefully.

Jeff Meatball Yang