How to exit a while loop inside of a while loop
-
For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database reader.ExecuteReader() method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!
Jude
-
For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database reader.ExecuteReader() method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!
Jude
the usual skeleton is: using (SqlDataReader dbreader = reader.ExecuteReader()) { // all your code here while (dbreader.Read()) { // use break wherever you want } } // at this point, dbreader will be disposed properly
Best regards, Jaime.
-
For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database reader.ExecuteReader() method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!
Jude
BTW, you have a repeated question, maybe you can delete it.
Best regards, Jaime.
-
BTW, you have a repeated question, maybe you can delete it.
Best regards, Jaime.
Thanx for the answer....I didn't repost, but I see it. When I try to access ir I receive this error:
XML Parsing Error: not well-formed
Location: http://www.codeproject.com/script/Forums/Edit.aspx?fid=1649&select=3274455&floc=/Forums/1649/Csharp.aspx&action=r
Line Number 63, Column 34:if (typeof(_gat) != "undefined" && _gat)Jude