Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » UltraLite - M-Business Anywhere Programming » Tutorial: A Sample Application for M-Business Anywhere

Lesson 3: Set up the M-Business Anywhere Server and Client Next Page

Lesson 4: Add startup code to your application


In this lesson you add startup code to your application that connects to an UltraLite database. This requires adding HTML to the main page, and adding JavaScript logic to control the application.

Add content to your application
  1. Add content to main.html.

    Add the following form to your application's main page, c:\Tutorial\mbus\main.html, immediately after the <a> tag:

    <form name="custForm">
    <input type="text" name="fname" size="15"><br>
    <input type="text" name="lname" size="20"><br>
    <input type="text" name="city" size="20"><br>
    <input type="text" name="phone" size="12"><br>
    <input type="text" name="custid" size="10"><br>
    <br><br>
    
    <table>
     <tr>
      <td><input type="button" 
           value="Insert" onclick="ClickInsert();">
      </td>
      <td><input type="button" 
           value="Update" onclick="ClickUpdate();">
      </td>
      <td>
         <input type="button" 
          value="Delete" onclick="ClickDelete();">
      </td>
     </tr>
     <tr>
      <td>
        <input type="button" 
         value="Next" onclick="ClickNext();">
      </td>
      <td>
        <input type="button" 
         value="Prev" onclick="ClickPrev();">
      </td>
      <td></td>
     </tr>
     <tr>
       <td colspan=3>
        <input type="button" 
         value="Synchronize" onclick="ClickSync();">
       </td>
     </tr>
    </table>
    </form>
  2. Create a JavaScript file c:\Tutorial\mbus\tutorial.js that provides application logic.

  3. Add content to the JavaScript file:

    Add the following code to the top of the file to declare the required UltraLite objects:

    var DatabaseMgr;
    var Connection;
    var CustomerTable;

    Add connection code:

    function Connect()
    {
      DatabaseMgr = CreateObject( "iAnywhere.Data.UltraLite.DatabaseManager.Tutorial" );
      if( DatabaseMgr == null ) {
        alert( "Error, make sure POD is on device" );
        return;
      }
    
      var connParms = DatabaseMgr.createConnectionParms();
      var dir = DatabaseMgr.directory;
    
      connParms.schemaOnPalm = "tutCustomer";
      connParms.databaseOnPalm = "Syb3";
    
      connParms.databaseOnCE = dir + "\\tutCustomer.udb";
      connParms.databaseOnDesktop = dir + "\\tutCustomer.udb";
    
      connParms.userID = "DBA";
      connParms.password = "sql";
      try {
        // try to connect to an existing database
        Connection = DatabaseMgr.openConnection( connParms.ToString() );
        alert("Connected to an existing database");
      } catch( ex ) {
        if( DatabaseMgr.sqlCode != DatabaseMgr.SQLError.SQLE_ULTRALITE_DATABASE_NOT_FOUND ) {
          alert( ex.getMessage() );
          return;
        } else {
          try {
            // the database does not exist, create one
            Connection = DatabaseMgr.createDatabase( connParms.ToString() );
            alert("Created a new database");
          } catch( ex2 ) {
            alert( ex2.getMessage() );
            return;
          }
        }
      }
    }
  4. Use the onload event handler to connect to the database when the application is started:

    1. Import tutorial.js into main.html by adding the following line immediately before the <body> tag:

      <script src="tutorial.js"></script>
    2. Edit main.html and change the <body> tag to the following:

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

    Synchronize your M-Business Client and start the application. A message box appears when your application creates the UltraLite database. Once this is working properly, you can continue to the next lesson.