2

I have a problem with a program with a silent error. I don't understand what it means. It is something with the this.ResumeLayout(false); ==> on frominloggen

and the window that popps up says this :

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

This is my main():

namespace Gip_info_Benny
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frominloggen());
        }
    }
}

this is my code for frominloggen

        public frominloggen()
        {
            InitializeComponent();

            timer1.Start(); 


            initialiseerDB();
            eersteForm = new frominloggen();
            tweedeForm = new FrmWelkom();
            derdeForm = new FrmKijkafwezighedenEnloonberekeningNa();
            vierdeForm = new FrmBekijklonen();
            vijfdeForm = new FrmKijkPersoneelsGegevensNaEnPasAan();
            zesdeForm = new FrmVoegPersoneelslidToe();
            zevendeForm = new FrmVerwijderPersoneelslid();
            achtsteFrom = new FrmPersoneelsgegevensWijzigenUitvoeren();

            txtPaswoordInvoeren.PasswordChar = '*';
        }

        private void initialiseerDB()
        {
            cnnGip = new SqlConnection();
            cnnGip.ConnectionString = @"Data Source=BENNYLAPTOP\SQLEXPRESS";
        }

        private void frominloggen_Load(object sender, EventArgs e)
        {
            MenuStrip strip;
            strip = new MenuStrip();

            ToolStripMenuItem keuze1, keuze2, keuze3, keuze4, keuze5;
            keuze1 = new ToolStripMenuItem("&Home");
            keuze2 = new ToolStripMenuItem("&Afwezigheden/Loonberekening");
            keuze3 = new ToolStripMenuItem("&Bekijk lonen");
            keuze4 = new ToolStripMenuItem("&Personeelsgegevens wijzigen");
            keuze5 = new ToolStripMenuItem("&Toevoegen/Verwijderen");
            keuze5.DropDownItems.Add("Toevoegen personeelslid", null, a_click);
            keuze5.DropDownItems.Add("Verwijderen personeelslid", null, b_click);
            strip.Items.Add(keuze1);
            strip.Items.Add(keuze2);
            strip.Items.Add(keuze3);
            strip.Items.Add(keuze4);
            strip.Items.Add(keuze5);
            this.Controls.Add(strip);
        }
        private void a_click(object sender, EventArgs e)
        {
            zesdeForm.ShowDialog();
        }

        private void b_click(object sender, EventArgs e)
        {
            zevendeForm.ShowDialog();
        }

        private void btnInlogen_Click(object sender, EventArgs e)
        {
            SqlCommand scmdcontroleergebruiker;
            scmdcontroleergebruiker = new SqlCommand();
            scmdcontroleergebruiker.Connection = cnnGip;
            scmdcontroleergebruiker.CommandType = CommandType.Text;
            scmdcontroleergebruiker.CommandText = "SELECT GebruikersNaam,Paswoord FROM Tbl_Account WHERE GebruikersNaam = '" + txtGebruikersnaamInvoeren.Text + "' , Paswoord = '" + txtPaswoordInvoeren.Text + "';";

            SqlDataReader sdrReadGebuiker;
            cnnGip.Open();
            sdrReadGebuiker = scmdcontroleergebruiker.ExecuteReader();

            int teller = 0;

            while (sdrReadGebuiker.Read())
            {
                teller += 1;
            }
            if (teller == 1)
            {
                MessageBox.Show("Gebruikersnaam en paswoord correct");
                tweedeForm.Naamgebruiker(txtGebruikersnaamInvoeren.Text); //is makkelijker als het werkt
                tweedeForm.ShowDialog();
                this.Hide();
            }
            else if (teller > 0)
            {
                MessageBox.Show("Gebruiker niet in gebruik");
            }
            else
            {
                MessageBox.Show("Gebruikersnaam of pasword niet correct");
            }

            txtGebruikersnaamInvoeren.Clear();
            txtPaswoordInvoeren.Clear();
            sdrReadGebuiker.Close();
            cnnGip.Close();
        }

        private void frominloggen_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Wil u het programma sluiten?", "Sluiten", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                Application.Exit();
            }
            else if (dialog == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime DatumTijd = DateTime.Now;
            lblHuidigetijd.Text = DatumTijd.ToString();
        }
    }
}

And this is my component():

namespace Gip_info_Benny
{
    partial class frominloggen
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.txtGebruikersnaamInvoeren = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btnInlogen = new System.Windows.Forms.Button();
            this.txtPaswoordInvoeren = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lblHuidigetijd = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // txtGebruikersnaamInvoeren
            // 
            this.txtGebruikersnaamInvoeren.Location = new System.Drawing.Point(644, 137);
            this.txtGebruikersnaamInvoeren.Name = "txtGebruikersnaamInvoeren";
            this.txtGebruikersnaamInvoeren.Size = new System.Drawing.Size(100, 20);
            this.txtGebruikersnaamInvoeren.TabIndex = 6;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(567, 186);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(54, 13);
            this.label2.TabIndex = 9;
            this.label2.Text = "Paswoord";
            // 
            // btnInlogen
            // 
            this.btnInlogen.Location = new System.Drawing.Point(649, 220);
            this.btnInlogen.Name = "btnInlogen";
            this.btnInlogen.Size = new System.Drawing.Size(95, 41);
            this.btnInlogen.TabIndex = 5;
            this.btnInlogen.Text = "Inloggen";
            this.btnInlogen.UseVisualStyleBackColor = true;
            this.btnInlogen.Click += new System.EventHandler(this.btnInlogen_Click);
            // 
            // txtPaswoordInvoeren
            // 
            this.txtPaswoordInvoeren.Location = new System.Drawing.Point(644, 183);
            this.txtPaswoordInvoeren.Name = "txtPaswoordInvoeren";
            this.txtPaswoordInvoeren.Size = new System.Drawing.Size(100, 20);
            this.txtPaswoordInvoeren.TabIndex = 7;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(537, 140);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(84, 13);
            this.label1.TabIndex = 8;
            this.label1.Text = "Gebruikersnaam";
            // 
            // lblHuidigetijd
            // 
            this.lblHuidigetijd.AutoSize = true;
            this.lblHuidigetijd.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblHuidigetijd.Location = new System.Drawing.Point(793, 411);
            this.lblHuidigetijd.Name = "lblHuidigetijd";
            this.lblHuidigetijd.Size = new System.Drawing.Size(0, 20);
            this.lblHuidigetijd.TabIndex = 10;
            // 
            // frominloggen
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(866, 457);
            this.Controls.Add(this.lblHuidigetijd);
            this.Controls.Add(this.txtGebruikersnaamInvoeren);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnInlogen);
            this.Controls.Add(this.txtPaswoordInvoeren);
            this.Controls.Add(this.label1);
            this.Name = "frominloggen";
            this.Text = "frominloggen";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frominloggen_FormClosing);
            this.Load += new System.EventHandler(this.frominloggen_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtGebruikersnaamInvoeren;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnInlogen;
        private System.Windows.Forms.TextBox txtPaswoordInvoeren;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lblHuidigetijd;
        private System.Windows.Forms.Timer timer1;
    }
}
user1251007
  • 15,891
  • 14
  • 50
  • 76
Benny De Pauw
  • 21
  • 1
  • 4
  • Welcome to Stack Overflow. Please re-format your question. http://stackoverflow.com/help/formatting – Soner Gönül Apr 28 '15 at 07:59
  • this is the problem, when i start the program i get this pop up "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" thx for the help – Benny De Pauw Apr 28 '15 at 08:02
  • 1
    if you run it in the debugger, are you able to see any of the call stack when the exception occurs and the debugger steps in? (ctrl+d,c to open the call-stack) – Marc Gravell Apr 28 '15 at 08:10

1 Answers1

6

The issue is that you are calling eersteForm = new frominloggen(); in constructor of frominloggen class. It fall in infinity loop, because new frominloggen() will call constructor of frominloggen class again and so on....

To detect this, you just need to use the debugger in the IDE; for example if I made a standard winforms app and reproduce your error:

    public Form1()
    {
        InitializeComponent();

        var frm = new Form1();
    }

Now press play (f5) and get the app to explode in the expected way. The debugger should interrupt us to tell us about the stack-overflow; we can now bring up the call stack (ctrl+d, c); this appears something like below:

Call-stack showing recursive Form1() calls

Each line in the call-stack is a different layer of calling; so the Form1() constructor (line 19) is calling the Form1() constructor, which is calling the Form1() constructor. At the very top there is usually something different, which just tells us what it is doing when it ran out of space. And if we look at line 19 in the Form1() constructor, it is:

var frm = new Form1();

And that is how you investigate a stack-overflow. Incidentally, you can double-click on any line in the call-stack window to jump directly to that call-site, including the ability to read any local variables (etc) in that method - so you can diagnose what is happening at each step in the recursion (useful if you are intentionally dealing with object trees / graphs, or linked-lists).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
bot_insane
  • 2,545
  • 18
  • 40