WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

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

We'll now go back and create a crude dialog to let the user pick an existing user to edit, or create a new user by pressing the New button, as shown in Figure 17.

Pick Patient
Figure 17. PickPatient dialog

Remove CreatePatient from the first menu item, and create a second menu item, Patient. The event handler for this new menu item will open the dialog shown in Figure 17.

Clicking on one of the existing patients invokes AddPatient with that patient's ID, and closes the current window. The constructor for the PickPatient dialog fills the list of patients and sets an event handler for when the user clicks on a patient (we don't do this in the Properties window because we don't want this method inovked until the list is fully populated).

public PickPatient(CacheConnection cacheConnection)
{
    InitializeComponent();
    this.cacheConnection = cacheConnection;
    FillPatientList();
    lbPatients.SelectedIndexChanged +=
        new EventHandler(lbPatients_SelectedIndexChanged);
}

Filling the list requires a call to the FindAllPatients method that started out as a query in the Patients class in Caché Studio.

Notice that you don't wire up the SelectedChanged event until all the patients are added, otherwise each time you added a patient that event would fire.

private void FillPatientList()
{
    CacheCommand command = 
        User.Patient.FindAllPatients(cacheConnection);

    patientTable = new DataTable();
    patientTable.Load(command.ExecuteReader());

    lbPatients.DataSource = patientTable;
    lbPatients.DisplayMember = "Name";
    lbPatients.ValueMember = "ID";
    lbPatients.SelectedIndex = -1;
}

Invoking the AddPatient page when the user does choose a patient is as easy as picking up the value chosen, opening the new window, and closing the picker window:

private void lbPatients_SelectedIndexChanged(object sender, EventArgs e)
{
    AddPatient newWindow = 
        new AddPatient(cacheConnection, 
            lbPatients.SelectedValue.ToString());
    newWindow.Show();
    this.Close();
}

Finally, creating a new user is exactly the same code except that you call the constructor for AddPatient that takes a single parameter, the CacheConnection, and you do not pass in any value for the user ID (as you have none).

"What Have You Learned, Dorothy?"

As a C# programmer, I'm able to think about the objects (Patient, Medication, etc.) as C# objects, without ever worrying about their original nature as Caché objects, let alone their underlying true identity in the Caché Globals. This releases me from thinking about ADO.NET, DataSets, Rows, Tables and so forth. I think about creating objects, saving objects, and updating objects, and I let Caché, which is built on 40-year-old database technology, handle the details! Pretty cool.

The complete C# source code is available for download from my website.

The Data Does Exist

I'm quite happy to let Caché manage my data, and never worry about how it does so. That said, Caché Terminal allows me to inspect the data, if I'm so motivated, as shown in Figures 18 and 19.

Medication in Cache
Figure 18. Inspecting medication data with Caché Terminal

Patient In Cache
Figure 19. Inspecting patient data with Caché Terminal

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

Next Pagearrow