title: Use patient ID to retrieve specific record from Access DB
body: <p>I have an Access database containing fictional patients, along with various data (such as address, consultation notes and date), and so forth.</p>
  
  <p>I have set it up so that each time a record is committed to the database, it is given a unique ID number, i.e 1001, 1002, and so on.</p>
  
  <p><strong>(For those who are unsure as to what my question is, this is it) --></strong> What I want to do is use that ID number, and load the data from a record into controls on the screen. I should mention that this is a Windows Forms Application, and that I've already setup a form to handle (i.e. pass the entered ID number as a parameter) to the appropriate method.</p>
  
  <p>The code comes in three sets.</p>
  
  <p>The first is the code that the button executes when it is clicked, (so it will pass the  string, cast as an integer into the appropriate method).</p>
  
  <p>The second set is the method which the ID is passed to -- it includes the SQL query that selects the information I need.</p>
  
  <p>The final set is the "reader", which displays the information of the record in the associated TextBoxes by using an accessor method from the appropriate class.</p>
  
  <p>The first set:</p>
  
  <pre><code>private void btnLoadPatientRecord_Click(object sender, EventArgs e)
      {
          if (String.IsNullOrEmpty(txtBoxPatientID.Text))
          {
              MessageBox.Show("You must enter a Patient ID Number to proceed.", "ATTENTION!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
          else
          {
              patientID = Convert.ToInt32(txtBoxPatientID.Text);
              dmAccessor.loadPatientRecord(patientID);
          }
  </code></pre>
  
  <p>The second set:</p>
  
  <pre><code>public void loadPatientRecord(int patientID)
        {
  
            string connString = "Provider= Microsoft.ACE.OLEDB.12.0;" + "Data Source= C:\\temp\\IntelliMed.accdb";
            string queryString = "SELECT * FROM PatientRecord WHERE ID = @patientID ";
  </code></pre>
  
  <p>The third set : (The 2nd and 3rd are actually two sets of code, one after the other, but I'm splitting them up for readability purposes).</p>
  
  <p>I should also mention that I've spent about 12 hours straight on this problem, with no result.</p>
  
  <pre><code>try
            {
                using (OleDbConnection connection = new OleDbConnection(connString))
                {
                  OleDbCommand cmnd = new OleDbCommand(queryString, connection);
                  connection.Open();
                  OleDbDataReader reader = cmnd.ExecuteReader();
                  while (reader.Read())
                  {
                      string patReaderFirstName = reader.GetString(3);
                      string patReaderLastName = reader.GetString(5);
                      string patReaderGender = reader.GetString(6);
                      string patReaderDateOfBirth = reader.GetString(7);
                      string patReaderResidentialAddress = reader.GetString(8);
                      string patReaderPostalAddress = reader.GetString(9);
                      string patReaderNHINumber = reader.GetString(10);
                      string patReaderConsultDate = reader.GetString(11);
                      string patReaderConsultNotes = reader.GetString(12);
  
                      //Data processing code below this line.
                      PatientRecord patientRecordClassOverseer = new PatientRecord();
                      patientRecordClassOverseer.setPatientFirstName(patReaderFirstName);
                      patientRecordClassOverseer.setPatientLastName(patReaderLastName);
                      patientRecordClassOverseer.setPatientGender(patReaderGender);
                      patientRecordClassOverseer.setDateOfBirth(patReaderDateOfBirth);
                      patientRecordClassOverseer.setPatientResidentialAddress(patReaderResidentialAddress);
                      patientRecordClassOverseer.setPostalAddress(patReaderPostalAddress);
                      patientRecordClassOverseer.setPatientNHINumber(patReaderNHINumber);
                      patientRecordClassOverseer.setPatientConsultationDate(patReaderConsultDate);
                      patientRecordClassOverseer.setPatientConsultationNotes(patReaderConsultNotes);
                  }
                  reader.Close();
  
                  }
                ctnIntelliMed.Close();
  
            }
  
            catch (Exception exf)
            {
                MessageBox.Show("Sorry, an error has occurred while attempting to load the selected record from the database. Please try this again.");
            }
        }
  </code></pre>
  
  <p>Help is gratefully received, and many thanks in advance.</p>