Lesson 4: Adding startup code to your application

This lesson provides the startup code to your application to connect to an UltraLite database.

 Add content to your application
  1. Add the following content to main.htm, immediately before the <a> tag:



    <form name="form">
    <br><td> ID: </td>
        <td> <input type="text" name="ID" size="10"> </td>
    <br><td> Given Name: </td>
        <td> <input type="text" name="GivenName" size="15"> 
        </td>
    <br><td> Surname: </td>
        <td> <input type="text" name="Surname" size="50"> </td>
    <br><td> Street: </td>
        <td> <input type="text" name="Street" size="20"> </td>
    <br><td> City: </td>
        <td> <input type="text" name="City" size="20"> </td>
    <br><td> Phone: </td>
        <td> <input type="text" name="Phone" size="12"> </td>
    <br>
    <br>
    
    <table>
    <tr>
        <td> <input type="button" value="Insert" 
             onclick="ClickInsert();"> </td>
        <td> <input type="button" value="Next" 
             onclick="ClickNext();"> </td>
        <td> <input type="button" value="Prev" 
             onclick="ClickPrev();"> </td>
    </tr>
    <tr>
        <td colspan=3>
        <input type="button" value="Synchronize" 
        onclick="ClickSync();">
        </td>
    </tr>
    </table>
    </form>
  2. Create a JavaScript file c:\tutorial\tutorial.js to provide application logic.

  3. Add the following variables declaration to tutorial.js for the UltraLite Pod object:

    var DB_mgr;
    var Connection;
    var CustomerTable;
  4. Add the following function to tutorial.js to connect to the tutorial database:



    function Connect()
    {
        var     dir;
        var     open_parms;
        var     browser = navigator.platform;
        
        DB_mgr = CreateObject( "iAnywhere.UltraLite.DatabaseManager.Tutorial" );
        if( DB_mgr == null ) {
         alert( "Error: cannot create database manager." );
     return;
        }
        dir = DB_mgr.directory;
        open_parms = "con=tutorial;" + "file_name=" + dir + "\\tutorial.udb";
        try {
     Connection = DB_mgr.reOpenConnection( "tutorial" );    
         if( Connection == null ) {
         Connection = DB_mgr.openConnection( open_parms );
     }
        } catch( ex ) {
         if( DB_mgr.sqlCode != 
             DB_mgr.SQLError.SQLE_ULTRALITE_DATABASE_NOT_FOUND ) {
             alert( "Error: cannot connect to database: " + ex.Message() );
         return;
         }
    }
  5. Use the onload event handler to connect to the database when the application is started. Modify main.htm as follows:

    1. Load tutorial.js by adding the following line immediately before the <body> tag:

      <script src="tutorial.js"></script>
    2. Modify the <body> tag:

      <body onload="Connect();">
  6. Test your application.

    Synchronize the UltraLite tutorial channel. The synchronization application should be connecting to the tutorial database.