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

The form for creating Patients is slightly more challenging. You'll want to gather the Name, PhoneNumber, and Doctor as strings (no problem) but you also need to be able to add and delete Prescriptions for this user. The form is shown in Figure 15.

Add Patient
Figure 15. Gathering strings: the Add Patient form

Your database does not have prescriptions; when you click the Add button you'll create a Prescription object and add it to the listbox (but you won't persist it until you save the person). To accomplish this, the Add button will open another form, using ShowDialog to open it modally, and then the Add button's event handler will check whether the DialogResult returned from the new form is DialogResult.OK. If so, you'll add the new prescription to the list (but not to the underlying storage).

This presents a bit of a problem. You don't want the new Prescription stored (the user may cancel the new patient) and you do want to display the name of the medication embedded in the prescription. To solve this, you'll create a nested class, PrescriptionHolder, and a type-safe list (using generics) called prescriptions, which will serve as the data source for your listbox. The PrescriptionHolder will have a public property that reaches into the Prescription, gets the Medication, and from that, gets the MedicationName property.

private class PrescriptionHolder
    private User.Prescription prescription;
    public User.Prescription ThePrescription 
        get { return prescription; } 
    public string MedicationName 
        get { return prescription.Medication.MedicationName; } 

    public PrescriptionHolder(User.Prescription prescription)
        this.prescription = prescription;

A second property makes the Prescription object itself available for retrieval. When you click Add next to the list of prescriptions, the AddPrescription window is opened, and that dialog gathers all the information needed to create a Prescription object, as shown in Figure 16.

Add Prescription
Figure 16. Add Prescription form

When you click the Save button, no event handler is invoked (the button is set to return DialogResult.OK). Instead, the prescription is created by the btnAdd_Click method back in AddPatient.cs.

private void btnAdd_Click(object sender, EventArgs e)
    AddPrescription dlg = new AddPrescription(cacheConnection);
    if (dlg.ShowDialog() == DialogResult.OK)
        User.Prescription newPrescription = 
            new User.Prescription(cacheConnection);
        newPrescription.Medication = dlg.Medication;
        newPrescription.Route = dlg.Route;
        newPrescription.Dosage = dlg.Dosage;
        newPrescription.DispensingInfo = dlg.Dispense;
        prescriptions.Add(new PrescriptionHolder(newPrescription));

private void BindPrescriptionList()
    lbPrescriptions.DataSource = null;
    lbPrescriptions.DataSource = prescriptions;
    lbPrescriptions.DisplayMember = "MedicationName";
    lbPrescriptions.ValueMember = "ID";

The only thing unusual in this form is the code to fill the two drop-down boxes:

private void FillLists()
    CacheCommand cacheCommand = User.Medication.FindAllMedications(cacheConnection);
    medicationTable = new DataTable();

    cbMedications.DataSource = medicationTable;
    cbMedications.DisplayMember = "MedicationName";
    cbMedications.ValueMember = "ID";

    cacheCommand = User.Route.FindAllRoutes(cacheConnection);
    routeTable = new DataTable();

    cbRoute.DataSource = routeTable;
    cbRoute.DisplayMember = "RouteName";
    cbRoute.ValueMember = "ID";

This code creates a standard .NET DataTable for the list of medications, and a second for the list of Routes. Each DataTable serves as the DataSource for the drop-downs, and is filled in the following steps:

  • Call the FindAll method on the object, and pass in the connection; get back a CacheCommand object
  • Initialize an empty DataTable
  • Call Load on the new DataTable, passing in the result of calling ExecuteReader on the CacheCommand object (thus filling the DataTable)

When the user clicks Save for the patient, all the information on the form is gathered into a new Patient object, which is then saved:

if ( patient == null )
    patient = new User.Patient(cacheConnection);
patient.Name = txtName.Text;
patient.PhoneNumber = txtPhoneNumber.Text;
patient.Doctor = txtDoctor.Text;
foreach (PrescriptionHolder holder in prescriptions)
CacheStatus cacheStatus = patient.Save();
if (cacheStatus.IsOK)
    patient = null;
        "Unable to save " + txtName.Text + ": " +
        "Uh oh",

Notice that the first line creates a new patient only if one does not already exist. That allows the same window to be used to modify the user.

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

Next Pagearrow