Adobe ColdFusion 8

Updating the database with the cfgridupdate tag

The cfgridupdate tag provides a simple mechanism for updating the database, including inserting and deleting records. It can add, update, and delete records simultaneously. It is convenient because it automatically handles collecting the cfgrid changes from the various form variables, and generates appropriate SQL statements to update your data source.

In most cases, use the cfgridupdate tag to update your database. However, this tag does not provide the complete SQL control that the cfquery tag provides. In particular, the cfgridupdate tag has the following characteristics:

  • You can update only a single table.
  • Rows are deleted first, then rows are inserted, then any changes are made to existing rows. You cannot modify the order of changes.
  • Updating stops when an error occurs. It is possible that some database changes are made, but the tag does not provide any information on them.

Update the data source with the cfgridupdate tag

  1. Create a ColdFusion page with the following contents:
    <html>
    <head>
        <title>Update grid values</title>
    </head>
    <body>
    
    <h3>Updating grid using cfgridupdate tag.</h3>
    
    <cfgridupdate grid="employee_grid"
        datasource="cfdocexamples"
        tablename="Employee">
        
    Click <a href="grid2.cfm">here</a> to display updated grid.
        
        </body>
    </html>
    

  2. Save the file as handle_grid.cfm.
  3. View the grid2.cfm page in your browser, make changes to the grid, and then submit them.

Note: To update a grid cell, modify the cell contents, and then press Return.

Reviewing the code

The following table describes the highlighted code and its function:

Code

Description

<cfgridupdate grid="employee_grid"

Updates the database from the Employee_grid grid.

datasource="cfdocexamples"

Updates the cfdocexamples data source.

tablename="Employee"

Updates the Employee table.