| | |
Exit from for loop
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
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.
•
•
Join Date: Jun 2009
Posts: 446
Reputation:
Solved Threads: 82
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.
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)
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; } } } }
break; - no need to post source code.
sonia sardana - read this,
http://catb.org/esr/faqs/smart-questions.html
sonia sardana - read this,
http://catb.org/esr/faqs/smart-questions.html
![]() |
Similar Threads
- How to exit a loop (C++)
- while loop (Java)
- exiting loop with switch (C++)
- Loop troubles (C++)
- Alright this loop problem is bugging me (Java)
- help me understand end of do while loop (C++)
Other Threads in the ASP.NET Forum
- Previous Thread: Need Help
- Next Thread: Connecting to Oracle from VWD
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol ajax alltypeofvideos appliances application asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox commonfunctions control dataaccesslayer database datagridview datagridviewcheckbox datalist deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose feedback fileuploader fill findcontrol flash form formatdecimal formview google gridview gudi iis image javascript listbox login microsoft mobile mouse mssql news novell numerical opera panelmasterpagebuttoncontrols parent radio redirect registration relationaldatabases reportemail save schoolproject search security select sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos view vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers






