Contents Index Lesson 3: Connect to the database Lesson 5: Build and deploy your application

UltraLite.NET User's Guide
  Tutorial: Visual Studio Application

Lesson 4: Insert, update, and delete data


This lesson shows you how to modify your database. It uses dynamic SQL to modify the data. You can also use a table-based API.

For more information, see Data access in UltraLite.

The first step in this section is to create a supporting method to maintain the list box. Once that is complete, you can add code to modify data.

Add code to maintain the listbox

  1. Right click on the form and select View Code. Add the following method to update and populate the listbox; it will be used by other methods in your form.

    The following code is for a C# application:

    private void RefreshLB()
    {
       try 
       {
          ResultSet DisplayAll;
          long NumRows;
          ListBoxNames.Items.Clear();
          PrepStmt = Conn.PrepareStatement( 
              "SELECT id, name FROM Names");
          DisplayAll = PrepStmt.ExecuteQuery();
          DisplayAll.MoveBeforeFirst();
          NumRows = DisplayAll.RowCount;
          ids = new int[ NumRows ];
          while (DisplayAll.MoveNext())
          {   
            lbNames.Items.Add(
            new LBItem(DisplayAll.GetString(2), 
                       DisplayAll.GetInt(1))
            );
           ids[ DisplayAll.GetInt(1) - 1 ] = 
                DisplayAll.GetInt(1);
          }
          DisplayAll.Close();
          PrepStmt.Close();
             }
             catch( SQLException err )
             {
          MessageBox.Show( 
             "Exception: " + err.Message +
             " sqlcode=" + err.ErrorCode);
             }
             catch ( System.Exception t )
             {
          MessageBox.Show( 
             "Exception: " + t.Message );
             }
          }

    For a Visual Basic application, use this code:

    Private Sub RefreshLB()
       Try
          Dim DisplayAll As iAnywhere.UltraLite.ResultSet
          Dim NumRows As Long
    
          lbNames.Items.Clear()
          PrepStmt = Conn.PrepareStatement( _
              "SELECT id, name FROM Names")
          DisplayAll = PrepStmt.ExecuteQuery
          DisplayAll.MoveBeforeFirst()
          NumRows = DisplayAll.RowCount
    
          While (DisplayAll.MoveNext)
             ListBoxNames.Items.Add( _
                 New LBItem(DisplayAll.GetString(2), _
                 DisplayAll.GetInt(1)) )
                 Ids[ DisplayAll.GetInt(1) - 1 ] = 
                DisplayAll.GetInt(1)
          End While
    
          DisplayAll.Close()
          PrepStmt.Close()
       Catch
          MessageBox.Show(
            "Exception: " + Err.Description 
          )
       End Try
    End Sub

    This code carries out the following tasks:

  2. Add the following method to create a new listbox item class to store the name and id.

    The following code is for a C# application:

    public class LBItem
    {
       public String ItemName;
       public int ItemID;
    
       public LBItem(String name, int id)
       {
          ItemName = name;
          ItemID = id;
       }
    
       public override string ToString()
       {
          return ItemName;
       }
    }

    For a Visual Basic application, add the following code after your Form1 class:

    Public Class LBItem
       Public ItemName As String
       Public Itemid As Integer
    
       Public Sub New(ByVal name As String, ByVal id As Integer)
          ItemName = name
          Itemid = id
       End Sub
    
       Public Overrides Function ToString() As String
          ToString = ItemName
       End Function
    End Class
  3. Add the following method to the Form1 class to get the ID of a selected listbox item.

    The following code is for a C# application:

    private int GetSelectedID()
    {
       int curSel;
       LBItem item;
       curSel = lbNames.SelectedIndex;
       if (curSel < 0)
       {
          return -1;
       }
       else
       {
          item = (LBItem)lbNames.Items[ curSel ];
          return item.ItemID;
       }
    }

    For a Visual Basic application, add the following code just before the end of your Form1 class:

    Private Function GetSelectedid() As Integer
       Dim curSel As Integer
       Dim item As LBItem
    
       curSel = lbNames.SelectedIndex
       If curSel < 0 Then
          GetSelectedid = -1
          Exit Function
       End If
    
       item = lbNames.Items(curSel)
       GetSelectedid = item.Itemid
    End Function

    This code carries out the following tasks:

  4. Build the project.

    You should be able to build the solution with no errors.

