Updating data
After users have made changes to data in a DataWindow control,
you can use the Update method to save those changes in the database.
In PowerBuilder, the code looks like this:
1 |
dw_emp.<span>Update</span>() |
Update sends to the database all inserts, changes, and deletions
made in the DataWindow control since the last Update method. When
you are using an external transaction object, you can then commit
(or roll back) those database updates. In PowerBuilder, you use
SQL statements. In the Web ActiveX, you use methods and properties
of the transaction object. In the Web DataWindow client control,
update requests call the update method in the server component, which
handles the commit or rollback.
For more specifics on how a DataWindow control
updates the database (that is, which SQL statements are sent in
which situations), see “Updating the database”.
Examples
The following example shows code that connects, retrieves,
updates, commits or rolls back, and disconnects from the database.
Although the example shows all database operations in a single
script or function, most applications separate these operations.
In a PowerBuilder application, for example, an application could
connect to the database in the application Open event, retrieve
and update data in one or more window scripts, and disconnect from
the database in the application Close event.
PowerBuilder
The following statements retrieve and update data using the transaction
object EmpSQL and the DataWindow control dw_emp:
1 |
// Connect to the database specified in the |
1 |
// transaction object EmpSQL |
1 |
CONNECT USING EmpSQL; |
1 |
1 |
// Set EmpSQL as the transaction object for dw_emp |
1 |
dw_emp.SetTransObject(EmpSQL) |
1 |
1 |
// Retrieve data from the database specified in |
1 |
// EmpSQL into dw_emp |
1 |
dw_emp.Retrieve( ) |
1 |
1 |
// Make changes to the data... |
1 |
... |
1 |
1 |
// Update the database |
1 |
IF dw_emp.Update( ) > 0 THEN |
1 |
COMMIT USING EmpSQL; |
1 |
ELSE |
1 |
ROLLBACK USING EmpSQL; |
1 |
END IF |
1 |
1 |
// Disconnect from the database |
1 |
DISCONNECT USING EmpSQL; |
Web ActiveX
The following JavaScript statements retrieve and update data using
the transaction object EmpSQL and the DataWindow control dw_emp.
1 |
// Connect to the database specified in the |
1 |
// transaction object EmpSQL |
1 |
EmpSQL.Connect( ); |
1 |
1 |
// Set EmpSQL as the transaction object for dw_emp |
1 |
dw_emp.SetTransObject(EmpSQL); |
1 |
1 |
// Retrieve data from the database specified in |
1 |
// EmpSQL into dw_emp |
1 |
dw_emp.Retrieve( ); |
1 |
1 |
// Make changes to the data |
1 |
... |
1 |
1 |
// Update the database |
1 |
if (dw_emp.Update( ) > 0) { |
1 |
EmpSQL.Commit( ); |
1 |
} else { |
1 |
EmpSQL.Rollback( ); |
1 |
} |
1 |
1 |
// Disconnect from the database |
1 |
EmpSQL.Disconnect( ); |
Handling retrieval or update errors
A production application should include error tests after
each database operation.For more about checking for errors, see “Handling DataWindow
errors”.