473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested Looping with DataReader

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>"));

}

Sep 22 '06 #1
4 2413
I suggest you use a DataTable instead of a DataReader.

--
I hope this helps,
Steve C. Orr
MCSD, MVP, CSM
http://SteveOrr.net
<de*******@yahoo.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
>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>"));

}

Sep 22 '06 #2
The overhead won't be too much for what I'm trying to do?

Steve C. Orr [MVP, MCSD] wrote:
I suggest you use a DataTable instead of a DataReader.

--
I hope this helps,
Steve C. Orr
MCSD, MVP, CSM
http://SteveOrr.net
<de*******@yahoo.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
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>"));

}
Sep 22 '06 #3
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>"));

Sep 22 '06 #4
I see, this way the checkbox control isn't accesed out of context... a
very nice change. Thank you, I will try it.
Gozirra wrote:
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>"));
Sep 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
7
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
2
by: tony collier | last post by:
Hi i want to get a user to input x which then creates an nested loop with x levels. each level of the loop has the same number of iterations e.g. for the case where x=3, the following code is...
0
by: Andrew Warren | last post by:
I have a need for a non-recursive technique to determine whether a user is a member of a specified group or any of its nested sub-groups I have read some of the posts regarding enumerating members...
2
by: Colin Nicholls | last post by:
Platform: ASP.NET 1.1 I have a repeater nested inside another repeater. My outer repeater is looping fine. I am manually binding the inner repeater to a DataReader obtained from another...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
3
by: =?Utf-8?B?R3JlZyBTdGV2ZW5z?= | last post by:
I am connecting to an Oracle database using an OleDbConnection. I am using DataReader objects to get query results. However, this limits me to only having one reader open at a time, which is a...
11
by: boker | last post by:
i try to do nested looping like this for (i=0;i<=640;i++) { for(j=0;j<=640;j++) { if(getpixel(j,i)!=0) { printf("this a color pixel); } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.