oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Object-Oriented Data Programming: C# Meets Caché
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9

Return to the CacheDemo page and double-click on the Create Routes menu entry to create the event handler (createRoutesToolStripMenuItem_Click) and add the following code:

AddRoutes dlg = new AddRoutes(CacheConnection);

Notice that you are passing in the CacheConnection object to the constructor. Open the code for the AddRoutes form and modify the constructor as follows:

public partial class AddRoutes : Form
    private CacheConnection CacheConnection;
    private DataTable tblRoutes;

    public AddRoutes(CacheConnection CacheConnection)
        this.CacheConnection = CacheConnection;

Visual Studio will offer to create a stub for FillRoutes, which you will fill with the following code:

private void FillRoutes()
    CacheCommand CacheCommand = User.Route.FindAllRoutes(CacheConnection);
    tblRoutes = new DataTable();

    lbRoutes.DataSource = tblRoutes;
    lbRoutes.DisplayMember = "RouteName";
    lbRoutes.ValueMember = "ID";

Notice that you create a CacheCommand object (which Intellisense will recognize, along with all the other Caché objects, thanks both to your using statement and to the reference you added earlier). This CacheCommand object is filled with the return value you receive when you call the member method FindAllRoutes. This method was created from the query you added to the class.

At this point a terrible chill passes over you. You forgot to add queries to the other classes, didn't you? No problem. Go back to Caché Studio and add queries to each class, then rebuild the project, delete the CacheClasses.cs file from your Visual Studio project, regenerate the proxy and add it back to the Visual Studio project, and you're all set!

Your next step (continuing with the AddRoutes dialog), is to implement event handlers for the Add button. The goal of this event handler is to create a new Route object, whose RouteName property will be taken from the text box, and to persist it.

You will set each property of the Route object (in this case there is only one). Intellisense will help, but if you want to remind yourself of what properties an object has, you need only return to Caché Studio, click on the class, and examine the names of the properties.

Finally, you'll call your C# method FillRoutes to refill the listbox with the new Route object added.

private void btnAdd_Click(object sender, EventArgs e)
    if (txtRoute.Text.Length < 1)

    User.Route newRoute = new User.Route(CacheConnection);
    newRoute.RouteName = txtRoute.Text;
    CacheStatus CacheStatus = newRoute.Save();
    if (CacheStatus.IsOK == true)
        txtRoute.Text = string.Empty;
        MessageBox.Show("Unable to add " + txtRoute.Text +
            CacheStatus.Message, "Uh oh", MessageBoxButtons.OK,

The key part of this code is in the creation of the new User.Route object and then calling Save() on that object to cause it to persist (to write it back to Caché's global).

To implement the Remove button we'll ask the selected item for its ID, and then call DeleteID on the Route class passing in the connection and the ID; the object will be removed from storage. (You might want to add a dialog box to make sure the user really wants to do this.)

private void btnRemove_Click(object sender, EventArgs e)
    if (lbRoutes.SelectedIndex < 0)
    string zombieID = lbRoutes.SelectedValue.ToString();

    // TODO: Add message box here to make sure they really
    // do want to delete this route
    User.Route.DeleteId(CacheConnection, zombieID);

Note that no code is needed for the OK button; just set its DialogResult property to OK and it will automatically close the form when clicked.

The result is shown in Figure 14.

Add Route
Figure 14. Using the Add Routes form

You are now ready to create a nearly identical form for creating Medications, using very much the same logic. (It is left as an exercise for the industrious reader both to implement that form, and then to consider whether it makes sense to combine these two forms into a single parameterized form.)

Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9

Next Pagearrow