Data Source Control_ Parameters
-
Upload
amessi-young -
Category
Documents
-
view
219 -
download
0
Embed Size (px)
Transcript of Data Source Control_ Parameters
-
7/29/2019 Data Source Control_ Parameters
1/22
How a Data Source Control Creates
Parameters for Data-bound Fields
-
7/29/2019 Data Source Control_ Parameters
2/22
When you use a data-bound control such as a
GridView, DetailsView, or FormView control with
an ASP.NET data source control, the data-
bound control can pass parameter names andvalues 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.
-
7/29/2019 Data Source Control_ Parameters
3/22
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.
-
7/29/2019 Data Source Control_ Parameters
4/22
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.
-
7/29/2019 Data Source Control_ Parameters
5/22
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.
-
7/29/2019 Data Source Control_ Parameters
6/22
NewValues collection Passed for an updateoperation. Contains the name/value pairs withnew values for the updated item, including newvalues for updatable key fields. Field names andvalues for the NewValues collection are takenfrom child controls in an EditItemTemplate orfrom bound fields in a DetailsView controlwhose ReadOnly property is set to false.
-
7/29/2019 Data Source Control_ Parameters
7/22
OldValues collection Passed for update or deleteoperations. Contains the original values for the datarecord to use for optimistic concurrency checking.
When a data-bound control is populated with data fromthe data source control, it maintains that data in viewstate. When an update or delete operation is requested,the OldValues collection is populated with values storedearlier in view state. If the data-bound control's
EnableViewState property is set to false, theOldValues collection is not populated for the update ordelete operation.
-
7/29/2019 Data Source Control_ Parameters
8/22
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 inthe 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.
-
7/29/2019 Data Source Control_ Parameters
9/22
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 data-
source control's ConflictDetectionproperty isset to CompareAllValues.
-
7/29/2019 Data Source Control_ Parameters
10/22
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 datasource 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 formatof the parameters for original values is determined by the
OldValuesParameterFormatString property.
Set the OldValuesParameterFormatString property toa string that includes "{0}" as a placeholder for the name
of the field.
-
7/29/2019 Data Source Control_ Parameters
11/22
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 aparameter named @old_LastModifiedDate is
created to pass the original value.
-
7/29/2019 Data Source Control_ Parameters
12/22
If a data-bound control such as a GridView control isbound to the SqlDataSource control, during an updateor delete operation the data-bound control passes bothcurrent and original record values to theSqlDataSource control.
The current values are passed in the Valuesdictionary. The original values are passed in the Keysor OldValues dictionaries. The contents of thesedictionaries are appended to the underlyingDbCommand object's Parameters collection for agiven data operation.
-
7/29/2019 Data Source Control_ Parameters
13/22
DbCommand Class
Represents an SQL statement or stored
procedure to execute against a data
source. Provides a base class for
database-specific classes that representcommands.
-
7/29/2019 Data Source Control_ Parameters
14/22
DbCommand.Parameters Property
Gets the collection ofDbParameter
objects.
http://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html/T_System_Data_Common_DbParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html/T_System_Data_Common_DbParameter.htm -
7/29/2019 Data Source Control_ Parameters
15/22
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 theCompareAllValues value, parameters are
created for Name, Number, original_Name, and
original_Number. (The exact name of the
parameters for the original values depends onthe OldValuesParameterFormatString property.)
http://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/P_System_Web_UI_WebControls_SqlDataSource_OldValuesParameterFormatString.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/P_System_Web_UI_WebControls_SqlDataSource_OldValuesParameterFormatString.htm -
7/29/2019 Data Source Control_ Parameters
16/22
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, asnecessary
-
7/29/2019 Data Source Control_ Parameters
17/22
ConflictOptions Enumeration :
CompareAllValuesA data source control uses the
oldValues collection of the Update and Delete
methods to determine whether the data has beenchanged by another process.
OverwriteChangesA data source control overwritesall values in a data row with its own values for the
row.
-
7/29/2019 Data Source Control_ Parameters
18/22
CompareAllValues
If the data source control is configured touse the CompareAllValues option,however, the control passes the original
data in the oldValues collections of theUpdate and Delete methods so that youcan write logic to update or delete dataonly if these values match the values
currently in the data storage. Thematching values indicate that the data hasnot changed since the time it was read.
-
7/29/2019 Data Source Control_ Parameters
19/22
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 thetime the data source control first read data
from the row and the time that the row is
updated.
-
7/29/2019 Data Source Control_ Parameters
20/22
Parameter Class
The Parameterclass 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.
Parameterobjects are contained in a
ParameterCollection object
http://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ParameterCollection.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ParameterCollection.htm -
7/29/2019 Data Source Control_ Parameters
21/22
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 Webapplications to placeholders in SQL query
strings,
http://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ControlParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_CookieParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_SessionParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ProfileParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_QueryStringParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_QueryStringParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ProfileParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_SessionParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_CookieParameter.htmhttp://ms-help//MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/T_System_Web_UI_WebControls_ControlParameter.htm -
7/29/2019 Data Source Control_ Parameters
22/22
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