CodeCharge Studio All ASP PHP Java C#.NET VB.NET Perl ColdFusion

Dynamically Modify the WHERE Clause

This example shows how to modify the WHERE clause of a Grid form. Within an SQL statement, the Where clause is used to specify the criteria which will be used to select database records.

In this case, we have a Tasks grid that is based on the tasks database table which has a text field called task_name. The Grid will show only those task records whose names starts with "Improve%".

  1. Add the Before Build Select event to the grid.
  2. Within the event, add the code below:

ASP

Function Tasks_DataSource_BeforeBuildSelect()

  If Tasks.DataSource.Where <> Empty Then
    Tasks.DataSource.Where = Tasks.DataSource.Where & " AND "
  End if

  Tasks.DataSource.Where = Tasks.DataSource.Where & "task_name like 'Improve%'"

End Function

Note: If the OR operator is used in the WHERE clause the conditions must be in brakets "()". For example:
Tasks.DataSource.Where = Tasks.DataSource.Where & "(task_name like 'Improve%' OR task_name like 'Add%')"

PHP

function Tasks_ds_BeforeBuildSelect() {
global $Tasks;

  if ($Tasks->ds->Where <> "") {
    $Tasks->ds->Where .= " AND ";
  }

  $Tasks->ds->Where .= "task_name like 'Improve%'";

}

Note: If the OR operator is used in the WHERE clause the conditions must be in brakets "()". For example:
  $Tasks->ds->Where .= "(task_name like 'Improve%' OR task_name like 'Add%')";

Perl

sub Tasks_ds_BeforeBuildSelect() {

  if ($Tasks->{ds}->{Where} ne "") {
    $Tasks->{ds}->{Where} .= " AND ";
  }
  $Tasks->{ds}->{Where} .= "task_name like 'Improve%'";

}

Note: If the OR operator is used in the WHERE clause the conditions must be in brakets "()". For example:
  $Tasks->{ds}->{Where} .= "(task_name like 'Improve%' OR task_name like 'Add%')";

ColdFusion

<!---Tasks_BeforeBuildSelect --->
  <CFIF strWhere NEQ "">
    <CFSET strWhere="#strWhere# AND ">
  </CFIF>
  <CFSET strWhere="#strWhere#task_name like 'Improve%'">

Note: If the OR operator is used in the WHERE clause the conditions must be in brakets "()". For example:
  <CFSET strWhere="#strWhere#(task_name like 'Improve%' OR task_name like 'Add%')">

Java

//Tasks_BeforeBuildSelect

  if (! StringUtils.isEmpty(e.getCommand().getWhere())) {
    e.getCommand().setWhere(e.getCommand().getWhere() + " AND task_name like '%Improve%'");
  } else {
    e.getCommand().setWhere("task_name like '%Improve%'");
  }


VB.Net

  1. Add the Before Execute Select event to the Grid.
  2. Within the event, add the code below:
'Tasks_BeforeExecuteSelect

  DirectCast(Select_,TableCommand).Where = "task_name like 'Improve%'"
  DirectCast(Select_,TableCommand).Operation = "AND"

C#

  1. Add the Before Execute Select event to the Grid.
  2. Within the event, add the code below:
//Tasks_BeforeExecuteSelect

  ((TableCommand)Select).Where = "task_name like 'Improve%'";
  ((TableCommand)Select).Operation = "AND";

See Also:

Before Build Select event, Before Execute Select event


On-line, printable versions and updates