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 4: Add startup code to your application Next Page

Lesson 5: Add inserts to your application


This lesson shows how to fill out your application with data manipulation and navigation logic.

Open the table
  1. Write code to initialize the CustomerTable that represents the Customer table in your database.

    Add the following code to the end of the Connect function in tutorial.js:

    try {
      CustomerTable = Connection.getTable( "customer", null );
      CustomerTable.open();
    } catch( ex3 ) {
      alert("Error: " + ex3.getMessage() );
    }
  2. Add variables to move data between the database and the web form.

    Add the following declarations to the top of tutorial.js, before the Connect function.

    var Cust_FName = "";
    var Cust_LName = "";
    var Cust_City = "";
    var Cust_Phone = "";
    var Cust_Id = "";
  3. Create procedures to fetch and display customer data.

    Add the following function to tutorial.js, immediately after the Connect function. It fetches the current row of the customer and also ensures that NULL columns display as empty strings:

    function FetchCurrentRow()
    {
      var id;
      if( CustomerTable.getRowCount() == 0 ) {
        Cust_FName = "";
        Cust_LName = "";
        Cust_City = "";
        Cust_Phone = "";
        Cust_Id = "";
      } else {
        id = CustomerTable.schema.getColumnID("FName");
        Cust_FName = CustomerTable.getString(id);
        id = CustomerTable.schema.getColumnID("LName");
        Cust_LName = CustomerTable.getString(id);
        id = CustomerTable.schema.getColumnID("city");
        if( CustomerTable.isNull(id) ) {
          Cust_City = "";
        } else {
          Cust_City = CustomerTable.getString(id);
        }
        id = CustomerTable.schema.getColumnID("phone");
        if( CustomerTable.isNull(id) ) {
          Cust_Phone = "";
        } else {
          Cust_Phone = CustomerTable.getString(id);
        }
    
        id = CustomerTable.schema.getColumnID("id");
        Cust_Id = CustomerTable.getString(id);
      }
    }

    Add the following JavaScript to main.html, immediately before the closing </body> tag. DisplayCurrentRow takes the values from the database and displays them in the web form. FetchForm takes the current values in the web form and makes them available to the database code.

    <script>
    function DisplayCurrentRow()
    {
      FetchCurrentRow();
      document.custForm.fname.value = Cust_FName;
      document.custForm.lname.value = Cust_LName;
      document.custForm.city.value = Cust_City;
      document.custForm.phone.value = Cust_Phone;
      document.custForm.custid.value = Cust_Id;
    }
    function FetchForm()
    {
      Cust_FName = document.custForm.fname.value;
      Cust_LName = document.custForm.lname.value;
      Cust_City = document.custForm.city.value;
      Cust_Phone = document.custForm.phone.value;
    }
    </script>
  4. Call DisplayCurrentRow when the application is loaded.

    Modify the body tag at the top of main.html as follows:

    <body onload="Connect(); DisplayCurrentRow();">

Although there is no data in your database and no rows are displayed, this is a good place to synchronize M-Business Client to ensure that you have not introduced bugs.

Add code to insert rows