SqlDataSourceView.Update Method (IDictionary, IDictionary, IDictionary)
Performs an update operation using the UpdateCommand SQL string, any parameters that are in the UpdateParameters collection, and the values that are in the specified keys, values, and oldValues collections.
Assembly: System.Web (in System.Web.dll)
Public Function Update ( keys As IDictionary, values As IDictionary, oldValues As IDictionary ) As Integer
Parameters
- keys
-
Type:
System.Collections.IDictionary
An IDictionary of primary keys to use with the UpdateCommand property to perform the update database operation. If there are no keys associated with the query or if the UpdateCommand is not a parameterized SQL query, pass null.
- values
-
Type:
System.Collections.IDictionary
An IDictionary of values to use with the UpdateCommand property to perform the update database operation. If there are no parameters associated with the query or if the UpdateCommand is not a parameterized SQL query, pass null.
- oldValues
-
Type:
System.Collections.IDictionary
An IDictionary that represents the original values in the database. If there are no parameters associated with the query or if the UpdateCommand is not a parameterized SQL query, pass null.
Return Value
Type: System.Int32A value that represents the number of rows updated in the underlying database.
| Exception | Condition |
|---|---|
| InvalidOperationException | The SqlDataSource cannot establish a connection with the underlying data source. |
| NotSupportedException | The CanUpdate property is false. |
The Update method calls the ExecuteUpdate method, passing the keys, values, and oldValues parameters. For more information, see SqlDataSource.UpdateCommand.
The following code example demonstrates how to use a SqlDataSource control to display data in a DropDownList control and update data when the Submit button is clicked. The UpdateCommand property is set with a parameterized SQL statement and two ControlParameter parameters are added to the UpdateParameters collection. When the Submit button is clicked, the OnClick event is handled to call the Update method explicitly.
<%@Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub On_Click(ByVal source As Object, ByVal e As EventArgs) Try SqlDataSource1.Update() Catch except As Exception ' Handle the Exception. End Try Label2.Text="The record was updated successfully!" End Sub 'On_Click </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees" UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID"> <UpdateParameters> <asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/> <asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/> </UpdateParameters> </asp:SqlDataSource> <asp:DropDownList id="DropDownList1" runat="server" DataTextField="LastName" DataValueField="EmployeeID" DataSourceID="SqlDataSource1"> </asp:DropDownList> <br /> <asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user." AssociatedControlID="TextBox1" /> <asp:TextBox id="TextBox1" runat="server" /> <asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" /> <br /><asp:Label id="Label2" runat="server" Text="" /> </form> </body> </html>
Available since 2.0