Export (0) Print
Expand All

SqlDataSource.Update Method

Performs an update operation using the UpdateCommand SQL string and any parameters that are in the UpdateParameters collection.

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

member Update : unit -> int

Return Value

Type: System.Int32
A value that represents the number of rows updated in the underlying database.

ExceptionCondition
InvalidOperationException

The SqlDataSource cannot establish a connection with the underlying data source.

The Update method is automatically called by the GridView, DetailsView, and FormView controls during postback if the data has been changed. For data that has been changed in other controls, the Update method can be explicitly called on postback during the Load event.

Before the Update operation is performed, the OnUpdating method is called to raise the Updating event. You can handle this event to examine the values of the parameters and to perform any preprocessing before an Update operation.

After the Update operation completes, the OnUpdated method is called to raise the Updated event. You can handle this event to examine any return values and error codes and to perform any post-processing.

The Update method delegates to the Update method of the SqlDataSourceView object that is associated with the SqlDataSource control. To perform an update operation, the SqlDataSourceView builds a DbCommand object using the UpdateCommand text and any associated UpdateParameters properties, and then executes the DbCommand object against the underlying database.

Security noteSecurity Note

Values are inserted into parameters without validation, which is a potential security threat. Use the Updating event to validate parameter values before executing the query. For more information, see Script Exploits Overview.

This section contains two code examples. The first 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 second code example demonstrates how to display data that is retrieved from a Microsoft SQL Server database in a DropDownList control and update the record using a TextBox control.

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.

No code example is currently available or this language may not be supported.

The following code example demonstrates how to display data that is retrieved from a SQL Server database in a DropDownList control and update the record using a TextBox control. The example shows how you can use a DbTransaction object to add transaction context when using the SqlDataSource control to update data.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft