Parsing a DataReader

Z

Zac Maclean

I need to loop through a DataReader.


The data it contains is in the following format (presented here as CSV, To
and From fields are dates):




Widget 1, Property 1, To, From
Widget 1, Property 2, To, From
Widget 1, Property 3, To, From
Widget 1, Property 4, To, From
Widget 1, Property 5, To, From
Widget 1, Property 6, To, From
Widget 1, Property 7, To, From
Widget 1, Property 8, To, From
Widget 1, Property 9, To, From
Widget 1, Property 10, To, From
Widget 2, Property 1, To, From
Widget 2, Property 2, To, From
Widget 2, Property 3, To, From
Widget 2, Property 4, To, From
Widget 2, Property 5, To, From
Widget 2, Property 6, To, From
Widget 2, Property 7, To, From
Widget 2, Property 8, To, From
Widget 2, Property 9, To, From
Widget 2, Property 10, To, From


I am building another table to be displayed as follows:

Widget_____|| Prop 1___ || Prop 2__ || Prop 3___ || Prop 4__ ||
-----------------------------------------------------------------------
Widget 1____|| To | From || To | From || To | From || To | From ||
-----------------------------------------------------------------------
Widget 2____|| To | From || To | From || To | From || To | From ||

/end feeble ASCII table rendition

Now.. I've gotten this work in flat ASP using a RecordSet, building a table
like above, but only with the "To" information, in ASP I am only able to
loop properly a single time without repeating the code, and changing almost
every line. I need to read this data 2 times, to get a To and From and
count. I placed the code to parse the data in a class, sending the needed
info over to get this going.. but I am missing something in the switch from
ASP to ASP.NET.


------- code ------
while (dr.Read())
{
strWidgetName = (string)dr.GetSqlString(0);
strWidgetNameOld = strWidgetName;
strWidgets[intCount] = strWidgetName;

// Fill Widget Name array
while (strWidgetName == strWidgetNameOld) // && (dr.Read()))
{
#region "Report logic is here and working. My problem is with the
looping."

boolDebug = dr.Read();
if (dr.Read() == false)
{
break;
}
else
{
clsReport.drCat.Read();
strWidgetNameOld = strWidgetName;
strWidgetName = (string)dr.GetSqlString(1);
}

}
intCount = ++intCount;
}

-------------- end code ------------------------


I've moved the pieces of the last Else statement around, seeing if that
would fix it. What I am seeing now it loops either too many times, or it
gets stuck on one Widget, and never progresses to the next.

If needed, I have working ASP (vbscript) code that has similar goal.

Zac Maclean
 
R

Ryan Trudelle-Schwarz

I need to loop through a DataReader.
The data it contains is in the following format (presented here as
CSV, To and From fields are dates):

Widget 1, Property 1, To, From
Widget 1, Property 2, To, From
Widget 1, Property 3, To, From
Widget 1, Property 4, To, From
Widget 1, Property 5, To, From
Widget 1, Property 6, To, From
Widget 1, Property 7, To, From
Widget 1, Property 8, To, From
Widget 1, Property 9, To, From
Widget 1, Property 10, To, From
Widget 2, Property 1, To, From
Widget 2, Property 2, To, From
Widget 2, Property 3, To, From
Widget 2, Property 4, To, From
Widget 2, Property 5, To, From
Widget 2, Property 6, To, From
Widget 2, Property 7, To, From
Widget 2, Property 8, To, From
Widget 2, Property 9, To, From
Widget 2, Property 10, To, From
I am building another table to be displayed as follows:

Widget_____|| Prop 1___ || Prop 2__ || Prop 3___ || Prop 4__ ||
----------------------------------------------------------------------
- Widget 1____|| To | From || To | From || To | From || To | From ||
----------------------------------------------------------------------
- Widget 2____|| To | From || To | From || To | From || To | From ||

/end feeble ASCII table rendition

Now.. I've gotten this work in flat ASP using a RecordSet, building a
table like above, but only with the "To" information, in ASP I am only
able to loop properly a single time without repeating the code, and
changing almost every line. I need to read this data 2 times, to get
a To and From and count. I placed the code to parse the data in a
class, sending the needed info over to get this going.. but I am
missing something in the switch from ASP to ASP.NET.

