Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Nested While loops and datareader problem

Posted on 2008-10-21
4
Medium Priority
?
2,962 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 400 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 400 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 200 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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