Contents Index Lesson 5: Write the JScript sample code Write code to synchronize the database

UltraLite ActiveX User's Guide
  Tutorial: An UltraLite Application for Pocket IE
    Lesson 5: Write the JScript sample code

Write code to connect to an UltraLite database


In Pocket IE, UltraLite ActiveX is accessed using JScript embedded on an HTML page. When a page containing a connection script is loaded, a new database connection is created. When the browser moves to another page, all of the objects created on the previous page are discarded. The database connection is thus lost when the page is unloaded.

Losing application state, including database connections, whenever a form changes, is slow and expensive. To avoid this, it is recommended that Pocket IE applications use HTML frames to maintain application state. The database connection can be kept by the frameset document. Pocket IE requires at least two frames within a frameset, and draws a three (3) pixel border between the frames.

In the CustDB sample, the minimal frame defined in topline.htm is a placeholder that displays the application name at the top of the screen. The rest of the screen is used to display the other pages of the application.

To swap forms in and out of the frameset, a JScript function can use the document.location.replace method. For example, the following code fragment closes the current connection and returns to the connect form.

<SCRIPT>
function exitApp() {
  if ( top.Connection != null ) {
    top.Connection.Close();
  }
  document.location.replace("connect.htm");
}
</SCRIPT>
<INPUT NAME="b_Done" TYPE="BUTTON" VALUE="Done" onClick="exitApp()">

The following procedure connects to an UltraLite database. For more information about connecting to an UltraLite database, see Connecting to an UltraLite database.

To connect to the UltraLite database

  1. Start Pocket Internet Explorer.

  2. Open login.htm.

  3. Tap the hyperlink.

    The hyperlink links to frames.htm.

    The code in frames.htm creates a new database manager object and uses it to open a connection to the database specified by a connection string. If the database does not exist, it creates a new one.

    For more information, see Connecting to an UltraLite database.

    <SCRIPT LANGUAGE="JScript">
    var Connection;      // UltraLite Connection
    var DatabaseMgr;   // UltraLite Database Manager
    var CS;         // Current State object
    
    // Initialize connection. Create database if not already present.
    function initDatabase() {
      var   udb, usm;
      udb = "file_name=\\UltraLiteDB\\ul_custapi.udb";
      usm = ";schema_file=\\UltraLiteDB\\ul_custdb.usm";
      CS = new CurrState( 0 );
      DatabaseMgr = new ActiveXObject( "UltraLite.ULDatabaseManager" );
      var bval = DatabaseMgr.ErrorResume;
      DatabaseMgr.ErrorResume = true;
      Connection = DatabaseMgr.OpenConnection( udb );
      if ( DatabaseMgr.LastErrorCode != 0 ) {
          Connection = DatabaseMgr.CreateDatabase( udb + usm );
      }
    }
    // Initialize the database.
    initDatabase();
    </SCRIPT>
  4. A script prompts you to enter an employee ID. Accept the default value of 50.


Contents Index Lesson 5: Write the JScript sample code Write code to synchronize the database