Performing Batch Updates (VB)
Download Sample App or Download PDF
Learn how to create a fully-editable DataList where all of its items are in edit mode and whose values can be saved by clicking an “Update All” button on the page.
Introduction
In the preceding tutorial we examined how to create an item-level DataList. Like the standard editable GridView, each item in the DataList included an Edit button that, when clicked, would make the item editable. While this item-level editing works well for data that is only updated occasionally, certain use case scenarios require the user to edit many records. If a user needs to edit dozens of records and is forced to click Edit, make their changes, and click Update for each one, the amount of clicking can hamper her productivity. In such situations, a better option is to provide a fully-editable DataList, one where all of its items are in edit mode and whose values can be edited by clicking an Update All button on the page (see Figure 1).
Figure 1: Each Item in a Fully Editable DataList can be Modified (Click to view full-size image)
In this tutorial we’ll examine how to enable users to update suppliers address information using a fully editable DataList.
Step 1: Create the Editable User Interface in the DataList s ItemTemplate
In the preceding tutorial, where we creating a standard, item-level editable DataList, we used two templates:
ItemTemplate
contained the read-only user interface (the Label Web controls for displaying each product s name and price).EditItemTemplate
contained the edit mode user interface (the two TextBox Web controls).
The DataList s EditItemIndex
property dictates what DataListItem
(if any) is rendered using the EditItemTemplate
. In particular, the DataListItem
whose ItemIndex
value matches the DataList s EditItemIndex
property is rendered using the EditItemTemplate
. This model works well when only one item can be edited at a time, but falls apart when creating a fully-editable DataList.
For a fully editable DataList, we want all of the DataListItem
s to render using the editable interface. The simplest way to accomplish this is to define the editable interface in the ItemTemplate
. For modifying the suppliers address information, the editable interface contains the supplier name as text and then TextBoxes for the address, city, and country values.
Start by opening the BatchUpdate.aspx
page, add a DataList control, and set its ID
property to Suppliers
. From the DataList s smart tag, opt to add a new ObjectDataSource control named SuppliersDataSource
.
Figure 2: Create a New ObjectDataSource Named SuppliersDataSource
(Click to view full-size image)
Configure the ObjectDataSource to retrieve data using the SuppliersBLL
class s GetSuppliers()
method (see Figure 3). As with the preceding tutorial, rather than updating the supplier information through the ObjectDataSource, we’ll work directly with the Business Logic Layer. Therefore, set the drop-down list to (None) in the UPDATE tab (see Figure 4).
Figure 3: Retrieve Supplier Information Using the GetSuppliers()
Method (Click to view full-size image)
Figure 4: Set the Drop-Down List to (None) in the UPDATE Tab (Click to view full-size image)
After completing the wizard, Visual Studio automatically generates the DataList s ItemTemplate
to display each data field returned by the data source in a Label Web control. We need to modify this template so that it provides the editing interface instead. The ItemTemplate
can be customized through the Designer using the Edit Templates option from the DataList s smart tag or directly through the declarative syntax.
Take a moment to create an editing interface that displays the supplier s name as text, but includes TextBoxes for the supplier s address, city, and country values. After making these changes, your page s declarative syntax should look similar to the following:
[!code-aspxMain]
1: <asp:DataList ID="Suppliers" runat="server" DataKeyField="SupplierID"
2: DataSourceID="SuppliersDataSource">
3: <ItemTemplate>
4: <h4><asp:Label ID="CompanyNameLabel" runat="server"
5: Text='<%# Eval("CompanyName") %>' /></h4>
6: <table border="0">
7: <tr>
8: <td class="SupplierPropertyLabel">Address:</td>
9: <td class="SupplierPropertyValue">
10: <asp:TextBox ID="Address" runat="server"
11: Text='<%# Eval("Address") %>' />
12: </td>
13: </tr>
14: <tr>
15: <td class="SupplierPropertyLabel">City:</td>
16: <td class="SupplierPropertyValue">
17: <asp:TextBox ID="City" runat="server"
18: Text='<%# Eval("City") %>' />
19: </td>
20: </tr>
21: <tr>
22: <td class="SupplierPropertyLabel">Country:</td>
23: <td class="SupplierPropertyValue">
24: <asp:TextBox ID="Country" runat="server"
25: Text='<%# Eval("Country") %>' />
26: </td>
27: </tr>
28: </table>
29: <br />
30: </ItemTemplate>
31: </asp:DataList>
32: <asp:ObjectDataSource ID="SuppliersDataSource" runat="server"
33: OldValuesParameterFormatString="original_{0}"
34: SelectMethod="GetSuppliers" TypeName="SuppliersBLL">
35: </asp:ObjectDataSource>
[!NOTE] As with the preceding tutorial, the DataList in this tutorial must have its view state enabled.
In the ItemTemplate
I m using two new CSS classes, SupplierPropertyLabel
and SupplierPropertyValue
, which have been added to the Styles.css
class and configured to use the same style settings as the ProductPropertyLabel
and ProductPropertyValue
CSS classes.
[!code-cssMain]
1: .ProductPropertyLabel, .SupplierPropertyLabel
2: {
3: font-weight: bold;
4: text-align: right;
5: }
6: .ProductPropertyValue, .SupplierPropertyValue
7: {
8: padding-right: 35px;
9: }
After making these changes, visit this page through a browser. As Figure 5 shows, each DataList item displays the supplier name as text and uses TextBoxes to display the address, city, and country.
Figure 5: Each Supplier in the DataList is Editable (Click to view full-size image)
Step 2: Adding an Update All Button
While each supplier in Figure 5 has its address, city, and country fields displayed in a TextBox, there currently is no Update button available. Rather than having an Update button per item, with fully editable DataLists there is typically a single Update All button on the page that, when clicked, updates all of the records in the DataList. For this tutorial, let s add two Update All buttons - one at the top of the page and one at the bottom (although clicking either button will have the same effect).
Start by adding a Button Web control above the DataList and set its ID
property to UpdateAll1
. Next, add the second Button Web control beneath the DataList, setting its ID
to UpdateAll2
. Set the Text
properties for the two Buttons to Update All. Lastly, create event handlers for both Buttons Click
events. Rather than duplicating the update logic in each of the event handlers, let s refactor that logic to a third method, UpdateAllSupplierAddresses
, having the event handlers simply invoking this third method.
[!code-vbMain]
1: Protected Sub UpdateAll1_Click(sender As Object, e As EventArgs) _
2: Handles UpdateAll1.Click
3: UpdateAllSupplierAddresses()
4: End Sub
5: Protected Sub UpdateAll2_Click(sender As Object, e As EventArgs) _
6: Handles UpdateAll2.Click
7: UpdateAllSupplierAddresses()
8: End Sub
9: Private Sub UpdateAllSupplierAddresses()
10: ' TODO: Write code to update _all_ of the supplier addresses in the DataList
11: End Sub
Figure 6 shows the page after the Update All buttons have been added.
Figure 6: Two Update All Buttons have been Added to the Page (Click to view full-size image)
Step 3: Updating All of the Suppliers Address Information
With all of the DataList s items displaying the editing interface and with the addition of the Update All buttons, all that remains is writing the code to perform the batch update. Specifically, we need to loop through the DataList s items and call the SuppliersBLL
class s UpdateSupplierAddress
method for each one.
The collection of DataListItem
instances that makeup the DataList can be accessed via the DataList s Items
property. With a reference to a DataListItem
, we can grab the corresponding SupplierID
from the DataKeys
collection and programmatically reference the TextBox Web controls within the ItemTemplate
as the following code illustrates:
[!code-vbMain]
1: Private Sub UpdateAllSupplierAddresses()
2: ' Create an instance of the SuppliersBLL class
3: Dim suppliersAPI As New SuppliersBLL()
4: ' Iterate through the DataList's items
5: For Each item As DataListItem In Suppliers.Items
6: ' Get the supplierID from the DataKeys collection
7: Dim supplierID As Integer = Convert.ToInt32(Suppliers.DataKeys(item.ItemIndex))
8: ' Read in the user-entered values
9: Dim address As TextBox = CType(item.FindControl("Address"), TextBox)
10: Dim city As TextBox = CType(item.FindControl("City"), TextBox)
11: Dim country As TextBox = CType(item.FindControl("Country"), TextBox)
12: Dim addressValue As String = Nothing, _
13: cityValue As String = Nothing, _
14: countryValue As String = Nothing
15: If address.Text.Trim().Length > 0 Then
16: addressValue = address.Text.Trim()
17: End If
18: If city.Text.Trim().Length > 0 Then
19: cityValue = city.Text.Trim()
20: End If
21: If country.Text.Trim().Length > 0 Then
22: countryValue = country.Text.Trim()
23: End If
24: ' Call the SuppliersBLL class's UpdateSupplierAddress method
25: suppliersAPI.UpdateSupplierAddress _
26: (supplierID, addressValue, cityValue, countryValue)
27: Next
28: End Sub
When the user clicks one of the Update All buttons, the UpdateAllSupplierAddresses
method iterates through each DataListItem
in the Suppliers
DataList and calls the SuppliersBLL
class s UpdateSupplierAddress
method, passing in the corresponding values. A non-entered value for address, city, or country passes is a value of Nothing
to UpdateSupplierAddress
(rather than a blank string), which results in a database NULL
for the underlying record s fields.
[!NOTE] As an enhancement, you may want to add a status Label Web control to the page that provides some confirmation message after the batch update is performed.
Updating Only Those Addresses That Have Been Modified
The batch update algorithm used for this tutorial calls the UpdateSupplierAddress
method for every supplier in the DataList, regardless of whether their address information has been changed. While such blind updates aren t usually a performance issue, they can lead to superfluous records if you re auditing changes to the database table. For example, if you use triggers to record all UPDATE
s to the Suppliers
table to an auditing table, every time a user clicks the Update All button a new audit record will be created for each supplier in the system, regardless of whether the user made any changes.
The ADO.NET DataTable and DataAdapter classes are designed to support batch updates where only modified, deleted, and new records results in any database communication. Each row in the DataTable has a RowState
property that indicates whether the row has been added to the DataTable, deleted from it, modified, or remains unchanged. When a DataTable is initially populated, all rows are marked unchanged. Changing the value of any of the row s columns marks the row as modified.
In the SuppliersBLL
class we update the specified supplier s address information by first reading in the single supplier record into a SuppliersDataTable
and then set the Address
, City
, and Country
column values using the following code:
[!code-vbMain]
1: Public Function UpdateSupplierAddress _
2: (supplierID As Integer, address As String, city As String, country As String) _
3: As Boolean
4: Dim suppliers As Northwind.SuppliersDataTable = _
5: Adapter.GetSupplierBySupplierID(supplierID)
6: If suppliers.Count = 0 Then
7: ' no matching record found, return false
8: Return False
9: Else
10: Dim supplier As Northwind.SuppliersRow = suppliers(0)
11: If address Is Nothing Then
12: supplier.SetAddressNull()
13: Else
14: supplier.Address = address
15: End If
16: If city Is Nothing Then
17: supplier.SetCityNull()
18: Else
19: supplier.City = city
20: End If
21: If country Is Nothing Then
22: supplier.SetCountryNull()
23: Else
24: supplier.Country = country
25: End If
26: ' Update the supplier Address-related information
27: Dim rowsAffected As Integer = Adapter.Update(supplier)
28: ' Return true if precisely one row was updated, otherwise false
29: Return rowsAffected = 1
30: End If
31: End Function
This code naively assigns the passed-in address, city, and country values to the SuppliersRow
in the SuppliersDataTable
regardless of whether or not the values have changed. These modifications cause the SuppliersRow
s RowState
property to be marked as modified. When the Data Access Layer s Update
method is called, it sees that the SupplierRow
has been modified and therefore sends an UPDATE
command to the database.
Imagine, however, that we added code to this method to only assign the passed-in address, city, and country values if they differ from the SuppliersRow
s existing values. In the case where the address, city, and country are the same as the existing data, no changes will be made and the SupplierRow
s RowState
will be left marked as unchanged. The net result is that when the DAL s Update
method is called, no database call will be made because the SuppliersRow
has not been modified.
To enact this change, replace the statements that blindly assign the passed-in address, city, and country values with the following code:
[!code-vbMain]
1: ' Only assign the values to the SupplierRow's column values if they differ
2: If address Is Nothing AndAlso Not supplier.IsAddressNull() Then
3: supplier.SetAddressNull()
4: ElseIf (address IsNot Nothing AndAlso supplier.IsAddressNull) _
5: OrElse (Not supplier.IsAddressNull() AndAlso _
6: String.Compare(supplier.Address, address) <> 0) Then
7: supplier.Address = address
8: End If
9: If city Is Nothing AndAlso Not supplier.IsCityNull() Then
10: supplier.SetCityNull()
11: ElseIf (city IsNot Nothing AndAlso supplier.IsCityNull) _
12: OrElse (Not supplier.IsCityNull() AndAlso _
13: String.Compare(supplier.City, city) <> 0) Then
14: supplier.City = city
15: End If
16: If country Is Nothing AndAlso Not supplier.IsCountryNull() Then
17: supplier.SetCountryNull()
18: ElseIf (country IsNot Nothing AndAlso supplier.IsCountryNull) _
19: OrElse (Not supplier.IsCountryNull() AndAlso _
20: String.Compare(supplier.Country, country) <> 0) Then
21: supplier.Country = country
22: End If
With this added code, the DAL s Update
method sends an UPDATE
statement to the database for only those records whose address-related values have changed.
Alternatively, we could keep track of whether there are any differences between the passed-in address fields and the database data and, if there are none, simply bypass the call to the DAL s Update
method. This approach works well if you re using the DB direct method, since the DB direct method isn t passed a SuppliersRow
instance whose RowState
can be checked to determine whether a database call is actually needed.
[!NOTE] Each time the
UpdateSupplierAddress
method is invoked, a call is made to the database to retrieve information about the updated record. Then, if there are any changes in data, another call to the database is made to update the table row. This workflow could be optimized by creating anUpdateSupplierAddress
method overload that accepts anEmployeesDataTable
instance that has all of the changes from theBatchUpdate.aspx
page. Then, it could make one call to the database to get all of the records from theSuppliers
table. The two resultsets could then be enumerated and only those records where changes have occurred could be updated.
Summary
In this tutorial we saw how to create a fully editable DataList, allowing a user to quickly modify the address information for multiple suppliers. We started by defining the editing interface a TextBox Web control for the supplier s address, city, and country values in the DataList s ItemTemplate
. Next, we added Update All buttons above and below the DataList. After a user has made his changes and clicked one of the Update All buttons, the DataListItem
s are enumerated and a call to the SuppliersBLL
class s UpdateSupplierAddress
method is made.
Happy Programming!
About the Author
Scott Mitchell, author of seven ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer. His latest book is Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell@4GuysFromRolla.com. or via his blog, which can be found at http://ScottOnWriting.NET.
Special Thanks To
This tutorial series was reviewed by many helpful reviewers. Lead reviewers for this tutorial were Zack Jones and Ken Pespisa. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell@4GuysFromRolla.com.
|