Filtering with string parameter that allows free user input

Posted by: the telerik blogs, on 17 Sep 2009 | View original | Bookmarked: 0 time(s)

As you probablyknow, Telerik Reportingcomes withbuilt-inparameters andfilters. Report parameters allow users to specify or control the data used in a report.They areextremely powerful when used in expressionsfor filtering, sorting, grouping or evendirectly provide a value. When used with filters you can limit the number of records in a report based on specified filter rules.If the conditions of the rules are met, the record is included in the report.

Let's suppose we havea string parameterthat allows free user input, wired up with a filter. This waythe user has the freedom to type any word and the report would be filteredbased on the parameter valueto show relevant information. String comparison howeveris case sensitive by design, so the user must be pretty precisewith the provided input. The reasonfor the case sensitivity isthat you can easilychange that by applying ToLower or ToUpper on both left and right sides of the comparison, while you cannot change it the other way around (from non case sensitive tocase sensitive).

So back tothe free input scenario -in this case it is definitely a good idea to make the accepted input non case sensitive, to assure that there would be a proper match. There are two ways to make this happen:

  1. Throughuser defined function, for example:

    public static string StringToUpper(object value)
    {
    if (null == value || System.DBNull.Value == value)
    {
    return string.Empty;
    }
    return value.ToString().ToUpper();
    }

    Then in the filter use:

    Expression Oper.Value
    ====================================================================
    =StringToUpper(Fields.MyField)==StringToUpper(Parameters.MyParam)
  2. Use String.ToUpper method directly in the expression, ensuring the value is not null:

    ExpressionOper. Value
    ====================================================================
    =CStr(IsNull(Fields.MyField, ")).ToUpper()==CStr(IsNull(Parameters.MyParam, ")).ToUpper()

So far so good - now we can entera value not caring about its casing and would surely receive relevant data (if there is a match). This would work fine for a single word values, however what happens if the value consists of several words e.g. Employee Name. We would have to type down the whole name down in order to receive a record back, which is not good. In order to take care of this, we would use "LIKE" as Operator instead of "=" and a wild card in theValue expression:

ExpressionOper. Value
====================================================================
=StringToUpper(Fields.MyField)Like="%" + StringToUpper(Parameters.MyParam) + "%"

You can find more infoon user defined functions, operators, wild chardsetc. in the help topic Using Expressions.

A sample project showing the second approach is available in this code library article.

Enjoy!
Steve

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 983 | Hits: 10

Similar Posts

  • Introducing Versatile DataSources more
  • Unit Testing - Do Repeat Yourself more
  • LINQ Tip of the week: System.String support more
  • Update to Logging in to DotNetNuke from a Silverlight Application with RIA Authentication more
  • Url.Action() and RouteValue Encoding more
  • Validation - Part 1 - Getting Started more
  • ORM Release History : Q1 2009 SP1 (version 2009.1.405.1) more
  • Don't be afraid of complex constraints more
  • Calling the Twitter API in C# more
  • Dynamic Delegates with Expression Trees more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD