Skip to main content

Dynamic Query Example

Complete the following steps to use a dynamic query with CSP:

  1. Using Studio, create a new CSP page named MySamplePage2.CSP in csp/user.

  2. Add the following script to the page. The script defines a query that retrieves the PhoneNumberType and Number values for each PhoneNumber whose Contact value matches the input value. Note that the script retrieves the Contact value from %request. This technique is discussed later in the tutorial.

    
    <script language="sql" name="rs" p1="#($Get(%request.Data("ID",1)))#"
    mode="display">
    SELECT PhoneNumberType, Number FROM CSPTutorial.PhoneNumber WHERE Contact = ?
    </script>
    
  3. Add the following <csp:while> tag to the page after the above script. The tag iterates through the query's result set and displays the query results.

    
    <csp:while condition="rs.Next()">
    <p>#(rs.Data("PhoneNumberType"))# #(rs.Data("Number"))#</p>
    </csp:while>
    
  4. Finally, add the following form to MySamplePage2.CSP. The form contains a single text field and a Submit button. When the user presses the button, the value in the form field is submitted to the page. This becomes the search value used in the dynamic query.

    
    <form>
    <p>Contact ID: <input type="text" size="5" name="ID"/></p>
    <input type="SUBMIT" Name="Search" Value="Search"/> 
    </form>
    
  5. Open MySamplePage2.CSP in a Web browser. Enter a valid Contact ID value in the text field.

    generated description: dynqueryexample20142

  6. Click the Search button. The page displays the phone numbers for the Contact with ID value 1.

    generated description: dynqueryexample220142

FeedbackOpens in a new tab