Modify the connection information

Where you are

   Look at the Demo Database

   Run the Connection Object wizard

   Declare a global variable

>   Modify the connection information

   Complete the login and logout scripts

   Run the application

You can now call the connection service manager to establish a database connection, but you should open a database connection only if the user enters a valid ID and password in the login window. You will therefore add the connection service call to the Clicked event of the OK button on this window, substituting user-entered information for information from the pbtutor.ini file.

However, before you add the call to the OK button, you remove or comment out the ProfileString calls that the connection service manager makes to get user ID and password information from the INI file. Then you modify the DBParm parameter in the pbtutor.ini file, because it includes hard-coded user ID and password values that were copied from the pb.ini file.

In this exercise you:

  • Modify the of_GetConnectionInfo function

  • Call the connection service manager

Modify the of_GetConnectionInfo function

You looked at the of_GetConnectionInfo function in the last exercise. Now you comment out the information that the function returns for the user ID and password information.

If you closed the User Object painter, you must open it again for the n_pbtutor_connectservice user object. You can use the File>Recent Objects menu to redisplay it.

  1. Select Functions in the first drop-down list box in the Script view.

  2. Select of_GetConnectionInfo in the second drop-down list box.

  3. Select the two ProfileString assignment lines that begin:

    as_userid       = ProfileString (...)
    as_dbpass       = ProfileString (...)

    The four arguments of a ProfileString call are the INI file name or variable, the INI file section, the INI file key, and the default value to be used if the INI file name, section, or key is incorrect. These lines are part of the IS_USE_INIFILE case of the CHOOSE CASE statement for the of_GetConnectionInfo function.

  4. Click the Comment button () in PainterBar2.

    By commenting out these lines, you make sure that the user ID and password information do not come from the pbtutor.ini file.

  5. Click anywhere in the line that begins:

    as_dbparm       = ProfileString ( ... )
  6. Click the Comment button in PainterBar2.

    The DBParm parameter in the pbtutor.ini file includes hard-coded values for the user ID and password as well as the database name. You do not use these values. Instead, you assign values to the DBParm parameter from user-entry information for user ID and password.

    About the SQLCA DBParm parameter

    Although the user ID and password are not required for the DBParm ConnectString, assigning them to the ConnectString overwrites SQLCA user ID and password values in the data source definition for an SQL Anywhere database. For this DBMS, the DBParm parameter also takes precedence over the SQLCA UserID and DBPass parameters.

  7. Click the Save button in PainterBar1.

    Click the Close button in PainterBar1.

Call the connection service manager

You will next call the connection service manager to connect to the database. Because you eventually need to add user-entry information from the login window, you add the call to the Clicked event for the OK button on this window.

An object is considered to be the parent of the controls that are added to it. The login window is therefore the parent of the OK button.

When referring to a parent object in a script, it is usually better practice to use the qualifier parent than to name the object explicitly. This allows the code to be reused more easily for controls placed on a different object. In the script for the Clicked event, you refer to the login window as parent.

Using a single wizard to create the application and connection

If you had created the connection service user object with the Template Application wizard, the code you enter in this exercise to call the connection service manager would have been generated automatically, but it would have been added to the application Open event, not to a Clicked event in a login window. It would also have used a local variable, not a global variable.

  1. Double-click w_welcome in the System Tree.

    The Window painter opens.

  2. Select cb_ok in the first drop-down list box of the Script view

    or

    Double-click the OK button in the Layout view.

    The Clicked event should be the selected event in the second drop-down list box. If it is not, select it. The Clicked event script is empty.

  3. Type these lines:

    // 1) Instantiate the Transaction object
    // 2) Close login window if connection successful

    These lines explain the code you add to the Clicked event. Adding double slash marks at the front of a line turns it into a comment.

  4. Type the following assignment statement below the comments:

    gnv_connect = CREATE &
             n_pbtutor_connectservice

    Do not type the ampersand (&) if you combine the lines of the script into a single line. The ampersand character indicates that a line of script is continued on the next line.

    The CREATE statement instantiates the SQLCA Transaction object with all the values retrieved by the of_GetConnectionInfo function from the pbtutor.ini file. Because you previously commented out the lines for the user ID and password, this information is not retrieved.

    For ease of reading, you can add blank lines between the comments and the assignment statement for the global variable gnv_connect.

  5. Type the following lines below the CREATE statement:

    IF gnv_connect.of_ConnectDB ( ) = 0 THEN
             Close (parent)
    END IF

    The of_ConnectDB function connects the application to the database. As you saw earlier in this lesson, if the connection fails (the SQLCode is not 0), a message box opens and displays the SQL error text.

    If of_ConnectDB returns a zero (the SQLCode for a successful connection), the lines that follow the IF-THEN statement line are parsed. In this case, the parent window for the cb_ok control (w_welcome) closes.

  6. Click the Compile button in PainterBar2

    or

    Right-click inside the Script view and click Compile in the pop-up menu.

    The script should compile without error. If you get an error message, make sure you have typed object and function names correctly and saved gnv_connect as a global variable.

    Toggling the Error window of the Script view

    You can show or hide the Error window by clicking the icon at the far right of the Script view just under the title bar.

    You still need to code the Clicked event to instantiate the Transaction object with user-entered connection information.