break in while and start reading from where it was left earlier

sana

New member
Joined
Feb 12, 2014
Messages
2
Programming Experience
1-3
Hi,
I have a while loop to read result of datareader in which I added a test with a break.
when the break will execute the while loop is repeated from the beginning instead of continuing from where it was left earlier.

C#:
 while (iRow <= count-1)                 {
                     read.Read(); 
                      
                    if (iTopMargin >=  240)
                    {
                        bMorePagesToPrint = true;
                       
                        break;


                    }
Please can you help in this probleme
Thank you
 

sana

New member
Joined
Feb 12, 2014
Messages
2
Programming Experience
1-3
Can i have an equivalent to this code to read result of sqdatreader
C#:
    while (iRow <= dataGridListe.Rows.Count - 1)
instead of
C#:
    while (reader.read())
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
First of all, 'while' loops don't have a beginning. They simply keep run while a condition is true. If you think that your loop has a definite beginning then you should be using a 'for' loop and not a while loop. If you're trying to loop through the items of a collection by index then absolutely you should be using a 'for' loop. The loop will start with the loop counter at whatever value you want it to start with. If you want it to start at zero then start at zero. If you want it to start at some other value then start it at some other value. How do you usually store a value that varies over time? In a variable of course.
 

jargoman

Member
Joined
Dec 19, 2013
Messages
7
Programming Experience
3-5
I'm not sure what you mean. Maybe this?

C#:
while (iRow <= dataGridListe.Rows.Count - 1) {
                    if (read.Read()) break; 
                      
                    if (iTopMargin >=  240)
                    {
                        bMorePagesToPrint = true;
                       
                        continue;
                    }
 

jargoman

Member
Joined
Dec 19, 2013
Messages
7
Programming Experience
3-5
You don't know what I mean when I say use a 'for' loop instead of a 'while' loop?
No I didn't understand the op's question. His title was
[h=2]break in while and start reading from where it was left earlier[/h]
I think he was asking how to continue
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
No I didn't understand the op's question. His title was


I think he was asking how to continue
Oops! Sorry, I thought that you were the OP answering me. Didn't look at the names properly.
 
Top Bottom