Mysql Query with Infinite Loop in C#

Asked

Viewed 47 times

1

I’m doing a project with creating dynamic objects with CSS, after he enters the for, he inserts all the data of the database right, but apparently at the end he makes an infinite loop and appears this message:

An unhandled Exception of type 'System.Stackoverflowexception' occurred in System.Web.dll

            int qtd = 0;

            clsNoticia noticia = new clsNoticia();
            List<string> codigosNoticias = new List<string>();

                for (int i = 0; i < codigosNoticias.Count; i++)
                {
                    noticia.carregaNoticia(codigosNoticias[i]);

                    #region Noticias Criadas Dinamicamente
                    Label lblTituloNoticia = new Label();
                    lblTituloNoticia.CssClass = "tituloNoticia";
                    lblTituloNoticia.ID = "tituloNoticia_" + noticia.cd_noticia;
                    lblTituloNoticia.Text = noticia.nm_titulo;

                    Label lblLinhaFina = new Label();
                    lblLinhaFina.CssClass = "linhaFina";
                    lblLinhaFina.ID = "linhaFina_" + noticia.cd_noticia;
                    lblLinhaFina.Text = noticia.nm_linha_fina;

                    Panel pnlTituloNoticia = new Panel();
                    pnlTituloNoticia.ID = "pnlTituloNoticia";
                    pnlTituloNoticia.Controls.Add(lblTituloNoticia);

                    Panel pnlLinhaFina = new Panel();
                    pnlLinhaFina.ID = "pnlLinhaFina";
                    pnlLinhaFina.Controls.Add(lblLinhaFina);

                    Image img_noticia = new Image();
                    if (noticia.ic_destaque)
                    {
                        img_noticia.CssClass = "col1Img";
                    }
                    else
                    {
                        if (qtd <= 2)
                        {
                            img_noticia.CssClass = "col2Img";
                            qtd++;
                        }
                        else
                        {
                            img_noticia.CssClass = "col3Img";
                            qtd++;

                            if (qtd == 6)
                            {
                                qtd = 0;
                            }
                        }
                    }

                    if (File.Exists(Request.PhysicalApplicationPath + @"\images\noticias\" + noticia.cd_noticia + ".jpg"))
                    {
                        img_noticia.ImageUrl = "~/images/noticias/" + noticia.cd_noticia + ".jpg";
                    }

                    HyperLink lnkNoticia = new HyperLink();
                    lnkNoticia.ID = "link_" + noticia.cd_noticia;
                    lnkNoticia.CssClass = "linkPadrao";
                    lnkNoticia.Controls.Add(img_noticia);
                    lnkNoticia.Controls.Add(pnlTituloNoticia);
                    lnkNoticia.Controls.Add(pnlLinhaFina);

                    Panel pnlNoticia = new Panel();
                    pnlNoticia.ID = "noticia_" + noticia.cd_noticia;

                    if (noticia.ic_destaque)
                    {
                        pnlNoticia.CssClass = "col1";
                        pnlNoticia.CssClass = "mainContBloco";
                    }
                    else
                    {
                        if (qtd <= 2)
                        {
                            pnlNoticia.CssClass = "col2";
                            pnlNoticia.CssClass = "col2Bloco";
                        }
                        else
                        {
                            pnlNoticia.CssClass = "col3";
                            pnlNoticia.CssClass = "col3Bloco";

                            if (qtd == 6)
                            {
                                qtd = 0;
                            }
                        }
                    }

                    pnlNoticia.Controls.Add(lnkNoticia);
                    #endregion

                    pnlNoticia.Controls.Add(pnlNoticia);
                }
            }
  • It was a very syntactic error, in the last line of code I put pnlNoticia.Controls.Add(pnlNoticia); but I had created a Panel with the same name in index.html, so I just had to change the name, so it didn’t run, but thanks for the help :D

2 answers

1


In the third line you create an empty list codigosNoticiasthat is to say codigosNoticias.Count == 0

So on the fourth line you use codigosNoticias.Count to compare with i what makes the code inside the for never be executed.

// Esse for quivale a for (int i = 0; i < 0; i++)
for (int i = 0; i < codigosNoticias.Count; i++)
{
  //Esse bloco nunca é executado
}

What is missing from this code is to fill in the list codigosNoticias before trying to sue her.

1

It would need more information of the classes involved, usually this type of error happens by circular reference between class instance: Class1 depends on class 2, which depends on Class1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.