Visual InterDev

setParameter Method

See Also      Applies To

Sets a parameter for a stored procedure or parameterized query that is referenced by the recordset

Syntax

object.setParameter(nIndex, strParameter)

Parameters

object

A script object.

nIndex

An integer that specifies the zero-based index of a particular parameter within the array of parameters.

strParameter

The string value that you want to assign to the parameter.

Remarks

To return a parameter of stored procedure or parameterized query, use the getParameter method.

Note   In JavaScript, all data types in parameters are passed as strings.

For information on parameterized queries and DHTML, see the white paper "Using Parameterized Queries in DHTML" on the Microsoft® Visual InterDev™ Web site at http://www.microsoft.com/vinterdev/.

Example for a stored procedure

This example sets the parameters for the stored procedure identified in a Recordset design-time control that is named "DTCRecordset1." This sample assumes that the Recordset control has been added to the page, that the database object is set to "Stored Procedure," and that the specific stored procedure has been selected in the Object Name field.

In the Implementation Tab of the Recordset design-time control property pages, the value for "Automatically open the recordset" has been unchecked.

In the example, the input parameters are set, the recordset opened, and then the return parameter is retrieved and evaluated for special conditions.

Note that with stored procedures, the parameters array is 0-based (where the "0" parameter is the return value while in script the parameters passed to the stored procedure start with "1").

<script language=javascript runat=server>
function InsertUser(strFName,strLName,strEName,stPassword)
   {
   DTCRecordset1.setParameter(1,strFName);
   DTCRecordset1.setParameter(2,strLName);
   DTCRecordset1.setParameter(3,strEName);
   DTCRecordset1.setParameter(4,stPassword);         
   DTCRecordset1.open();
   // Get the new ID
   var getID = DTCRecordset1.getParameter(0);
   // check the return value.  Either ID or special case
   if (getID < 1)
      {
      var msg = "Sorry, Couldn't add <b>" + strFName + " " + strLName + "</b><br>";
      switch (getID)
         {
         case -1:
            msg += "You are already a user in the system." + "<br>";
         }
         Response.Write(msg);   
      }
   else if (getID >= 1)
      {
      // Formulate the response message
      Response.Write("Welcome " + strFName + " " + strLName + "<p>");
      }
}
</script>

Example for a parameterized query

This example of a parameterized query sets the parameters for the SQL statement identified in the Recordset design-time control named "DTCRecordset1".

This sample assumes that the Recordset design-time control has been added to the page, that the database object is set to "SQL Statement", and that the SQL Statement is "SELECT * FROM AUser WHERE email=?".

In the Implementation Tab of the Recordset design-time control property pages, the value for "Automatically open the recordset" has been un-checked. Parameterized queries only work for ASP pages.

In the example, the input parameter is being set, the recordset opened, and then the parameter is returned to display a warning condition. Note that with parameterized queries, the parameters array is zero-based

<script language=javascript runat=server>
   function UserStatus(strEName)
      {
         DTCRecordset1.setParameter(0,strEName);
         DTCRecordset1.open();
         if (DTCRecordset1.getCount() > 0)
         {
         var strFName = DTCRecordset1.fields.getValue("FName")
         var strLName = DTCRecordset1.fields.getValue("LName")
         Response.Write("Welcome " + strFName + " " + strLName + "<p>");
         }
         else
         {
         Response.Write(DTCRecordset1.getParameter(0) + " is not in the system");
         }
      }
</script>