473,406 Members | 2,312 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,406 software developers and data experts.

datareader - traversing results from a query

Hi all
I'm trying to traverse through the results from a query
that returns more than 1 row. The data reader reads only
the first row. The following code doesn't work. Let me
know what's wrong.
do
{
while(reader.read)
{
x += y;
reader.NextResult();
}
}while reader.NextResult();
I've tried a few different ways. Nothing seems to work.

Finally, after trying a few different things, now it
doesn't seem to read any of the rows. Thanks a lot for
your help.

Nov 15 '05 #1
6 11832
Maybe I misunderstand the goal, but I think you can just use dr.Read and
kill the outer loop.

Looks like the inner loop is pushing the pointer up with the nextResult.

If you just want to increment x, you can get rid of the outer look and kill
the NextResult in the inner loop.

while(reader.Read()){

x+=y;
}

That will traverse the entire reader until it's done.

HTH,

Bill

If I misunderstood what you are doing, let me know and I'll see what I can
do.
"lakshmi" <lm@newsgroup.net> wrote in message
news:c8****************************@phx.gbl...
Hi all
I'm trying to traverse through the results from a query
that returns more than 1 row. The data reader reads only
the first row. The following code doesn't work. Let me
know what's wrong.
do
{
while(reader.read)
{
x += y;
reader.NextResult();
}
}while reader.NextResult();
I've tried a few different ways. Nothing seems to work.

Finally, after trying a few different things, now it
doesn't seem to read any of the rows. Thanks a lot for
your help.

Nov 15 '05 #2
Thus spake lakshmi:
do
{
while(reader.read)
{
x += y;
reader.NextResult();
}
}while reader.NextResult();
I've tried a few different ways. Nothing seems to work.


NextResult moves to the next result set, not the next record. The Read
method automatically positions the cursor at the next row so there's no
need to manually move to the next record. Try this:

do
{
while (reader.Read())
{
x+= y; // Where is y coming from, anyway?
}
}
while reader.NextResult();

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #3
problem1:
thanks Frank and Bill.
sorry for not making my requirement clear.
my sql query is
sql = "SELECT Value from myTable";
cmd = new SqlCommand(sql);
SqlDataReader reader = cmd.ExecuteReader();
double myFinalValue = 0.0;
do
{
while(reader.Read)
{
myFinalValue = myFinalValue + reader.GetDouble(0);
}
}while (reader.NextResult();

my requirement is values from all different rows should be
summed and returned based on a few other business rules.
I'm not able to get the reader move past the first record.

problem2:
Now I have a new problem. I was trying hard to resolve the
above problem and apparently I broke another place.

I'm using datareader to read a 1 row result from a query.

sql = "Select name from myTable where id = 1";
SqlCommand cmd = new SqlCommand(sql);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
if(!reader.IsDBNull(0))
myName = reader.GetString(0);
}
reader.Close();
cmd.Dispose();
This was working fine until I don't know what I broke.
Although this query returns rows from SQL query analyzer,
reader.HasRows property returns 0 and the while loop is
never executed.
Any thoughts? Thanks a lot.
-----Original Message-----
Hi all
I'm trying to traverse through the results from a query
that returns more than 1 row. The data reader reads only
the first row. The following code doesn't work. Let me
know what's wrong.
do
{
while(reader.read)
{
x += y;
reader.NextResult();
}
}while reader.NextResult();
I've tried a few different ways. Nothing seems to work.

Finally, after trying a few different things, now it
doesn't seem to read any of the rows. Thanks a lot for
your help.

.

Nov 15 '05 #4
Thus spake lakshmi:
my requirement is values from all different rows should be
summed and returned based on a few other business rules.
I'm not able to get the reader move past the first record.


In that case, save yourself some trouble and use SELECT SUM in your
query. Feed that query to a command object and call ExecuteScalar.
You'll get the total in one shot.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #5
thanks Frank. But it is not that simple. I have to
traverse through each row and based on the value returned
I would either add the row value to the sum or not. (and a
few other business rules.)
My problem is how do I get the reader to move to the next
row. I'll try all your suggestions. thanks.
-----Original Message-----
Thus spake lakshmi:
my requirement is values from all different rows should be summed and returned based on a few other business rules.
I'm not able to get the reader move past the first
record.
In that case, save yourself some trouble and use SELECT SUM in yourquery. Feed that query to a command object and call ExecuteScalar.You'll get the total in one shot.

--
There are 10 kinds of people. Those who understand binary and those whodon't.

http://code.acadx.com
.

Nov 15 '05 #6
Lakshmi:

Your first problem still seems to be the same. Your inner loop gets to the
end of the reader, so there's nothing left. It executes once b/c you have a
Do While loop. I would get out of the out loop totally, and perhaps load an
arraylist with the value of Dr(0). Then you can close the reader and be
done with it. You can walk the arraylist up down and backwards which you
can't do with a reader. Since you are only grabbing one value and summing
it, this is very easy to implement.

For count 1

For i as Integer = 0 to Arraylist.Count
If (al(i) 'Meets business rules Then Increment it

Next

On the second query, if you are just grabbing one value, why not use
ExecuteScalar?
"lakshmi" <lm@newsgroup.net> wrote in message
news:c9****************************@phx.gbl...
problem1:
thanks Frank and Bill.
sorry for not making my requirement clear.
my sql query is
sql = "SELECT Value from myTable";
cmd = new SqlCommand(sql);
SqlDataReader reader = cmd.ExecuteReader();
double myFinalValue = 0.0;
do
{
while(reader.Read)
{
myFinalValue = myFinalValue + reader.GetDouble(0);
}
}while (reader.NextResult();

my requirement is values from all different rows should be
summed and returned based on a few other business rules.
I'm not able to get the reader move past the first record.

problem2:
Now I have a new problem. I was trying hard to resolve the
above problem and apparently I broke another place.

I'm using datareader to read a 1 row result from a query.

sql = "Select name from myTable where id = 1";
SqlCommand cmd = new SqlCommand(sql);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
if(!reader.IsDBNull(0))
myName = reader.GetString(0);
}
reader.Close();
cmd.Dispose();
This was working fine until I don't know what I broke.
Although this query returns rows from SQL query analyzer,
reader.HasRows property returns 0 and the while loop is
never executed.
Any thoughts? Thanks a lot.
-----Original Message-----
Hi all
I'm trying to traverse through the results from a query
that returns more than 1 row. The data reader reads only
the first row. The following code doesn't work. Let me
know what's wrong.
do
{
while(reader.read)
{
x += y;
reader.NextResult();
}
}while reader.NextResult();
I've tried a few different ways. Nothing seems to work.

Finally, after trying a few different things, now it
doesn't seem to read any of the rows. Thanks a lot for
your help.

.

Nov 15 '05 #7

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

Similar topics

2
by: rob | last post by:
I'm having some trouble with this.. My VB datareader seems to skip the first record in the reader. I've looked at my query in enterprise manager to confirm what I am suppose to receive back,...
6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: nabil m | last post by:
hi , i have been looking around on msdn and google but i still have a couple of oquestions about the difference between a datareader and a dataset - which to use when - 1) do i have to choose...
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
10
by: Scott Richards | last post by:
Hi I am getting a exception while using a datareader here is the code Me.SqlConnection1.Open() Me.SqlReservationCheck.Parameters("@Ref").Value = Ref Dim Reader As SqlClient.SqlDataReader = _...
2
by: David Beaven | last post by:
I am creating an aspx page to show (i.e. read only) hundreds or probably thousands of items from two database queries. The user may of course only want to read one or two pages worth. I have (I...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
1
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the...
3
by: DaveRook | last post by:
Hello Let me show you the code I'm trying to do and then I'll ask the question: SqlDataReader rdr = null; SqlConnection conn = new SqlConnection(strConn); string strSQL = "SELECT * FROM...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.