Solved

C# Nested While loops and datareader problem

Posted on 2008-10-21
4
2,786 Views
Last Modified: 2012-05-05
HI I am working with datareader. I have 2 seperate connections strings and cmds executing /
setup so I can do the different reads.

I get multiple results for the first datareader (dr) and I know the single record in the
dr3 datareader will match one of the values but the second while loop doesn't re-read the
same single piece of data. Shouldn't the while loop cause the datareader (dr3) to re-read
and recompare the result?

Thanks

Baz
While (dr.Read())
 

{

	while (dr3.Read())

	{

	

      if (dr3["data"].ToString() == dr["otherdata"].ToString())
 

	{
 

	//do something
 

	}
 

	}

}

Open in new window

0
Comment
Question by:ipgbarry
4 Comments
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 100 total points
Comment Utility
You will need to 'reset' the dr3 datareader, because it will loop until the end of the records, and then it has no data left, so it will not loop the next time the dr.read() loop iterates.

Kind regards,

Matthias Vance
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 100 total points
Comment Utility
A Datareader is read-only, foreward-only.  There is no way to reset the datareader back to the beginning, and re-read it.  I would copy the values from the second datareader into an array.
 
AW
 
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 50 total points
Comment Utility
i agree with the comments of MatthiasVance & Arthur_Wood - as the SqlDataReader is a forward only and once it reaches to the end as in your inner while loop you cannot reset it back to the beginning.

i recommend that as you are doing operations in a connected mode (data readers have a open connection to the database), try doing your operations in a disconnected mode by using data table or a dataset. The operations using a dataset/datatable can be a little slower but they provide you with the flexibility of kind of operations you want to do - as is your requirement
0
 

Author Closing Comment

by:ipgbarry
Comment Utility
Thanks eveyone this has really helped!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 23
ASP.NET Web Form Variables being reset 2 17
Hide Tab Page 3 18
Jquery autocomplete 10 19
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now