Samples for retrieving and displaying data

This section presents separate examples for a Web site target to illustrate the objects and methods you use to:

In these examples, the target page receives the value of a department passed from a link on another page to a target page parameter. It then retrieves the names and IDs of employees who work in that department.

The employee names are then displayed in a table with a link to an employee detail page. The employee ID is in a query string of the link so that it can be used in another query on the detail page.

The complete source for these examples is provided at the end of this section.

Getting the value of a page parameter

The GetParam method for the psDocument object accesses the value of a page parameter. You can assign the value to a variable and use that variable in other scripts on the page.

Example: getting the value of a page parameter This script would appear after the first heading in the file. It assigns the page parameter value to the variable curr_dept. It also writes the department name on the page:

 curr_dept = psDocument.GetParam("Dept"); 
 psDocument.Write("<P>Employees for department <B>");
 psDocument.Write(curr_dept + "</B></P>"); 

Establishing a database connection

The psServer instance is automatically instantiated in your Web target and is available to server scripts on every page. Using psServer methods, you can define new connections at execution time or you can access connections you defined in PowerBuilder. (When you deploy a Web target, the connection information is made available to the application server.)

Example: connecting to a database Here the GetConnection method for psServer instantiates a PSConnectionClass object using the connection profile Employees. If an error occurs, the code calls a WriteError function to display error information:

conn = psServer.GetConnection("Employees");
rows = 0;
 if ( conn.GetError() != null )
{
 WriteError( "GetConnection", conn );
   return;
 } 

The WriteError function is called only if the error object is not null. The script that defines the WriteError function is shown in the next example and is placed in the Head section of the document. The arguments for WriteError are:

The WriteError function calls the GetError method for the connection object to get the first instance of the PSErrorClass object. An error object is available only if an error has occurred; otherwise, GetError returns null.

Handling database errors

After getting error information, the GetError function writes the connection name, error code, error message, and the name of the function that failed in the document. The GetCode and GetMessage methods for PSErrorClass get the error code and message.

Example: handling database errors This is the code for the WriteError function:

function WriteError( function_called, connName )		{		
   errobj = connName.GetError(); 
   str = errobj.GetCode() + " " + errobj.GetMessage();
       psDocument.Write("<P>Error: " ); 
       psDocument.Write ( function_called + " " + str );
       psDocument.Write("</P>"); 
   return; 
}

Using the page parameter in a SQL query

After you establish a connection, you can retrieve data with a SQL statement and store the result set in a PSCursorClass object. To retrieve data, this code:

Example: retrieving and storing data The code that creates the cursor and retrieves data looks like this:

//build a SQL statement
sqlquery = "  SELECT  \"employees\".\"fname\" , "  + 
    " \"employees\".\"lname\" , " + 
    " \"employees\".\"empid\" " + 
    " FROM \"employees\"  " + 
    " WHERE \"employees\".\"deptid\" = " + 
    "'" + curr_dept + "'";
// Do the query and assign the result set to
mycursormycursor = conn.CreateCursor(sqlquery);
if ( conn.GetError() != null ){  
    WriteError( "CreateCursor", conn ); 
return;
}

Displaying the query results in a table with link formatting

After the rows are retrieved, methods for the PSCursorClass object provide access to the data. Code that writes HTML for displaying data is mixed with method calls that get the data from the PSCursorClass object. This code:

For each row in the result set, the code:

Example: processing rows The code that processes the rows looks like this:

rows = mycursor.GetRowCount();
// Write Table start tag
psDocument.Write("<TABLE BORDER=1>");
// Loop over retrieved rows
// where rows variable is the row count
for (var i=0; i < rows;  i++ ) 
{ 
  // Write TR and TD start tags
  psDocument.Write("<TR><TD>"); 

  // Write A element with employee ID in query string
  psDocument.Write( "<A HREF=\"detail.htm?Key=");
  psDocument.Write( mycursor.GetValue(2) + "\">");
  
  // Write first and last names 
  psDocument.Write( mycursor.GetValue(0) + " ");
  psDocument.Write( mycursor.GetValue(1));  

  // Write A, TD, and TR end tags
  psDocument.Write("</A><TD><TR>"); 
  // Go to next row in result set mycursor.MoveNext();
} 
  // Write Table end tag 
  psDocument.Write("</TABLE>");

Complete example

The complete page looks like this in Source view:

<HTML>
<HEAD>
<% 
// function for displaying error information 
function WriteError( function_called, connName )
{    errobj = connName.GetError(); 	
    str = errobj.GetCode() + " " + errobj.GetMessage();
    psDocument.Write("<P>Error: " );  	
    psDocument.Write ( function_called + " " + str );
    psDocument.Write("</P>");
    return;  
} 
%> 
</HEAD>
<BODY>
<H1>Employees</H1>
<P><% // Get page parameter and write value in document 
curr_dept = psDocument.GetParam("Dept"); 
psDocument.Write("<P>Employees for department <B>"); 	 psDocument.Write(curr_dept + "</B></P>");%></P> 

<P><% // Get a connection		
conn = psServer.GetConnection("Employees");
rows = 0; 
if ( conn.GetError() != null ) {       
       WriteError( "GetConnection", conn ); 
       return;  
}
 // Construct the SQL statement
    sqlquery = "  SELECT  \"employees\".\"fname\" , "  + 
    " \"employees\".\"lname\" , " + 
    " \"employees\".\"empid\" " + 
    " FROM \"employees\"  " + 
    " WHERE \"employees\".\"deptid\" = " + 
    "'" + curr_dept + "'"; 
// Retrieve the data 
mycursor = conn.CreateCursor(sqlquery); 
if ( conn.GetError() != null ) {		
     WriteError( "CreateCursor", conn );  	
     return; 
} 
// Get the number of rows retrieved 
rows = mycursor.GetRowCount(); 

psDocument.Write("<TABLE BORDER=1>"); 
for (var i=0; i < rows;  i++ ) {     
    // Write TR and TD start tags 
    psDocument.Write("<TR><TD>"); 	
    // Write A element with employee ID in query string
    psDocument.Write( "<A HREF=\"detail.htm?Key=");
    psDocument.Write( mycursor.GetValue(2) + "\">"); 
    // Write first and last names  	
    psDocument.Write( mycursor.GetValue(0) + " ");
    psDocument.Write( mycursor.GetValue(1)); 	
    psDocument.Write("</A><TD><TR>"); 
    mycursor.MoveNext(); 
} 
psDocument.Write("</TABLE>");
%>
</BODY>
</HTML>