WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

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

Much More Is Possible

Of course, we've barely scratched the surface on manipulating Word documents. You can learn more about how to create and manipulate Word (and other Office products) documents from the MSDN and/or from two books on the subject that come highly recommended: Microsoft .NET Development for Microsoft Office and Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath.

Epilogue: Stocking the Database

In order to have data to work with, I needed to create Medications, Diagnoses, and Patients. I created the Medications by mixing and matching pseudo-prefixes, stubs, and suffixes to create chemical-sounding names, which were then added to the MedicationName table:

private void CreateNewMeds()
{
    string[] prefixes = { "klono", "Stero", "Albuto", "Lexo", "Neuro", 
        "Hydro", "Glyco", "Effexo", "Amino", "Ecto", "Pheno", "Tricyclo"};
    string[] pseudoChems = { "phos", "sulf", "chlor", "ster", "pyro" };
    string[] suffixes = { "ate", "ise", "ide", "ite" };
    lblMsg.Text = "Working...";
    for (int suffixCtr = 0; suffixCtr < suffixes.Length; suffixCtr++)
    {
        for (int chemCtr = 0; chemCtr < pseudoChems.Length; chemCtr++)
        {
            for (int prefixCtr = 0; prefixCtr < prefixes.Length; prefixCtr++)
            {
                string newName = prefixes[prefixCtr] + pseudoChems[chemCtr] + suffixes[suffixCtr];
                int numAdded = this.medicationNamesTableAdapter.InsertNewMedication(newName);
                if (numAdded != 1)
                {
                    MessageBox.Show("Unable to add " + newName, "Uh oh",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }

     this.medicationNamesTableAdapter.Fill(this.doctorOfficeDataSet.MedicationNames);
     this.lblMsg.Text = "Done.";
}

This results in a table with 240 pseudo-chemical names such as Tricyclophosate, as shown in Figure 12.

med names
Figure 12. Medication names

Similar code is used to create the patient names, mixing popular first and last names:

string[] firstNames = 
{
    "Abdul", "Bobby", "Charlie", "Cheryl", "David", "Diana",
    "Edgar", "Ethel", "Fran", "Gerald", "Gigi", "Hannah", "Isaih",
    "Isabelle", "Jesse", "Jody", "Ken", "Karen", "Kim", "Lori", "Marvin",
    "Melinda", "Nancy", "Oscar", "Pat", "Quentin", "Rachel", "Robert",
    "Sandy", "Stacey", "Tom", "Ursala", "Victoria", "Yolanda", "Zak"
};

string[] lastNames = 
{
    "Addison", "Baker", "Carter", "Davis", "Europa", "Finkelstein",
    "Godfrey", "Hughes", "Isaacson", "Jackson", "Marshall", "Nelson",
    "Oppenheimer", "Petri", "Rosen", "Sachs", "Tishmann"
};

lblMsg.Text = "Working...";
Application.DoEvents();


for (int firstNameCtr = 0; firstNameCtr < firstNames.Length; firstNameCtr++)
{
    for (int lastNameCtr = 0; lastNameCtr < lastNames.Length; lastNameCtr++)
    {
        string newName = firstNames[firstNameCtr] + " " + lastNames[lastNameCtr];
        int numAdded = this.patientsTableAdapter.InsertNewPatient(newName,null);
        if (numAdded != 1)
        {
            MessageBox.Show("Unable to add " + newName, "Uh oh",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

Resulting in 595 patients. While generating the patients, a random number of medications are assigned to each as well:

this.patientsTableAdapter.Fill(this.doctorOfficeDataSet.Patients);
MedicalLetters.DoctorOfficeDataSet.PatientsDataTable patients =
    this.patientsTableAdapter.GetData();
foreach (DataRow row in patients.Rows)
{
    int patientID = Convert.ToInt32(row["PatientID"]);
    const int MaxMedsPerPerson = 10;
    int maxMeds = Math.Min(medications.Rows.Count, MaxMedsPerPerson);
    int startingOffset = rand.Next(0, medications.Rows.Count - MaxMedsPerPerson);
    for (int medCtr = startingOffset; medCtr < maxMeds + startingOffset; medCtr++)
    {
        int medID = Convert.ToInt32(medications.Rows[medCtr][0]);
        int numAdded =
            this.patientToMedicationTableAdapter.InsertNewPatientMedication(
            patientID, medID, DateTime.Now);
        if (numAdded != 1)
        {
            string medicationName = medications.Rows[medCtr]["MedicationName"].ToString();
            string patientName = row["PatientName"].ToString();
            MessageBox.Show("Unable to add " + medicationName + " for " + patientName, 
                "Uh oh", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

Notice that to avoid duplication, the starting offset is set by using a random number between 0 and the total number of medications minus the maximum number of medications to be assigned to any individual patient (10).

These three database-filling routines are accessed through the Maintenance menu choices, as shown in Figure 13.

maintenance
Figure 13. Maintenance menu

To recreate this application, download the script to create the database from my website, then run each of these maintenance routines in order (medications, then diseases, then patients). Finally, click on Patient Records and choose New Visit, as shown in Figure 14.

new visit
Figure 14. Choose new visit

----

Footnote 1. Ph.G. = "Poppa Had Gelt"
Back to text

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.


Return to the Windows DevCenter.