Reading Datasets

E

et

How do I read a dataset so that I get only one value of one field, then read
the entire database. For instance, I'm doing a database of bakery products,
want to list all cakes. General query, Select * from tblProducts where
Category = 'Cakes"
So I read the first row so tht I can get a heading, then want to list all
items. But because I've already "read" the first row, the list of all items
do not contain the first item in my datareader. How do I fix this, do I
have to do 2 queries, one to get the heading, then the second query to get
the list of products under that heading. Seems to me that's going backwards
from using recordsets.

dr.Read

Response.Write(" <tr><td colspan=3><font size=+1><b>" &
dr("Category")</td></tr>")

while dr.read

Response.Write("<tr><td>" & dr("ProductName") & "</td><td>")

end while
 
D

David Browne

et said:
How do I read a dataset so that I get only one value of one field, then
read
the entire database. For instance, I'm doing a database of bakery
products,
want to list all cakes. General query, Select * from tblProducts where
Category = 'Cakes"
So I read the first row so tht I can get a heading, then want to list all
items. But because I've already "read" the first row, the list of all
items
do not contain the first item in my datareader. How do I fix this, do I
have to do 2 queries, one to get the heading, then the second query to get
the list of products under that heading. Seems to me that's going
backwards
from using recordsets.

dr.Read

Response.Write(" <tr><td colspan=3><font size=+1><b>" &
dr("Category")</td></tr>")

while dr.read

Response.Write("<tr><td>" & dr("ProductName") & "</td><td>")

end while

There are two answers here. First, your immediate problem is just a coding
problem.

dr.Read

Response.Write(" <tr><td colspan=3><font size=+1><b>" &
dr("Category")</td></tr>")

do
Response.Write("<tr><td>" & dr("ProductName") & "</td><td>")
loop while dr.Read()

But this, and 100 similar problems can be avoided by using a DataSet instead
of a DataReader. You have to force your program logic to conform to the
forward-only nature of the DataReader. This example is easy to work around,
but others aren't so much. Just byte the bullet and use DataSets, and then
you can sort, group, loop, compute, cache, etc to get the data when you need
it, not when you have to.

David
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top