Nested Looping with DataReader

D

dejavue82

I am trying to use a DataReader to read a table (that was attained from
JOINing two other tables) with the following format:

questions answers

1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4

Unfortunately, I need to check both in the outer and inner loop to see
if reader.Read() == null.
At the same time, this statement not only checks to see if the reader
can go to the next row, but actually proceeds to the next row. It ends
up skipping the last answer of every question > 2. Any ideas? Thank you
for your time!

int quest_no = 0;
while (reader.Read())
{
quest_no++;

Label quest_count = new Label();
quest_count.Text = "Question " + quest_no.ToString() + ".
";

Label quest_lb = new Label();
quest_lb.Text = reader["quest"].ToString();

LabelPlaceHolder.Controls.Add(quest_count);
LabelPlaceHolder.Controls.Add(quest_lb);

CheckBoxList rad1 = new CheckBoxList();
while (((int)reader["quest_id"] == quest_no))
{
rad1.ID = quest_no.ToString();
rad1.Items.Add(reader["answ"].ToString());
LabelPlaceHolder.Controls.Add(rad1);

ArrayList possbAns = new ArrayList();
possbAns.Add(reader["is_true"]);
ViewState.Add(quest_no.ToString(), possbAns);


if (!reader.Read()) break;
}


}
LabelPlaceHolder.Controls.Add(new LiteralControl("<br>"));

}
 
D

dejavue82

The overhead won't be too much for what I'm trying to do?


I suggest you use a DataTable instead of a DataReader.

--
I hope this helps,
Steve C. Orr
MCSD, MVP, CSM
http://SteveOrr.net


I am trying to use a DataReader to read a table (that was attained from
JOINing two other tables) with the following format:

questions answers

1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4

Unfortunately, I need to check both in the outer and inner loop to see
if reader.Read() == null.
At the same time, this statement not only checks to see if the reader
can go to the next row, but actually proceeds to the next row. It ends
up skipping the last answer of every question > 2. Any ideas? Thank you
for your time!

int quest_no = 0;
while (reader.Read())
{
quest_no++;

Label quest_count = new Label();
quest_count.Text = "Question " + quest_no.ToString() + ".
";

Label quest_lb = new Label();
quest_lb.Text = reader["quest"].ToString();

LabelPlaceHolder.Controls.Add(quest_count);
LabelPlaceHolder.Controls.Add(quest_lb);

CheckBoxList rad1 = new CheckBoxList();
while (((int)reader["quest_id"] == quest_no))
{
rad1.ID = quest_no.ToString();
rad1.Items.Add(reader["answ"].ToString());
LabelPlaceHolder.Controls.Add(rad1);

ArrayList possbAns = new ArrayList();
possbAns.Add(reader["is_true"]);
ViewState.Add(quest_no.ToString(), possbAns);


if (!reader.Read()) break;
}


}
LabelPlaceHolder.Controls.Add(new LiteralControl("<br>"));

}
 
G

Gozirra

How about doing this instead. There is only a single point when the
next record is accessed. The ID of the record is used to determine
when you have moved to the next question. rad1 is moved outside of the
reader and is instantiated as a new object only when the quest_id is
changed. I think this will work but I haven't tested or compiled. If
nothing else, I think it will get you moving in the right direction.

Or you can use a datatable if you want. The overhead probably won't
hurt you but it depends on how much data you expect to retrieve.

CheckBoxList rad1;
int quest_no = 0;
int quest_id = -1;
while (reader.Read())
{
// Trigger new question stuff using the quest_id of the
reader
if ( quest_id != (int)reader["quest_id"] )
{
quest_no++;
rad1 = new CheckBoxList(); // get a new checkboxlist
Label quest_count = new Label();
quest_count.Text = "Question " + quest_no.ToString() +
".";

Label quest_lb = new Label();
quest_lb.Text = reader["quest"].ToString();


LabelPlaceHolder.Controls.Add(quest_count);
LabelPlaceHolder.Controls.Add(quest_lb);
quest_id = (int)reader["quest_id"];
}

// do this with every record
rad1.ID = quest_no.ToString();
rad1.Items.Add(reader["answ"].ToString());
LabelPlaceHolder.Controls.Add(rad1);


ArrayList possbAns = new ArrayList();
possbAns.Add(reader["is_true"]);
ViewState.Add(quest_no.ToString(), possbAns);
}
LabelPlaceHolder.Controls.Add(new LiteralControl("<br>"));
 
D

dejavue82

I see, this way the checkbox control isn't accesed out of context... a
very nice change. Thank you, I will try it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top