You are on page 1of 22

How a Data Source Control Creates Parameters for Data-bound Fields

When you use a data-bound control such as a GridView, DetailsView, or FormView control with an ASP.NET data source control, the databound control can pass parameter names and values to the data source control based on the bound fields in the data-bound control.

The data source control then includes the field names and values in the parameter collection for select or update operations.

Dictionaries Passed to Data Source Controls


When a data-bound control requests an operation from the data source control, it passes one or more IDictionary collections containing parameter names and values for the requested data operation. The values of the name/value pairs in the dictionary are derived from child controls. For example, in an update operation, the data-bound control reads parameter values from TextBox or CheckBox controls that are displayed in edit mode.

The names for the name/value pairs are taken from the names of the fields bound to child controls and from the field names specified in the DataKeyNames property.

Name/value pairs are passed using the following IDictionary collections :


Values collection Passed for an insert operation. Contains the name/value pairs for a new record. Field names and values for the Values collection are taken from child controls in an InsertItemTemplate or from bound fields in a DetailsView control

Keys collection Passed for update and delete operations. Contains the primary key or keys for the record being updated or deleted.

NewValues collection Passed for an update operation. Contains the name/value pairs with new values for the updated item, including new values for updatable key fields. Field names and values for the NewValues collection are taken from child controls in an EditItemTemplate or from bound fields in a DetailsView control whose ReadOnly property is set to false.

OldValues collection Passed for update or delete operations. Contains the original values for the data record to use for optimistic concurrency checking. When a data-bound control is populated with data from the data source control, it maintains that data in view state. When an update or delete operation is requested, the OldValues collection is populated with values stored earlier in view state. If the data-bound control's EnableViewState property is set to false, the OldValues collection is not populated for the update or delete operation.

Parameter Names
The data source control creates parameters automatically for the values passed in the IDictionary collections. For an insert operation, the data source control populates its InsertParameters collection with values from the name/value pairs in the Values collection. For an update operation, the data source control populates its UpdateParameters collection with values from the name/value pairs in the Keys, NewValues, and OldValues collections.

For a delete operation, the data source control populates its DeleteParameters collection with values from the name/value pairs in the Keys and OldValues collections. The OldValues collection is not populated by default. It is populated only when the datasource control's ConflictDetection property is set to CompareAllValues.

If you need to access both current and original bound values, such as a scenario where you must support optimistic concurrency checks, you can have the data source control create parameters for both current and original values. To do this, you must establish a naming convention for parameters that will contain original values. The format of the parameters for original values is determined by the OldValuesParameterFormatString property.
Set the OldValuesParameterFormatString property to a string that includes "{0}" as a placeholder for the name of the field.

Consider an update operation that involves a field named LastModifiedDate. The current value for the field is passed in the Values dictionary and the original value for the field is passed in the OldValues dictionary. A parameter named @LastModifiedDate is created to pass the current value and a parameter named @old_LastModifiedDate is created to pass the original value.

If a data-bound control such as a GridView control is bound to the SqlDataSource control, during an update or delete operation the data-bound control passes both current and original record values to the SqlDataSource control. The current values are passed in the Values dictionary. The original values are passed in the Keys or OldValues dictionaries. The contents of these dictionaries are appended to the underlying DbCommand object's Parameters collection for a given data operation.

DbCommand Class
Represents an SQL statement or stored procedure to execute against a data source. Provides a base class for database-specific classes that represent commands.

DbCommand.Parameters Property
Gets the collection of DbParameter objects.

SqlDataSource.ConflictDetection Property
The ConflictDetection property determines whether parameters for old and new values are applied to the Update method. If the ConflictDetection property is set to the CompareAllValues value, parameters are created for Name, Number, original_Name, and original_Number. (The exact name of the parameters for the original values depends on the OldValuesParameterFormatString property.)

By setting the ConflictDetection property to the CompareAllValues value, your Update method can then compare the old and new values to the original data source to detect conflicts and handle them, as necessary

ConflictOptions Enumeration :

CompareAllValuesA data source control uses the oldValues collection of the Update and Delete methods to determine whether the data has been changed by another process.
OverwriteChangesA data source control overwrites all values in a data row with its own values for the row.

CompareAllValues
If the data source control is configured to use the CompareAllValues option, however, the control passes the original data in the oldValues collections of the Update and Delete methods so that you can write logic to update or delete data only if these values match the values currently in the data storage. The matching values indicate that the data has not changed since the time it was read.

OverwriteChanges
By default, the ConflictDetection property is set to OverwriteChanges, which means the data source control will overwrite any changes made to a data row between the time the data source control first read data from the row and the time that the row is updated.

Parameter Class
The Parameter class represents a parameter in a parameterized SQL query, a filtering expression, or a business object method call that an ASP.NET data source control uses to select, filter, or modify data.

Parameter objects are contained in a ParameterCollection object

Use the parameter classes that are provided with ASP.NET, including ControlParameter, CookieParameter, SessionParameter, ProfileParameter, and QueryStringParameter, with data source and data-bound controls to build Web-based data applications. These classes are used by data source controls to bind specific kinds of values found in Web applications to placeholders in SQL query strings,

Data source controls typically include a parameter collection for each data operation. When selecting data, you can specify a SelectParameters collection, when updating a data item you can specify an UpdateParameters collection

You might also like