Complete the login and logout scripts

Note

Earlier in this lesson, you called the connection service manager from the Clicked event for the login window OK button. Next you add code to the same Clicked event to instantiate the Transaction object with information entered by the user.

You also add code to the login window Cancel button Clicked event and to the application Closed event.

NoteMinimizing typing errors in the Script view If you right-click inside the scripting area, you open a pop-up menu that includes Paste Special commands. You can use these commands to paste statements, objects, functions or even the contents of text files into the event script. This reduces the risk of typing errors. You can also use AutoScript to complete code, as you will see in this lesson.

In this exercise you:

Set up shortcuts for AutoScript

When you are coding scripts, AutoScript provides help by completing the statement you are typing or displaying a list of language elements that are appropriate to insert in the script at the cursor location.

  1. Select Design>Options from the menu bar and click the AutoScript tab.

  2. Make sure all the check boxes in the first three group boxes are selected.

  3. Make sure the Activate Only After A Dot and Automatic Popup check boxes in the fourth group box are cleared, and click OK.

    With these settings, AutoScript provides Help wherever it has enough information to determine what code to insert, but it does not pop up automatically when you pause. By selecting the Statement Templates check box (not selected by default), you can use AutoScript to enter structures for a multiple line PowerScript statement.

  4. Select Tools>Keyboard Shortcuts from the menu bar.

    Expand the Edit node in the tree.

  5. Scroll down and select Activate AutoScript.

    With your cursor in the Press Keys For Shortcut box, press Ctrl+space.

  6. Expand the Edit>Go To node in the Current Menu list and select Next Marker.

    Type Ctrl+M in the shortcut box and click OK.

    Now whenever you want help completing code, you can press Ctrl+space to pop up a list of possible completions. If you paste in a statement or function with comments, you can press Ctrl+M to move to the next comment.

Add code to the OK button Clicked event

As is often the case when you are developing production applications, you get some of the connection properties from an initialization file and some from user input.

For the tutorial application, you should not get the user ID and password from the tutorial INI file. Get them directly from the user in the login window and then pass the database information in a script.

  1. Make sure you are looking at the Clicked event script for the cb_ok control.

    This is the script in which you added the call to the Connection Service object.

  2. Click before the IF-THEN statement.

    Type the following lines:

    //Local variable declarations
    
    string ls_database, ls_userid, ls_password
    
    
    
    //Assignment statements
    
    ls_userid = Trim ( sle_userid.text )
    
    ls_password = Trim ( sle_password.text )
    
    ls_database = "ConnectString='DSN=EAS Demo DB V105;"
    

    With these lines you declare local variables and assign them values. Do not use blank spaces around the = signs in the ConnectString text. Do not worry about the lone single quotation mark. You will add a single quotation mark in the next step to complete the connection script.

    NoteUsing AutoScript to help code the assignment statements When you type the assignment statements, if you type the letters before the underscore in a variable name and then press Ctrl+space, AutoScript pops up a list of possible completions. Use the arrow keys to move to the correct completion and the Tab key to paste it into your script. If you type the underscore and the first letter after the underscore and then press Ctrl+space, AutoScript pastes the completion directly into your script, as long as there is a unique completion.

    The Trim function removes leading and trailing spaces from the user ID and password values passed as arguments to the function from the SingleLineEdit boxes on the login window.

  3. Click after the lines you just added (which follow the CREATE statement) but before the IF-THEN statement.

    Type the following lines:

    //Instantiate with user-entry values
    
    SQLCA.userid = ls_userid
    
    SQLCA.dbpass = ls_password
    
    SQLCA.dbparm = ls_database + "UID=" + &
    
    			ls_userid + ";PWD=" + ls_password + "'"
    

    These lines instantiate SQLCA parameters with values from the SingleLineEdit text boxes.

    The lines must be added to the script after the CREATE statement to keep them from being overwritten with blank values from the Constructor event of the connection service manager. They must be added before the IF-THEN statement or their values are not used by the Transaction object when it is called by the of_ConnectDB function of the connection service manager.

    Shown is the Clicked event script for c b _ o k with the lines of code added in the previous two steps in the text.
  4. 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.

Add code to the Cancel button Clicked event

Now you add code to the Cancel button to stop the application when this button is clicked.

  1. Double-click the Cancel button in the Layout view

    or

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

    The script area for the Cancel button is blank.

  2. Type this one-line script for the Clicked event:

    HALT
    

    This statement terminates the application immediately when the user clicks Cancel on the login window.

  3. Click the Save button in the PainterBar

    or

    Select File>Save from the menu bar.

    PowerBuilder compiles the script.

  4. Click the Close button in the PainterBar

    or

    Select File>Close from the menu bar.

    The Window painter closes.

Add code to the application Close event

Because the connection service manager was called by a global variable, it is still available to the application and does not need to be instantiated again (as it would if you had used a local variable).

Now you call the connection service manager disconnect function to close the database connection.

  1. Double-click the pbtutor application icon in the System Tree.

    The Application painter displays different views of the tutorial application object. The Script view is part of a stack in the default layout, but you might find it easier to detach it from the stack or open a second Script view.

  2. Select close ( ) returns (none) in the second drop-down list box of the Script view.

    There is no code yet for the application Close event.

  3. Type the following lines for the Close event comment:

    Application Close script:
    
    		Disconnect from the database
    
  4. Select all or part of the lines you just added.

    Click the Comment button.

  5. Type the following line below the comment you typed (you can use AutoScript to complete the variable name and the function name):

    gnv_connect.of_DisconnectDB ( )
    
    Shown is the script for the application close event with the entries made during the previous three steps of the lesson

    NoteReleasing memory by setting global variables to null If this were not the application Close event and you no longer needed an instance of the global connection variable, you could release the memory it occupies by calling the SetNull function.

    PowerBuilder also provides a DESTROY statement to destroy object instances. Do not use the DESTROY statement for local or global variables for nonvisual objects. PowerBuilder garbage collection removes any local variables that go out of scope.

  6. Right-click anywhere in the script area of the Script view.

    Click Compile in the pop-up menu.

    PowerBuilder compiles the Close script. If you get an error message, look carefully at the lines you typed to make sure there is no mistyped variable or object name.

  7. Click the Close button in PainterBar1.

    A message box asks if you want to save your changes to the Application object in the application library file.

  8. Click Yes.

    This saves your changes and closes the Application painter.