The recordset object is the primary interface to data. You use it to access and manipulate the data returned from a query. The recordset object represents all the records from a table or query result, but only references a single record at a time.
The recordset object has several properties, methods, and events that are exposed to scripting. You use these to set parameters, such as the SQL query string, execute functions, and respond to actions.
You will only use some of the
properties, methods, and events in this course.
You have already used recordset objects in some of the exercises. When a Recordset Design-Time control is inserted into a page, it creates a recordset object and some
initialization script[1] to set up some of the object's properties. You can see the script by right-clicking the control and selecting Show Runtime Text. In this lesson you will learn to use the recordset object via script.
The following image and MouseOver shows script from within the source editor of the file Specials. The Textbox DTCs have been replaced with server-side script, and the script is bound to the Recordset DTC to display the data. The purpose of Specials.asp is to display only books that are on special. To do this, we will use script to change the recordset object's default behavior from retrieving all records to retrieving only records who's Status field equals 'S.' In this example, the
onbeforeopen
event is used to change the default query before the
recordset object is opened.
Click the View Image button to see the new browser display.
The SQL query in the BookRecords_onbeforeopen event handler is explicitly assigned. You can also build the query string dynamically. For example, you could pass the Status
value from another page and capture it in Specials.asp using the Request
object, and then use routine string concatenation to build the query string. In the next lesson, you will see how error-handling code is added to an ASP page.
[1]initialization script: Commonly used to set initial values of variables, properties, and so on. Initialization code can be executed as soon as the Web page is loaded to make global settings, or in a function or procedure to make local settings.