This topic illustrates how to update data in a database. It is important to understand the topic <a href="getdatafromdb.aspx">Getting Data from a Database</a> before continuing.
<p>
Getting Data From a Database discusses retrieving data from a database and into a DataSet, and how the DataSet is separate and distinct from the database. Once the DataSet is loaded, you can modify the data, and the DataSet will track the changes.
<P>
Adding data is done through the Add method on the DataTable. The Add method takes either an array of the expected data columns, or a DataRow.
Note that the DataTable must return a DataRow through the NewRow method. The method returns a DataRow object with the appropriate schema of the DataTable. The new DataRow is independent of the table until it is added to the RowsCollection.
<p>
Changing data in a DataRow can be done by accessing the DataRow and changing the data. You can use the index of the row in the RowsCollection accessed through the Rows property:
where Key is the Primary Key in the table. When using the SQLDataSetCommand, the Key is established from the Database. You can also set the Key if you are not using the database through the PrimaryKey property.
<P>
Removing the Row is done through the Delete method. Note that you do not remove the row from the collection because this information must eventually be sent back to the database. Similarly you can use RejectChanges on the DataSet, in which case the Row is restored.
<div class=code>
dsCustomers.Tables["Customers"].Rows[0].Delete;
</div>
<p>
The original and new values are maintained in the row. The RowChanging event allows you to access both original and new values to decide whether you want the edit to proceed. Because we maintain original and new values, we can establish scenarios such as optimistic locking and key changes.
<p>
To submit the Data from the DataSet into the database, use the Update method on the SQLDataSetCommand.
The example below gets data from a Database using an SQLDataSetCommand, modifies the data within the DataSet, and then submits the data back to the database through the SQLDataSetCommand.