Add data modification methods

  1. Double click on the Insert button to create a btnInsert_Click method. If developing a C# application, add the following code to the method:

    try
    {
       long RowsInserted;
    
       PrepStmt = Conn.PrepareStatement(
          "INSERT INTO Names(name) VALUES (?)");
       PrepStmt.SetStringParameter(1, txtName.Text);
       RowsInserted = PrepStmt.ExecuteStatement();
       PrepStmt.Close();
       RefreshLB();
    }
    catch( SQLException err )
    {
       MessageBox.Show("Exception: " + err.Message +
          " sqlcode=" + err.ErrorCode);
    }
    catch ( System.Exception t )
    {
       MessageBox.Show( "Exception: " + t.Message);
    }

    For a Visual Basic application, use the following code:

    Try
       Dim RowsInserted As Long
    
       PrepStmt = Conn.PrepareStatement( _
           "INSERT INTO Names(name) VALUES (?)")
       PrepStmt.SetStringParameter(1, txtName.Text)
       RowsInserted = PrepStmt.ExecuteStatement()
       PrepStmt.Close()
       RefreshLB()
       Catch
          MessageBox.Show("Exception: " + Err.Description)
       End Try

    This code carries out the following tasks:

  2. Double click on the Update button to create a btnUpdate_Click method. If developing a C# application, add the following code to the method:

    try
    {
       long RowsUpdated;
       int updtid = ids[ lbNames.SelectedIndex ];
    
       PrepStmt = Conn.PrepareStatement( 
          "UPDATE Names SET name = ? WHERE id = ?" );
       PrepStmt.SetStringParameter(1, txtName.Text);
       PrepStmt.SetIntParameter(2, updtid);
       RowsUpdated = PrepStmt.ExecuteStatement();
       PrepStmt.Close();
       RefreshLB();
    }
    catch( SQLException err )
    {   
       MessageBox.Show( 
          "Exception: " + err.Message +
          " sqlcode=" + err.ErrorCode);
    }
    catch ( System.Exception t )
    {
       MessageBox.Show( "Exception: " + t.Message);
    }

    For a Visual Basic application, use the following code:

    Try
       Dim RowsUpdated As Long
       Dim updtid As Integer = ids(lbNames.SelectedIndex)
    
       PrepStmt = Conn.PrepareStatement( _
          "UPDATE Names SET name = ? WHERE id = ?")
       PrepStmt.SetStringParameter(1, txtName.Text)
       PrepStmt.SetIntParameter(2, updtid)
       RowsUpdated = PrepStmt.ExecuteStatement()
       PrepStmt.Close()
       RefreshLB()
    Catch
       MessageBox.Show("Exception: " + Err.Description)
    End Try

    This code carries out the following tasks:

  3. Double click on the Delete button to create a btnDelete_Click method. If developing a C# application, add the following code to the method:

    try
    {
       int delid = ids[lbNames.SelectedIndex];
       long RowsDeleted;
    
       PrepStmt = Conn.PrepareStatement(
          "DELETE From Names WHERE id = ?" );
       PrepStmt.SetIntParameter(1, delid);
       RowsDeleted = PrepStmt.ExecuteStatement();
       PrepStmt.Close();
       RefreshLB();
    }
    catch( SQLException err )
    {   
       MessageBox.Show("Exception: " + err.Message +
          " sqlcode=" + err.ErrorCode);
    }
    catch ( System.Exception t )
    {
       MessageBox.Show( "Exception: " + t.Message);
    }

    For a Visual Basic application, use the following code:

    Try
       Dim delid As Integer = ids(lbNames.SelectedIndex)
       Dim RowsDeleted As Long
    
       PrepStmt = Conn.PrepareStatement( _
          "DELETE From Names WHERE id = ?")
       PrepStmt.SetIntParameter(1, delid)
       RowsDeleted = PrepStmt.ExecuteStatement()
       PrepStmt.Close()
       RefreshLB()
    Catch
       MessageBox.Show("Exception: " + err.description)
    End Try

    This code carries out the following tasks:


Contents Index Lesson 3: Connect to the database Lesson 5: Build and deploy your application