943,844 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 1481
  • ASP.NET RSS
Jul 17th, 2009
-1

Exit from for loop

Expand Post »
I want to just exit from the loop, I m facing the probs,see bolded part...


protected void btnSellProduct_Click(object sender, EventArgs e)
    {

        string sPath = Server.MapPath("~/");
        string sFileName = sPath + "item.txt";
        string[] sArr;
        string[] sArr2;
        int lCount;
        int lCurrentStock;
        int lSellingStock;
        

        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            for (lCount =0;lCount <= sArr.Length -1; lCount ++)
            {
                sArr2 = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);

                if (sArr2[0].Trim() == txtSellProdID.Text.Trim())
                 //   Once Control go inside this condition,i want to exit from the for loop. For Dat  
                 //       i Write break,but 
                 //for (lCount =0;lCount <= sArr.Length -1; lCount ++)
                 //    in the above line , lCount ++ is underline - Unreachable code detected.
                {
                    lCurrentStock = Convert.ToInt32(sArr2[3]);
                    lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
                    if (lSellingStock > lCurrentStock)
                        lblStatus.Text = "Could not Sell this Product.Out of Stock";
                     else
                    {
                        WriteToTransactionFile();
                        DecrementFromItemFile();
                        lblStatus.Text = "Transaction Processed Successfully";
                    }                   
                }
                break; 
             }
        }
       }
Last edited by peter_budo; Jul 18th, 2009 at 12:50 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Jul 18th, 2009
0

Re: Exit from for loop

You have put the break statement after closing the 'if' block and before the closing braces of 'for' loop.

So the' for' loop will be executed only once and the counter lCount++ will never be executed. Therefore it displays 'Unreachable code detected' error.

You should put the break statement inside the 'if' block.

I have changed your code.

ASP.NET Syntax (Toggle Plain Text)
  1. protected void btnSellProduct_Click(object sender, EventArgs e)
  2. {
  3. string sPath = Server.MapPath("~/");
  4. string sFileName = sPath + "item.txt";
  5. string[] sArr;
  6. string[] sArr2;
  7. int lCount;
  8. int lCurrentStock;
  9. int lSellingStock;
  10.  
  11. if (File.Exists(sFileName.Trim()))
  12. {
  13. sArr = System.IO.File.ReadAllLines(sFileName);
  14. for (lCount = 0; lCount <= sArr.Length - 1; lCount++)
  15. {
  16. sArr2 = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);
  17.  
  18. if (sArr2[0].Trim() == txtSellProdID.Text.Trim())
  19. {
  20. lCurrentStock = Convert.ToInt32(sArr2[3]);
  21. lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
  22. if (lSellingStock > lCurrentStock)
  23. lblStatus.Text = "Could not Sell this Product.Out of Stock";
  24. else
  25. {
  26. WriteToTransactionFile();
  27. DecrementFromItemFile();
  28. lblStatus.Text = "Transaction Processed Successfully";
  29. }
  30. break;
  31. }
  32.  
  33. }
  34. }
  35.  
  36. }
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Jul 18th, 2009
0

Re: Exit from for loop

break; - no need to post source code.
sonia sardana - read this,
http://catb.org/esr/faqs/smart-questions.html
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 18th, 2009
0

Re: Exit from for loop

hi ramesh thx very much,its working,hi adatapost, when i didnt put the code,i got the answers put the code,when i start putting it,i got not to put it...
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Need Help
Next Thread in ASP.NET Forum Timeline: Connecting to Oracle from VWD





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC