| Home | Previous Lesson: The Transaction Object Next Lesson: Allowing the User to Cancel the Login |
We populate the Transaction Object (SQLCA) and connect to the database, when the user enters the connection information and clicks on the OK CommandButton. The code for the clicked event of the OK button is as follows:
// Object: cb_ok in window w_login
// Event: clicked
// Declare Variables
int lUserAnswer
// Populate SQLCA with the user input
SQLCA.dbms = sle_dbms.text
SQLCA.database = sle_database.text
SQLCA.userid = sle_name.text
SQLCA.dbpass = sle_password.text
SQLCA.dbparm = "Connectstring='DSN=product'"
// Connect to the databaseasdf
connect;
// Check for errors
if SQLCA.sqlCode <> 0 then
lUserAnswer = MessageBox("Login Error", &
SQLCA.SqlErrText + "~r" + &
"Do you want to try again ? ", Question!, &
YesNo!, 2)
if lUserAnswer = 2 then
halt
else
return 0
end if
else
close(parent)
end if
The first two lines in the code gives an idea about the object and the event to which the code should be written. The first line specifies the object name and the second line tells you the event for which you need to write the code.
After declaring the variables, we are populating the SQLCA properties with the values from the SingleLineEdit controls in the w_login window. We connect to the database using the CONNECT statement and check if the SQLCode property's value is zero or not. Please note that the CONNECT statement is terminated with a semicolon, since, it is an embedded SQL statement.
if SQLCA.SQLCode <> 0 then
If the SQLCode isn't equal to a zero, an error has occurred in the connection and we use the MessageBox() function to display the error message to the user. This dialog box also asks the user if they would like to try again. If they answer a 'no', we halt the execution, but if they answer 'yes', we return them to the login window, to try another combination. Using a YesNo! argument, displays two CommandButtons to the user. If the user selects the first one, i.e., 'Yes', the MessageBox() returns 1 otherwise, 2.
Since this loop is handled programmatically, it is easy to count the aborted attempts while connecting to the database. If this number becomes excessive, there may be a possibility that someone is attempting to hack into one of your databases. At this point, you may alert the network administrator, so that the necessary steps in safeguarding the databases are taken. Let us implement these checks later.
When PowerBuilder encounters the Connect statement, it automatically uses values from the SQLCA object. The full syntax for the Connect statement is:
CONNECT <USING TransactionObjectName>;
To connect to the database using a different transaction object, just specify that transaction object in the Connect statement. If you don't specify any, PowerBuilder uses SQLCA by default. You will learn more on creating transaction object in later sessions.
MessageBox() function comes in different flavors. In the "PowerScript Basics" session, we just gave two arguments; but in the above code we gave more than two. The first argument is the title of the message box, while the second one is the message displayed to the user. Third argument is an icon to be displayed at the left-hand side of the message. Here we asked PowerBuilder to display a question mark. You have few more icons to choose from. Please note that this is an enumerated datatype argument, and ends with an exclamation mark. That means, you have to give the pre-defined values. We advise you to check the PowerBuilder online help for the MessageBox(). The next argument specifies the icons (buttons) to be displayed below the message. PowerBuilder counts the buttons from left to right. For example, if the user clicks the left most button, MessageBox() returns 1 and so on. The next argument specifies the default value to return if the user doesn't click any of the displayed buttons.
If there are database errors, but if the user wants to try again, we are calling return 0. This statement completes the execution of the script and returns. Does this mean PowerBuilder closes the window? No. PowerBuilder gives the control back to the user. Zero is the script execution status. Typically, a zero indicates a successful execution and any non-zero return status indicates an error in the script execution.
Calling the halt command stops the execution and the application ends. If you notice in the above code, you can see that we are not using sle_server control. Don't worry. This value is not required for the SQL Anywhere database, however, you need to have it if you are using Sybase/MS SQL Server.
If everything goes fine, i.e., user connects to the database, we are closing the parent of the cb_ok CommandButton, which is the w_login window itself. When this window is closed, the control goes to the next line in the application object's open event. While testing this window, provide the following details to connect to the product database successfully:
"DBA" for Login Name, "SQL" for Password field, "ODBC" for DBMS field, "product" for Database Name field. Don't type the quotation marks.
| Home | Previous Lesson: The Transaction Object Next Lesson: Allowing the User to Cancel the Login |