Solved

C# Nested While loops and datareader problem

Posted on 2008-10-21
4
2,811 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
ID: 22766627
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
ID: 22766832
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
ID: 22768602
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
ID: 31508234
Thanks eveyone this has really helped!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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