ObjectDataSource.UpdateParameters Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the parameters collection that contains the parameters that are used by the method that is specified by the UpdateMethod property.

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

Public ReadOnly Property UpdateParameters As ParameterCollection

Property Value

Type: System.Web.UI.WebControls.ParameterCollection

A ParameterCollection that contains the parameters used by the method that is specified by the UpdateMethod property.

The names and types of the parameters that are contained in the UpdateParameters collection must match the names and types of the parameters in the UpdateMethod method signature. The parameter names are affected by the OldValuesParameterFormatString property and are case-sensitive. The parameters in the collection depend on the data in the data-bound control, the parameters that are specified declaratively, and the parameters that are added programmatically. For more information, see the "Parameter Merging" section in Update and Using Parameters with the ObjectDataSource Control.

The UpdateParameters property retrieves the UpdateParameters property that is contained by the ObjectDataSourceView object that is associated with the ObjectDataSource control.

For more information about parameter merging, object lifetime, and method resolution, see UpdateMethod.

System_CAPS_security Security Note

You should validate any parameter value that you receive from the client. The runtime simply substitutes the parameter value into the UpdateMethod property.

This section contains two code examples. The first code example demonstrates how to use a DropDownList control, TextBox controls, and several ObjectDataSource objects to update data. The second code example shows the EmployeeLogic class that is used in the first code example.

The following code example demonstrates how to use a DropDownList control, TextBox controls, and several ObjectDataSource controls to update data. The DropDownList displays the name of a Northwind employee, while the TextBox controls are used to enter and update address information. Because the UpdateParameters collection contains a ControlParameter that is bound to the selected value of the DropDownList, the button that raises the Update operation is enabled only after an employee is selected.

System_CAPS_security Security Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<%@ Import namespace="Samples.AspNet.VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

' Add parameters and initialize the user interface
' only if an employee is selected.
Private Sub Page_Load(sender As Object, e As EventArgs)

  ' Be sure the text boxes are initialized with
  ' data from the currently selected employee.
  Dim selectedEmployee As NorthwindEmployee
  selectedEmployee = EmployeeLogic.GetEmployee(DropDownList1.SelectedValue)

  If Not selectedEmployee Is Nothing Then
    AddressBox.Text    = selectedEmployee.Address
    CityBox.Text       = selectedEmployee.City
    PostalCodeBox.Text = selectedEmployee.PostalCode

    Button1.Enabled = True
    Button1.Enabled = False
  End If
End Sub ' Page_Load

' Press the button to update.
Private Sub Btn_UpdateEmployee (sender As Object, e As CommandEventArgs )
End Sub ' Btn_UpdateEmployee

<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>ObjectDataSource - VB Example</title>
    <form id="Form1" method="post" runat="server">

        <!-- The DropDownList is bound to the first ObjectDataSource. -->
          typename="Samples.AspNet.VB.EmployeeLogic" />

          autopostback="True" /></p>

        <!-- The second ObjectDataSource performs the Update. This
             preserves the state of the DropDownList, which otherwise
             would rebind when the DataSourceChanged event is
             raised as a result of an Update operation. -->

        <!-- Security Note: The ObjectDataSource uses a FormParameter,
             Security Note: which does not perform validation of input from the client.
             Security Note: To validate the value of the FormParameter,
             Security Note: handle the Updating event. -->

            <asp:controlparameter name="anID" controlid="DropDownList1" propertyname="SelectedValue" />
            <asp:formparameter name="anAddress" formfield="AddressBox" />
            <asp:formparameter name="aCity" formfield="CityBox" />
            <asp:formparameter name="aPostalCode" formfield="PostalCodeBox" />

          runat="server" /></p>

          runat="server" /></p>

          runat="server" /></p>

          text="Update Employee"
          oncommand="Btn_UpdateEmployee" />


