Hello everyone would like to ask if how to end my very first loop cycle in foreach statement using goto but this code gives me error "no such label within the scope of the goto statement". I want to end the looping so i put it inside finally block. please help. dunno whats the logic. Thanks you so much. please see attached snippet.
Snippet:
try
{
if (drpchkPlant.Texts.SelectBoxCaption != "Select")
{
string[] plnt = drpchkPlant.Texts.SelectBoxCaption.Split(',');
foreach (var pn in plnt)
{
var pl = (from s in con.Plants
where s.PlantLongName == pn.ToString()
select new { s.PlantIdx, s.PlantCD }).ToList();
if (pl.Count != 0)
{
string plntseries = pl[0].PlantCD;
string[] so = drpchkSalesOffice.Texts.SelectBoxCaption.Split(',');
foreach (var iso in so)
{
var sos = (from s in con.SalesOffices
where s.SalesOfficeName == iso.ToString()
&& s.PlantPIdx == pl[0].PlantIdx
select new { s.SalesOfficeIdx, s.SalesOfficeCD }).ToList();
if (sos.Count != 0)
{
string soseries = sos[0].SalesOfficeCD;
string[] sku = drpchkSKU.Texts.SelectBoxCaption.Split(',');
foreach (var i in sku)
{
HtmlTableCell tmaincell2 = new HtmlTableCell();
var d = (from s in context.Dim_Product_Items
where s.Item_Name == i.ToString()
select new { s.Item_Code, s.Item_Idx }).ToList();
if (d.Count != 0)
{
string series = d[0].Item_Code;
TextBox txrowfour2 = (Tablet.FindControl("txrowfour2-" + series + soseries + plntseries)) as TextBox; txrowfour2.Text = Request.Form.Get(txrowfour2.UniqueID);
TextBox txrowfour3 = (Tablet.FindControl("txrowfour3-" + series + soseries + plntseries)) as TextBox; txrowfour3.Text = Request.Form.Get(txrowfour3.UniqueID);
TextBox txrowfour4 = (Tablet.FindControl("txrowfour4-" + series + soseries + plntseries)) as TextBox; txrowfour4.Text = Request.Form.Get(txrowfour4.UniqueID);
ScriptManager.RegisterStartupScript(this, GetType(), "MyScript_Copy", "Copy('" + cphID + "_" + dynamicSKU.ID + "','" + cphID + "_" + txrowfour3.ID + "');", true);
goto StopLoop;
}
}
}
}
}
}
}
}
catch (Exception ex) { Response.Write(ex); }
finally
{
StopLoop:
con.Dispose();
context.Dispose();
}

VulpesPosted Sep 4, 2014, 4:30 AM
If you want to jump out of ALL the foreach loops, then you'll need to put the label where I suggested.
If you want to jump out of the second and third foreach loops, then put the label where you've just suggested. The next iteration of the first foreach loop will then take place.
mary jean ligasPosted Sep 3, 2014, 8:01 PM
VulpesPosted Sep 3, 2014, 7:51 AM
Jignesh TrivediPosted Sep 2, 2014, 11:54 PM
Hi,
you can also use break instead of this goto statement
for example, l = new List();
List
l.Add("test");
l.Add("Test1");
l.Add("test2");
l.Add("Test3");
int i = 0;
foreach(string s in l)
{
if(i==1)
{
break;
}
Console.Write(s);
i = i + 1;
}
hope this will help you.