asp.net 2.0 usage parameters for Advanced data processing

Source: Internet
Author: User
Tags filter connectionstrings

You can process the Select, Update, Insert, delete, and filter events to validate and process the parameter values passed to these operations. To achieve this goal, data-bound controls and data source controls expose the appropriate events. For example, in the updating event of the GridView, you can see the parameter names and values in the keys, NewValues, and oldvalues dictionaries, and they will be passed to the data source. At one end of the data source, you can handle SqlDataSource updating events and see the parameters that are applied to the underlying command object, which will be executed to complete the operation. Similarly, you can handle ObjectDataSource updating events to view or change the dictionary of parameters, which will be used to analyze the appropriate operations of UpdateMethod. You can use these events to add or remove parameters from dictionaries or commands, to change their values, or to simply verify that the input format of the parameters is correct.

Note: In particular, you need to validate the parameter input for the filtering event, because the SQL encoding (encoded) is not obtained until it is applied to the FilterExpression (filter expression) of the associated DataView object.

The following example shows an event that handles multiple data controls to enumerate the collection of parameters passed by the event arguments. Note that this example sets the InsertVisible property of the bound field associated with the OrderID primary key field to False because OrderID is an identity column in the underlying database and should not be passed to the insert operation (the database automatically increases the value when the insert occurs). Also note that in DataKeyNames, the OrderID field is marked as the primary key, so the original value of this field remains in the keys dictionary passed by the data-bound control. The value of the user input control is passed into the NewValues dictionary (except for those fields marked with Readonly=false). The original value of the Non-key field is persisted by the data-bound control in the OldValues dictionary for passing to the data source. These parameter values are appended to the command in the order of NewValues, keys, and oldvalues, although, by default, when the ConflictDetection is set to OverwriteChanges, the data source No oldvalues will be attached. You can see how the data source uses oldvalues later in the "Use Conflict Detection" section.

<script runat= "Server"
Protected Sub enumeratedictionary (ByVal dictionary as System.Collections.Specialized.IOrderedDictionary)
Dim Entry as DictionaryEntry
for all entry in dictionary Response.Write ("<b>" & Server.HTMLEncode (Entry). Key) & "</b>=" & Server.HTMLEncode (entry. Value) & "(" & Server.HTMLEncode (Entry). Value.gettype (). Name) & ") <br/>")
Next
End Sub
Protected Sub Enumeratecommandparameters (ByVal command as System. Data.Common.DbCommand)
Response.Write ("<br/>parameter order in Data source...<br/>")
Dim param as System.Data.Common.DbParameter the
for each param in command. Parameters
Response.Write ("<b>" & Server.HTMLEncode (param). parametername) & "</b>=" & Server.HTMLEncode (param. Value) & "(" & Server.HTMLEncode (param). Value.gettype (). Name) & ") <br/>")
Next
End Sub
Protected Sub Detailsview1_itemuPdating (ByVal sender as Object, ByVal e as System.Web.UI.WebControls.DetailsViewUpdateEventArgs)
Response.Write ("& Lt;br/>new Values passed from detailsview...<br/> ")
Enumeratedictionary (e.newvalues)
Response.Write ("<br/>keys passed from detailsview...<br/>")
Enumeratedictionary (E.keys)

Response.Write ("<br/>old Values passed from detailsview...<br/&G t; ")
Enumeratedictionary (e.oldvalues)
End Sub
Protected Sub Sqldatasource1_updating (ByVal sender as Object, Byva L e as System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
Enumeratecommandparameters (E.command)
E. Cancel = True
Response.Write ("<br/>update canceled")
End Sub

You can change the order of parameters that SqlDataSource attaches to a command by adding a static parameter object to the parameter collection used by the data source. SqlDataSource the parameters passed by the data-bound control according to the order of the parameter objects. This is useful when the ProviderName property of the data source is set to System.Data.OleDb, because it does not support named (named) parameters, so the order of the parameters attached to the command must be the same as the anonymous parameter placeholder ('? ') in the command. ) are matched in order. When we use named parameters, the order of the arguments is irrelevant. You can specify the type attribute of the parameter object to ensure that the value passed by the data-bound control is converted to the appropriate data type before executing the command or method. Similarly, you can set the Size property of the parameter to specify the number of digits dbparameter in the SqlDataSource command (must be used for input/output, output, and return value parameters).

<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:NorthwindOLEDB %>" ID="SqlDataSource1" ProviderName="<%$ ConnectionStrings:NorthwindOLEDB.ProviderName % >" runat="server" SelectCommand="SELECT TOP 10 [OrderID], [OrderDate], [ShipCountry] FROM [Orders]" UpdateCommand="UPDATE [Orders] SET [OrderDate] = ?, [ShipCountry] = ? WHERE [OrderID] = ?" OnUpdating="SqlDataSource1_Updating">
<UpdateParameters>
  <asp:Parameter Name="OrderDate" Type="DateTime" />
  <asp:Parameter Name="ShipCountry" Type="String" />
  <asp:Parameter Name="OrderID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.