Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Nested While loops and datareader problem

Posted on 2008-10-21
4
Medium Priority
?
2,922 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

660 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