WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Programming Word from .NET
Pages: 1, 2, 3, 4, 5, 6, 7

The Save Button Handler Unpacked

There is a lot going on here, let's take it apart step by step.



First, because this operation will take a few moments, the cursor is set to the wait cursor. Next, the PatientID and PatientName are retrieved from the combo box, based on the user's selection.

This of course raises the question of how these combo boxes were initially populated. After creating my database tables in SQL Server, I returned to Visual Studio and clicked on Data->Add New Data Source, and used the wizard to create a DataSet that contained all the tables from my database.

To create the drop-down of patients, I right-clicked on the Patients table and set its representation to ComboBox (as shown in Figure 8) and then dragged it onto the form.

Setting Combo Box
Figure 8. Setting the combo box

When I dragged this table onto the form, a combo box was created, and, in the tray below, a PatientsBindingSource object and a PatientsTableAdapter. Clicking on the smart tag associated with the combo box let me set both the display value and the underlying value (the PatientID) that was to be held by the combo box, as shown in Figure 9.

drop down properties
Figure 9. Drop-down properties

Returning to the discussion of what happens when the Save button is clicked, it is a simple matter to query the control for the ID and name of the chosen patient (and similarly for the diagnosis).

int patientID = Convert.ToInt32(patientsComboBox.SelectedValue);
string patientName = 
    ((System.Data.DataRowView)(patientsComboBox.SelectedItem)).Row.ItemArray[1].ToString();
int diagnosisID = Convert.ToInt32(diagnosesComboBox.SelectedValue);
string diagnosisString = 
    ((System.Data.DataRowView)(diagnosesComboBox.SelectedItem)).Row.ItemArray[1].ToString()

When the dialog opened, we had set the time of the visit so that all the associated records (such as the medication) will have the identical time:

 private DateTime visitTime = DateTime.Now;

You now use that private member variable and the data from the combo boxes to update the Visits table through the provided adapter:

this.visitsTableAdapter.InsertVisit(visitTime, txtNotes.Text, patientID);

Similarly, now that we know that the records are to be saved, we can add a record to the PatientToDiagnosis table:

this.patientToDiagnosesTableAdapter.InsertPatientToDiagnosis(patientID, diagnosisID);

One problem we faced when the user was adding medications to the Visit was what to do with the Medication choices. We wanted to display the new medications in the listbox, but we did not want to add them to the database until and unless the record is saved. To manage this, we created a nested class to hold the information about the selected medications.

 public partial class Visit : Form
 {
   public class MedicationHolder
   {
     private int medID;
     public int MedID
     {
        get { return medID; }
        set { medID = value; }
     }
     private string medName;
     public string MedName
     {
        get { return medName; }
        set { medName = value; }
     }
     public MedicationHolder(int medID, string medName)
     {
        this.medID = medID;
        this.medName = medName;
     }
   }   // end nested class
}      // end outer class

The user clicks on the Add Rx button to open the Add Prescription dialog. If the user saves the prescription it is not written to the database, but instead it is wrapped in a holder and placed in a list which serves as the data source for the listbox.

private void addRxBtn_Click(object sender, EventArgs e)
{
    AddRx dlg = new AddRx();

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        
        int medicationID = dlg.ChosenMedID;
        string medicationName = dlg.ChosenMed;
        newMeds.Add(new MedicationHolder(medicationID, medicationName));
        this.lbMeds.DataSource = null;
        this.lbMeds.DataSource = newMeds;
        lbMeds.DisplayMember = "MedName";
        lbMeds.ValueMember = "MedID";
    }
    
}

Again, returning to the Save button event handler, we have now only to iterate through these medication holders and add each to the database:

string newMeds = string.Empty;
foreach (MedicationHolder holder in lbMeds.Items)
{
    newMeds += holder.MedName + ", ";
    int numAdded = this.patientToMedicationTableAdapter.InsertNewPatientMedication(
        patientID, holder.MedID, visitTime);
    if (numAdded != 1)
    {
        MessageBox.Show("Unable to add " + holder.MedName, "Uh oh",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Along the way we build up a string of the new medication names (newMeds), which we will use in the letter to the patient.

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

Next Pagearrow