Updating data

After users have made changes to data in a DataWindow control, you can use the UpdateData method to save those changes in the database.

The code looks like this in Visual Basic:

dwEmp.UpdateData()

UpdateData sends to the database all inserts, changes, and deletions made in the DataWindow control since the last UpdateData method call. You can then commit (or roll back) those database updates with the Commit and Rollback methods of the Transaction object or of the AdoTransaction object’s Transaction property.

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 module, most applications separate these operations. For example, an application could connect to the database in the form’s Load event and retrieve and update data in one or more button clicked events.

The following C# statements retrieve and update data using the transaction object EmpSQL and the DataWindowControl dwEmp:

// Create an instance of the Transaction object
// and set its parameters
Transaction EmpSQL = new Transaction();
EmpSQL.Dbms = DbmsType.Odbc;

EmpSQL.DbParameter = "ConnectString=
'DSN=EAS Demo DB V110;UID=dba;PWD=sql',
DisableBind=1";
EmpSQL.AutoCommit = false;

// Connect to the database specified in the
// transaction object EmpSQL
EmpSQL.Connect();

// Set EmpSQL as the transaction object for dwEmp
dwEmp.SetTransaction(EmpSQL);

// Retrieve data from the database specified in
// EmpSQL into dwcEmp
dwEmp.Retrieve();

// Make changes to the data
......

// Update the database
try {
   dwEmp.UpdateData();
   EmpSQL.Commit();
}
catch (Exception ex) {
   EmpSQL.Rollback();
}

finally {
   // Disconnect from the database
   EmpSQL.Disconnect();
}

Handling retrieval or update errors

A production application should include more robust error tests after each database operation. For more about checking for errors, see “Handling DataWindow exceptions”.