| Home | Previous Lesson: Sharing Data between DataWindows Next Lesson: Saving User Defaults |
We know that by now you might have run the application multiple times. The frustrating about that is, each time you run the application you need to type the database information in the login window. Wouldn't it be nice to see all those details (may be except the password) populated automatically in the login window? It can be done by writing the details to a file when the user closes the application, and reading it back when he logs in again.
Let's learn about writing to a file in the next section. First let's learn about reading an initialization file. To read an initialization file, we need to use the ProfileString() function. It takes four parameters, the file to read, section name, variable name and the default value if the variable is not defined in the file or the file is not found. If the path to the file name is not given, PowerBuilder first checks the working directory and then the PATH. Write the code as follows:
// Object: w_login
// Event: open
sle_DBMS.text = ProfileString("product.ini",&
"product app" , "DBMS" , "")
sle_Database.text = ProfileString("product.ini",&
"product app" , "Database" , "")
sle_Name.text = ProfileString( "product.ini" , &
"product app" , "Name" , "")
The above code reads "c:\workdir\product.ini" file and reads the values under "product app" section. The values are automatically populated into appropriate controls in the w_login window. If the variables are not found, "" is populated in the appropriate control.
You may be wondering why we didn't just hard code the DBMS, database and userid in the open event script. If we do that and later something changes, for example, say you moved your database to Sybase SQL Server, then you need to physically open up the open script for all the applications that you have distributed, to modify the code and reflect the changes. If you use a .INI file, all you need to do is distribute a new .INI file containing the updated information or ask the user to change that file on their computer.
| Home | Previous Lesson: Sharing Data between DataWindows Next Lesson: Saving User Defaults |