Solved

C# Nested While loops and datareader problem

Posted on 2008-10-21
4
2,833 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

680 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