Adding Update Functionality

You've seen how your GridView control lost some functionality when using an object data source that only provided a method for data retrieval. You will now see how to add methods to update the database encapsulated by the object data source. This will, in turn, enable you to add this functionality to your GridView control, all with only a small amount of code.

1. Select the App_Code/DataAccess.vb (or .cs) tab. To the DataAccess class add methods for updating records in the database, removing the line breaks required for Web presentation.


Public Sub UpdateAuthor(ByVal au_lname As String, ByVal au_fname As String, _
    ByVal phone As String, ByVal address As String, ByVal city As String, ByVal state As String, _
    ByVal zip As String, ByVal contract As Boolean, ByVal Original_au_id As String)
    Dim authorsTableAdapter As New AuthorsDataSetTableAdapters.authorsTableAdapter
    authorsTableAdapter.Update(Original_au_id, au_lname, au_fname, phone, address, city, state, _
        zip, contract, Original_au_id)
End Sub
VB

Notice that there are only two lines of code. You are taking advantage of the new TableAdapter class's inherent support for updating a database. It was able to generate an SQL UPDATE statement automatically based on your SQL SELECT statement. The data access class merely wraps the existing TableAdapter logic, passing in the required arguments.

When you make changes to the data access layer you must update the ObjectDataSource configuration.

2.  Click the Authors.aspx tab. In the Properties window, for the ObjectDataSource, set OldValuesParameterFormatString=Original_{0}. Next, open the ObjectDataSource Tasks menu. Click Configure Data Source and then click Next. In the Define Data Methods step, click the Update tab. For Choose a method select UpdateAuthor. Click Next and then click Finish.

3. Open the GridView Tasks menu. Check Enable Editing, which is now a new option with your updated ObjectDataSource control.

4. Press ESC to close the GridView Tasks menu and then press F5 to run the application.

5. When the page loads click Edit for any row and modify one of the row values. Click Update to push the changes back to the database.

6. Close the browser.

 
HyperLink HyperLink

Powered By ASP.NET v2.0