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;
}
}
}
}