onSQLCountQuery Event

Description

Fires when the Grid needs to count the records in the current Grid query. If you want to base the Grid on a stored procedure you need to use this event to compute the number of records in the Grid.

Discussion

Each time the Grid is rendered, it executes a query to compute the number of records that are shown in the Grid. This event fires just before the count query is executed. You can add an event handler for this event to compute the record count yourself and tell Alpha Anywhere not to try to compute the count itself.

If your Grid is based on a stored procedure then you must define code for this event as Alpha Anywhere will not be able to get the count of the number of records itself.

If your event handler takes over the responsibility of computing the record count, it must set these properties:

' Set the record count
e.count = recordCount

' Indicate the count has been computed and that Alpha Anywhere
' should not compute the count query
e.handled = .t.

Here is a sample onSQLCountQuery event handler that you might write:

function onSQLCountQuery as v (e as p)

    dim flag as L
    flag = e.cn.execute("exec myCountStoredProcedure @city=:whatcity",e.arguments)
    if flag = .t. then
        dim rs as SQL::ResultSet
        rs = e.cn.resultSet
        e.count = rs.data(1)
        e.handled = .t.
    else
        e.fatalError = .t.
        e.errorText = "Could not get record count. " + e.cn.callResult.text
    end if 

end function

In the above example, the following assumptions were made:

  • The stored procedure is for SQL Server, so the syntax used is appropriate for SQL Server.
  • The stored procedure is called 'myCountStoredProcedure' and it takes a single parameter '@city'.
  • The Grid has a Search Part that allows the user to search by city and the e.arguments object that is passed in has an argument called 'whatcity' which we bind to the stored procedure's '@city' parameter.
  • The resultset returned by the stored procedure has a single row and column with the record count in it. We set e.count to the value in the first row and first column.
  • We set e.handled = .t. to tell Alpha Anywhere that the count has been computed.

One of the properties passed in with the 'e' object is e.sql. This is the SQL that Alpha Anywhere will parse to convert into a count query in order to compute the record count IF e.handled is NOT SET TO .t.. Your event handler can actually modify the value in e.sql, or any of the values in e.arguments should you wish to for any reason.

Event Arguments & Properties

The following variables are available to you in the event:

Variable
Description
e.tmpl

The grid component definition

e.rtc

Run-time calculations (allows you to pass data to other event handlers)

e.__si

State information

e.cn

The SQL::Connection object for the Grid. You can call this object's .execute() method to execute your own SQL.

e.filter

The WHERE clause which indicate what filter (if any) the user has applied to the Grid

e.order

The fields (if any) that the user has ordered the Grid on

e.arguments

The SQL::Arguments object that contains argument value for all arguments used in the WHERE clause (i.e. the e.filter parameter) To read a value from arguments: e.arguments.find("argumentName").data

e.sql

The SQL statement that the Grid has computed. If e.handled is NOT set to .t., then the Grid will compute the number of records returned by this query.

Your event must set the following properties:

Property
Description
e.count

The number of records in the query

e.handled

Set to .t. if your event has computed the record count. Set to .f. if you want Alpha Anywhere to execute its own count query

Your event can optionally set these properties of the e object:

Property
Description
e.fatalError

Set to .t. if a fatal error has occurred

e.errorText

The error text to show if a fatal error occurred

Setting State Variables

You can also set state variables in this event. The value of any state variables will be available in all subsequent ajax callbacks (in the e.__si2 object).

To set a state variable:

e._state.myvar1 = "value1"
e._state.myvar2 = "value2"