------- code ------
while (dr.Read())
{
strWidgetName = (string)dr.GetSqlString(0);
strWidgetNameOld = strWidgetName;
strWidgets[intCount] = strWidgetName;
// Fill Widget Name array
while (strWidgetName == strWidgetNameOld) // && (dr.Read()))
{
#region "Report logic is here and working. My problem is with the
looping."
boolDebug = dr.Read();
if (dr.Read() == false)
{
break;
}
else
{
clsReport.drCat.Read();
strWidgetNameOld = strWidgetName;
strWidgetName = (string)dr.GetSqlString(1);
}
}
intCount = ++intCount;
}
-------------- end code ------------------------

I've moved the pieces of the last Else statement around, seeing if
that would fix it. What I am seeing now it loops either too many
times, or it gets stuck on one Widget, and never progresses to the
next.


Sounds like this would do it for you:

~~~~~~~~~~~~~~~~~~~~~~~~~~~
String widget = null;
String newWidget = null;

while(dr.Read())
{
newWidget = dr.GetSqlString(0).Value;
if(newWidget != widget)
{
if(widget != null)
{
// End the previous row.
}
// Begin a new row.
widget = newWidget;
}
// Output the cell.
}

if(widget != null)
{
// End the row.
}
~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Z

Zac Maclean

I'll give it a try... Thanks.

I'll try to reply on Monday when I am able to test it.


Ryan Trudelle-Schwarz said:
I need to loop through a DataReader.

The data it contains is in the following format (presented here as
CSV, To and From fields are dates):

Widget 1, Property 1, To, From
Widget 1, Property 2, To, From
Widget 1, Property 3, To, From
Widget 1, Property 4, To, From
Widget 1, Property 5, To, From
Widget 1, Property 6, To, From
Widget 1, Property 7, To, From
Widget 1, Property 8, To, From
Widget 1, Property 9, To, From
Widget 1, Property 10, To, From
Widget 2, Property 1, To, From
Widget 2, Property 2, To, From
Widget 2, Property 3, To, From
Widget 2, Property 4, To, From
Widget 2, Property 5, To, From
Widget 2, Property 6, To, From
Widget 2, Property 7, To, From
Widget 2, Property 8, To, From
Widget 2, Property 9, To, From
Widget 2, Property 10, To, From
I am building another table to be displayed as follows:

Widget_____|| Prop 1___ || Prop 2__ || Prop 3___ || Prop 4__ ||
----------------------------------------------------------------------
- Widget 1____|| To | From || To | From || To | From || To | From ||
----------------------------------------------------------------------
- Widget 2____|| To | From || To | From || To | From || To | From ||

/end feeble ASCII table rendition

Now.. I've gotten this work in flat ASP using a RecordSet, building a
table like above, but only with the "To" information, in ASP I am only
able to loop properly a single time without repeating the code, and
changing almost every line. I need to read this data 2 times, to get
a To and From and count. I placed the code to parse the data in a
class, sending the needed info over to get this going.. but I am
missing something in the switch from ASP to ASP.NET.

------- code ------
while (dr.Read())
{
strWidgetName = (string)dr.GetSqlString(0);
strWidgetNameOld = strWidgetName;
strWidgets[intCount] = strWidgetName;
// Fill Widget Name array
while (strWidgetName == strWidgetNameOld) // && (dr.Read()))
{
#region "Report logic is here and working. My problem is with the
looping."
boolDebug = dr.Read();
if (dr.Read() == false)
{
break;
}
else
{
clsReport.drCat.Read();
strWidgetNameOld = strWidgetName;
strWidgetName = (string)dr.GetSqlString(1);
}
}
intCount = ++intCount;
}
-------------- end code ------------------------

I've moved the pieces of the last Else statement around, seeing if
that would fix it. What I am seeing now it loops either too many
times, or it gets stuck on one Widget, and never progresses to the
next.


Sounds like this would do it for you:

~~~~~~~~~~~~~~~~~~~~~~~~~~~
String widget = null;
String newWidget = null;

while(dr.Read())
{
newWidget = dr.GetSqlString(0).Value;
if(newWidget != widget)
{
if(widget != null)
{
// End the previous row.
}
// Begin a new row.
widget = newWidget;
}
// Output the cell.
}

if(widget != null)
{
// End the row.
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

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,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top