| Home | Previous Lesson: Populating User Defaults in the Login Window Next Lesson: User Defaults � Registry Database |
First let us decide the right place to write the code for saving the user defaults. It is a good idea to write the code in the application's close event.
// Object: Application Object
// Event: Close
Int l_ReturnStatus, l_FileHandle
l_ReturnStatus = SetProfileString("product.ini",&
"product app" , "DBMS", SQLCA.DBMS)
// Let us create the file as it is not existing.
if l_ReturnStatus <> 1 THEN
l_FileHandle = &
FileOpen( "product.ini", LineMode!, &
Write!, LockWrite!, Replace!)
FileClose( l_FileHandle)
SetProfileString( "product.ini" , &
"product app" , "DBMS", SQLCA.DBMS)
End If
SetProfileString( "product.ini" ,&
"product app" , "Database", SQLCA.Database)
SetProfileString( "product.ini" , &
"product app" , "Name", SQLCA.UserID)
In the above code, we are saving the value of DBMS. The function returns -1, if it is unsuccessful. An unsuccessful SetProfileString() means that the file is not there and so we need to create the file. FileOpen() opens a file in the specified mode (write mode) with specified locks on the file (Write lock). FileOpen() creates the file if the file is not existing. FileClose() closes the file. Once the file is created, we are setting the DBMS value.
We are setting the values by reading from the SQLCA object, recall that we set values to SQLCA in the w_login window. Whatever values you populate in the SQLCA will remain there till you close the application, unless you modify them in between.
Run the application and see whether it is working. It is a good idea to see the contents of "product.ini" and how they looks like.
[product app]
DBMS=ODBC
Database=product
Name=DBA
| Home | Previous Lesson: Populating User Defaults in the Login Window Next Lesson: User Defaults � Registry Database |