Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to exit a while loop inside of a while loop

How to exit a while loop inside of a while loop

Scheduled Pinned Locked Moved C#
helpquestioncsharpdatabasetutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheJudeDude
    wrote on last edited by
    #1

    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

    J 2 Replies Last reply
    0
    • T TheJudeDude

      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

      J Offline
      J Offline
      Jaime Olivares
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T TheJudeDude

        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

        J Offline
        J Offline
        Jaime Olivares
        wrote on last edited by
        #3

        BTW, you have a repeated question, maybe you can delete it.

        Best regards, Jaime.

        T 1 Reply Last reply
        0
        • J Jaime Olivares

          BTW, you have a repeated question, maybe you can delete it.

          Best regards, Jaime.

          T Offline
          T Offline
          TheJudeDude
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups