Editare

Partajați prin


ParameterCollection.Add Method

Definition

Adds a Parameter object to the collection.

Overloads

Add(Parameter)

Appends the specified Parameter object to the end of the collection.

Add(String, String)

Creates a Parameter object with the specified name and default value, and appends it to the end of the collection.

Add(String, DbType, String)

Creates a Parameter object with the specified name, database type, and default value, and adds it to the end of the collection.

Add(String, TypeCode, String)

Creates a Parameter object with the specified name, TypeCode, and default value, and appends it to the end of the collection.

Add(Parameter)

Appends the specified Parameter object to the end of the collection.

public:
 int Add(System::Web::UI::WebControls::Parameter ^ parameter);
public int Add (System.Web.UI.WebControls.Parameter parameter);
member this.Add : System.Web.UI.WebControls.Parameter -> int
Public Function Add (parameter As Parameter) As Integer

Parameters

parameter
Parameter

The Parameter to append to the collection.

Returns

The index value of the added item.

Examples

The following code example demonstrates how to use an AccessDataSource control and a FormParameter object to display information from a Microsoft Access database in a GridView control. The FormParameter object is added to the SelectParameters collection using the Add(Parameter) method.

Important

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.

<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

void Page_Load(Object sender, EventArgs e){

  // You can add a FormParameter to the AccessDataSource control's
  // SelectParameters collection programmatically.
  AccessDataSource1.SelectParameters.Clear();

  // Security Note: The AccessDataSource 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 Selecting event.

  FormParameter formParam = new FormParameter("lastname","LastNameBox");
  formParam.Type=TypeCode.String;
  AccessDataSource1.SelectParameters.Add(formParam);
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataSet"
          datafile="Northwind.mdb"
          selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
                         FROM Orders WHERE EmployeeID =
                         (SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
      </asp:accessdatasource>

      <br />Enter the name "Davolio" or "King" in the text box and click the button.

      <br />
      <asp:textbox
        id="LastNameBox"
        runat="server" />

      <br />
      <asp:button
        id="Button1"
        runat="server"
        text="Get Records" />

      <br />
      <asp:gridview
          id="GridView1"
          runat="server"
          allowsorting="True"
          datasourceid="AccessDataSource1">
      </asp:gridview>

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Private Sub Page_Load(sender As Object, e As EventArgs)

  ' You can add a FormParameter to the AccessDataSource control's
  ' SelectParameters collection programmatically.
  AccessDataSource1.SelectParameters.Clear()

  ' Security Note: The AccessDataSource 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 Selecting event.

  Dim formParam As New FormParameter("lastname","LastNameBox")
  formParam.Type=TypeCode.String
  AccessDataSource1.SelectParameters.Add(formParam)
End Sub ' Page_Load

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataSet"
          datafile="Northwind.mdb"
          selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
                         FROM Orders WHERE EmployeeID =
                         (SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
      </asp:accessdatasource>

      <br />Enter the name "Davolio" or "King" in the text box and click the button.

      <br />
      <asp:textbox
        id="LastNameBox"
        runat="server" />

      <br />
      <asp:button
        id="Button1"
        runat="server"
        text="Get Records" />

      <br />
      <asp:gridview
          id="GridView1"
          runat="server"
          allowsorting="True"
          datasourceid="AccessDataSource1">
      </asp:gridview>

    </form>
  </body>
</html>

Remarks

Use the Add(Parameter) method to append a Parameter object to the end of the collection. This implementation of the method takes the Parameter object specified by the param parameter and appends it to the collection.

See also

Applies to

Add(String, String)

Creates a Parameter object with the specified name and default value, and appends it to the end of the collection.

public:
 int Add(System::String ^ name, System::String ^ value);
public int Add (string name, string value);
member this.Add : string * string -> int
Public Function Add (name As String, value As String) As Integer

Parameters

name
String

The name of the parameter.

value
String

A string that serves as a default value for the parameter.

Returns

The index value of the added item.

Examples

The following code example demonstrates how the Add(String, String) method can be used to add new Parameter objects to a ParameterCollection collection by supplying the name and value parameters. In this example, a Parameter object is added to an Update command of an Access data source control that is bound to the value of a TextBox control.

Important

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.

<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString());
      MyAccessDataSource.UpdateParameters.Add("orderid", oid);
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())

      MyAccessDataSource.UpdateParameters.Add("orderid", oid)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>

Remarks

Use the Add(String, String) method to create and append a Parameter object with a default value to the end of the collection. This implementation of the method creates the Parameter object using the name and default value specified by the name and value parameters, respectively, and appends it to the collection.

See also

Applies to

Add(String, DbType, String)

Creates a Parameter object with the specified name, database type, and default value, and adds it to the end of the collection.

public:
 int Add(System::String ^ name, System::Data::DbType dbType, System::String ^ value);
public int Add (string name, System.Data.DbType dbType, string value);
member this.Add : string * System.Data.DbType * string -> int
Public Function Add (name As String, dbType As DbType, value As String) As Integer

Parameters

name
String

The name of the parameter.

dbType
DbType

The database type of the parameter.

value
String

The default value for the parameter.

Returns

The index value of the added item.

Remarks

This method is for database types. Use the Add(String, TypeCode, String) method for CLR types.

Applies to

Add(String, TypeCode, String)

Creates a Parameter object with the specified name, TypeCode, and default value, and appends it to the end of the collection.

public:
 int Add(System::String ^ name, TypeCode type, System::String ^ value);
public int Add (string name, TypeCode type, string value);
member this.Add : string * TypeCode * string -> int
Public Function Add (name As String, type As TypeCode, value As String) As Integer

Parameters

name
String

The name of the parameter.

type
TypeCode

The type of the parameter.

value
String

The default value for the parameter.

Returns

The index value of the added item.

Examples

The following code example demonstrates how the Add(String, TypeCode, String) method can be used to add new Parameter objects to a ParameterCollection collection by supplying the name, value, and type parameters. In this example, a Parameter object is added to an Update command of an Access data source control that provides the value of the current system time. The parameter is added with the TypeCode of DateTime.

<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString());
      MyAccessDataSource.UpdateParameters.Add("orderid", oid);
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())

      MyAccessDataSource.UpdateParameters.Add("orderid", oid)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>

Remarks

Use the Add(String, TypeCode, String) method to create and append a strongly typed Parameter object with a default value to the end of the collection. This implementation of the method creates the Parameter object using the name, type and value specified by the name, type and value parameters, respectively, and appends it to the collection.

See also

Applies to