The following code example shows the EmployeeLogic class that is used in the preceding code example.

Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB
' EmployeeLogic is a stateless business object that encapsulates
' the operations one can perform on a NorthwindEmployee object.
Public Class EmployeeLogic

   ' Returns a collection of NorthwindEmployee objects.
   Public Shared Function GetAllEmployees() As ICollection
      Dim al As New ArrayList()

      ' Use the SqlDataSource class to wrap the
      ' ADO.NET code required to query the database.
      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
      Dim sds As New SqlDataSource(cts.ConnectionString, _
                                  "SELECT EmployeeID FROM Employees")
         Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)

         ' Iterate through the Enumeration and create a
         ' NorthwindEmployee object for each ID.
         Dim enumerator As IEnumerator = IDs.GetEnumerator()
         While enumerator.MoveNext()
            ' The IEnumerable contains DataRowView objects.
            Dim row As DataRowView = CType(enumerator.Current,DataRowView)
            Dim id As String = row("EmployeeID").ToString()
            Dim nwe As New NorthwindEmployee(id)
            ' Add the NorthwindEmployee object to the collection.
         End While
         ' If anything strange happens, clean up.
      End Try

      Return al
   End Function 'GetAllEmployees

   Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee
      Dim al As ArrayList = CType(GetAllEmployees(), ArrayList)
      Dim enumerator As IEnumerator = al.GetEnumerator()
      While enumerator.MoveNext()
         ' The IEnumerable contains initialized NorthwindEmployee objects.
         Dim ne As NorthwindEmployee = CType(enumerator.Current,NorthwindEmployee)
         If ne.EmpID.Equals(anID.ToString()) Then
            Return ne
         End If
      End While
      Return Nothing
   End Function 'GetEmployee

   Public Shared Sub UpdateEmployee(ne As NorthwindEmployee)
      Dim retval As Boolean = ne.Update()
      If Not retval Then
         Throw New NorthwindDataException("Employee update failed.")
      End If
   End Sub 'UpdateEmployee

   ' This method is added as a conveniece wrapper on the original
   ' implementation.
   Public Shared Sub UpdateEmployeeWrapper(anID As String, _
                                           anAddress As String, _
                                           aCity As String, _
                                           aPostalCode As String)
      Dim ne As New NorthwindEmployee(anID)
      ne.Address = anAddress
      ne.City = aCity
      ne.PostalCode = aPostalCode
   End Sub 'UpdateEmployeeWrapper
   ' And so on...

End Class 'EmployeeLogic

Public Class NorthwindEmployee

   Public Sub New(anID As Object)
      Me.ID = anID
      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
      Dim conn As New SqlConnection(cts.ConnectionString)
      Dim sc As New SqlCommand(" SELECT FirstName,LastName,Address,City,PostalCode " & _
                               " FROM Employees " & _
                               " WHERE EmployeeID = @empId", conn)

      ' Add the employee ID parameter and set its value.
      sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString())
      Dim sdr As SqlDataReader = Nothing

         sdr = sc.ExecuteReader()

         ' This is not a while loop. It only loops once.
         If Not (sdr Is Nothing) AndAlso sdr.Read() Then
            ' The IEnumerable contains DataRowView objects.
            Me.aFirstName = sdr("FirstName").ToString()
            Me.aLastName = sdr("LastName").ToString()
            Me.aAddress = sdr("Address").ToString()
            Me.aCity = sdr("City").ToString()
            Me.aPostalCode = sdr("PostalCode").ToString()
            Throw New NorthwindDataException("Data not loaded for employee id.")
         End If
            If Not (sdr Is Nothing) Then
            End If
         Catch se As SqlException
            ' Log an event in the Application Event Log.
         End Try
      End Try
   End Sub 'NewNew

   Private ID As Object
   Public ReadOnly Property EmpID() As Object
         Return ID
      End Get
   End Property

   Private aLastName As String
   Public WriteOnly Property LastName() As String
         aLastName = value
      End Set
   End Property

   Private aFirstName As String
   Public WriteOnly Property FirstName() As String
         aFirstName = value
      End Set
   End Property

   Public ReadOnly Property FullName() As String
         Return aFirstName & " " & aLastName
      End Get
   End Property

   Private aAddress As String
   Public Property Address() As String
         Return aAddress
      End Get
         aAddress = value
      End Set
   End Property

   Private aCity As String
   Public Property City() As String
         Return aCity
      End Get
         aCity = value
      End Set
   End Property

   Private aPostalCode As String
   Public Property PostalCode() As String
         Return aPostalCode
      End Get
         aPostalCode = value
      End Set
   End Property

   Public Function Update() As Boolean

      ' Implement Update logic.
      Return True

   End Function 'Update

End Class 'NorthwindEmployee

Friend Class NorthwindDataException
   Inherits Exception

   Public Sub New(msg As String)
   End Sub 'NewNew

End Class 'NorthwindDataException
End Namespace

.NET Framework
Available since 2.0
Return to top