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())
               [B]  //   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.[/B]
                {
                    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; 
             }
        }
       }
kvprajapati commented: No code tags -1

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.

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()))
        {
            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())
                {
                    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;
                }

            }
        }

    }

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.