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.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public int Update(
	IDictionary keys,
	IDictionary values,
	IDictionary oldValues
)

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.Int32

A 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="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 private void On_Click(Object source, EventArgs e) {
    try {
        SqlDataSource1.Update();
    }
    catch (Exception except) {
        // Handle the Exception.
    }

    Label2.Text="The record was updated successfully!";
 }
</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>

.NET Framework
Available since 2.0
Return to top
Show: