Calling Custom Code Functions in WHERE Clauses

In most applications there are a few cases where it’s useful to constrain a query based on values computed by your own custom code.  For example, in a customer portal application, you may want to calculate and display a customer’s available credit capacity.  You can write a custom function in C#, Visual Basic .NET or other .NET language that is called by your application.  Custom functions are available in several contexts.

Custom functions in WHERE clauses

Custom functions can be called from within your application’s SQL WHERE clauses when selecting and displaying data.  Custom functions return a value that is compared to the record's field value when that record is read at run-time.  Based on the comparison of the two, the record is either included in or excluded from the result set.

Use a custom function of your own to constrain the selection criteria to retrieve data.

Select your custom function in the Add WHERE Clause dialog.  When Iron Speed Designer generates your application, it creates the “addFilter” mechanism that applies the comparison.

Custom functions should return values that appropriately match the left side of the filter clause.  It’s important to note that custom functions are not filtering functions themselves; they simply provide a value which is used by the generated filtering code when comparing record field values to the value returned by the custom function.

Custom functions in field initialization

Custom functions can be called when initializing FieldValue components.  Use your custom function to provide custom field initialization values.

Use a custom function when initializing a field.

Select your custom function in the Properties dialog, Bindings tab, Set Initial Value To section.

Usage contexts

Regardless of whether you are calling your custom function from within a WHERE clause or as part of a field’s initialization, the mechanism for adding your custom code and calling your functions from within your application are the same.  The only difference is the context in which function names are resolved.

Context

Description

WHERE clauses

Iron Speed Designer inserts function calls to your code into the appropriate generated code files.  Your custom functions must be accessible from there.

Note: These functions must be defined in the .Controls.cs or .vb files.  They cannot be defined in the page class.

Field Initialization
(Set Initial Value To)

Iron Speed Designer inserts function calls to your code within "server control tags" in the generated page (a.k.a. "code in-fronts"), so your custom functions must be accessible from that context.

Note: These functions must be defined in the Page code-behind files.  They cannot be defined in the .Controls.cs or .vb files.

Typically, fully-qualified function names are accessible from both places, so the same function reference can be used both contexts.

For example, if your custom function is defined in the customizable class of a Show Customers Table page, then the fully-qualified function reference would be:

ShowCustomersTable.MyFunction()

In this example, the function should be declared as "Public Shared".  If you have a separate function library included in the application, the function reference would be similar to:

MyLib.MyFunction()

Passing parameters to custom functions

You can pass any kind of parameter you want into a custom function, but the parameter must be meaningful within the scope of the custom function and its surrounding class structure.  (Record source queries are added as data reader filters in the page's code-behind).

For example, you can pass column references into the function:

MyCustomFn(CustomFunctionDemo.ProductsTable.Instance.UnitsOnOrderColumn)

Note that this is only a column reference and not the actual column's data.  This is because there is no data available (yet) at "add filter time".

Record source custom functions are only used as a right hand side operand for a comparison operation, e.g.

WHERE Products.UnitsOnOrder > MaximumOnOrderLevel()

It is possible to filter data using a custom function, but this involves customizing a code-behind rather than invoking a custom function set in the Properties dialog.  Custom functions themselves only return right-hand side values that are used by the filtering code.

Related

Example: Calling a .NET Framework Function

Example: Calling a Custom Function in a Customizable Class

See Also

Customizing Database Queries with the Query Wizard

Using Tables and Views in WHERE Clauses

Using Constant Values in WHERE Clauses

Using URL Parameters in WHERE Clauses

Using the Logged-In User ID in WHERE Clauses

Calling Custom Code Functions in WHERE Clauses

Adding a Custom Query