tags:

views:

30

answers:

2

I have an sql statement that is supposed to return 2 rows. the first with psychological_id = 1, and the second, psychological_id = 2. here is the sql statement

select * from psychological where patient_id = 12 and symptom = 'delire';

But with this code, with which I populate an array list with what is supposed to be 2 different rows, two rows exist, but with the same values: the second row.

OneSymptomClass oneSymp = new OneSymptomClass();
ArrayList oneSympAll = new ArrayList();

string connStrArrayList = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
    "Initial Catalog=PatientMonitoringDatabase; " +
    "Integrated Security=True";

string queryStrArrayList = "select * from psychological where patient_id = " + patientID.patient_id + " and symptom = '" + SymptomComboBoxes[tag].SelectedItem + "';";

using (var conn = new SqlConnection(connStrArrayList))
using (var cmd = new SqlCommand(queryStrArrayList, conn))
{
    conn.Open();

    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            oneSymp.psychological_id = Convert.ToInt32(rdr["psychological_id"]);
            oneSymp.patient_history_date_psy = (DateTime)rdr["patient_history_date_psy"];
            oneSymp.strength = Convert.ToInt32(rdr["strength"]);
            oneSymp.psy_start_date = (DateTime)rdr["psy_start_date"];
            oneSymp.psy_end_date = (DateTime)rdr["psy_end_date"];

            oneSympAll.Add(oneSymp);
        }
    }

    conn.Close();
}

OneSymptomClass testSymp = oneSympAll[0] as OneSymptomClass;
MessageBox.Show(testSymp.psychological_id.ToString());

the message box outputs "2", while it's supposed to output "1". anyone got an idea what's going on?

A: 

You're adding the same instance to the ArrayList twice. Try this:

List<OneSymptomClass> oneSympAll = new List<OneSymptomClass>();

string connStrArrayList = 
    "Data Source=.\\SQLEXPRESS;" +
    "AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
    "Initial Catalog=PatientMonitoringDatabase; " +
    "Integrated Security=True";

Patient patientID;
string queryStrArrayList =
    "select * from psychological where patient_id = " +
    patientID.patient_id + " and symptom = '" +
    SymptomComboBoxes[tag].SelectedItem + "';";

using (var conn = new SqlConnection(connStrArrayList))
{
    using (var cmd = new SqlCommand(queryStrArrayList, conn))
    {
        conn.Open();

        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                OneSymptomClass oneSymp = new OneSymptomClass();
                oneSymp.psychological_id =
                    Convert.ToInt32(rdr["psychological_id"]);
                oneSymp.patient_history_date_psy =
                    (DateTime) rdr["patient_history_date_psy"];
                oneSymp.strength = Convert.ToInt32(rdr["strength"]);
                oneSymp.psy_start_date =
                    (DateTime) rdr["psy_start_date"];
                oneSymp.psy_end_date =
                    (DateTime) rdr["psy_end_date"];

                oneSympAll.Add(oneSymp);
            }
        }

        conn.Close();
    }
}

MessageBox.Show(oneSympAll[0].psychological_id.ToString());
MessageBox.Show(oneSympAll[1].psychological_id.ToString());

Note that I replaced the ArrayList with a List<OneSymptomClass>. There is no reason to use ArrayList unless you're using .NET 1.1.

John Saunders
but isn't there supposed to be 2 different oneSymps in the oneSympAll arraylist? I mean, the first time, one version of oneSymp is added to the arraylist, and the second time, a different version. shouldn't they be different?
jello
@jello: no, you added the same reference twice.
John Saunders
A: 

thx for the tip John Saunders. I added a line that makes it work. was that what you were gonna suggest me?

                while (rdr.Read())
                {
                    oneSymp = new OneSymptomClass();

                    oneSymp.psychological_id = Convert.ToInt32(rdr["psychological_id"]);
                    oneSymp.patient_history_date_psy = (DateTime)rdr["patient_history_date_psy"];
                    oneSymp.strength = Convert.ToInt32(rdr["strength"]);
                    oneSymp.psy_start_date = (DateTime)rdr["psy_start_date"];
                    oneSymp.psy_end_date = (DateTime)rdr["psy_end_date"];

                    oneSympAll.Add(oneSymp);
                }
jello