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

SAP Sybase SQL Anywhere 16.0 » UltraLite - .NET Programming » Tutorial: Building a Windows Mobile application using UltraLite.NET

 

Lesson 3: Adding database connection controls to the application

In this lesson, you add a control to your UltraLite.NET application that establishes a connection to an UltraLite database.

Prerequisites

This lesson assumes you have completed all preceding lessons. See Lesson 1: Creating a Visual Studio project.

 Task
  1. Double-click the form to open the source file (Form1.cs or Form1.vb).

  2. Add code to import the iAnywhere.Data.UltraLite namespace.

    Add the following statement as the very first line of the file.

    //Visual C#
    using iAnywhere.Data.UltraLite;
    'Visual Basic
    Imports iAnywhere.Data.UltraLite
  3. Add global variables to the form declaration.

    For Visual C#, add the following code after the code describing the form components and before the first method declaration.

    //Visual C#
    private ULConnection Conn;
    private int[] ids;

    For a Visual Basic, add the following code at the beginning of the Form1 class.

    'Visual Basic
    Dim Conn As ULConnection
    Dim ids() As Integer

    These variables are used as follows:

    • ULConnection   A Connection object is the root object for all actions executed on a connection to a database.

    • ids   The ids array is used to hold the ID column values returned after executing a query.

      Although the ListBox control itself allows you access to sequential numbers, those numbers differ from the value of the ID column once a row has been deleted. For this reason, the ID column values must be stored separately.

  4. Double-click a blank area of your form to create a Form1_Load method.

    This method performs the following tasks:

    • Open a connection to the database using the connection parameters set in the ulConnectionParms1 control.

    • Call the RefreshListBox method (defined later in this tutorial).

    • Print (display) and error message if an error occurs. For SQL Anywhere errors, the code also prints the error code. See Error Messages.

    For C#, add the following code to the Form1_Load method.



    //Visual C#
    try {
        String ConnString = "dbf=\\Program Files\\CSApp\\CSApp.udb";
        Conn = new ULConnection( ConnString );
        Conn.Open();
        Conn.DatabaseID = 1;
        RefreshListBox();
    }
    catch ( System.Exception t ) {
        MessageBox.Show( "Exception: " + t.Message);
    }

    For Visual Basic, add the following code to the Form1_Load method.



    'Visual Basic
    Try
        Dim ConnString as String = "dbf=\Program Files\VBApp\VBApp.udb"
        Conn = New ULConnection( ConnString )
        Conn.Open()
        Conn.DatabaseID = 1
        RefreshListBox()
    Catch
        MsgBox("Exception: " + err.Description)
    End Try
  5. Build the project.

    From the Build menu, click Build Solution. At this stage, you may receive a single error reported; for example in C#: error CS0103: The name 'RefreshListBox' does not exist in the current context. because RefreshListBox is not yet declared. The next lesson adds that function.

    If you get other errors, you must correct them before proceeding. Check for common errors, such as case inconsistencies in C#. For example, UltraLite and ULConnection must match case exactly. In Visual Basic it is crucial to include the Imports iAnywhere.Data.UltraLite statement as described in Lesson 3.

Results

The application is set up to connect to an UltraLite database.