4
Answers

how to use goto inside foreach to end loop

mary jean ligas

mary jean ligas

10y
1.5k
1

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



Answers (4)