Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I m getting error "An unhandled exception of type 'System.StackOverflowException' occurred in windowsApplication1.exe" on my application.

I have two forms in My application: Form1 and Form2. Both Form containong 2 textbox and one button.

The code i written in Form1 is Below:
C#
public string firstName
{
    set{ txtFirst.Text = value;}
    get { return firstName;}
}

public string lastName
{
    set{ txtLast.Text = value;}
    get { return lastName;}
}

private void btnNext_Click(Object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.show();
}

The code i written in Form2 is Below:
C#
Form1 f1 = new Form1(); // Throws exception " StackOverFlowException was unhandled"

private void insertRecord()
{
   //Code for SqlConnetion String and SqlCommand
   cmdParameters.addWithValue("@FirstName", f1.firstName);
   cmdParameters.addWithValue("@LastName", f1.lastName);
   cmdParameters.addWithValue("@Address", txtBox1.Text);
   cmdParameters.addWithValue("@City", txtBox2.Text);
}

private void btnNext_Click(Object sender, EventArgs e)
{
    insertRecord();
}

Anybody Plz Help me out. Its an urgent task for me bt while running this code i m getting error on Form2.

Regards
Posted
Updated 25-Jun-12 0:05am
v2

1 solution

Your property names are causing recursion that is generating the Exception. Look at this code..

C#
public string firstName
    {
        set{ txtFirst.Text = value;}
        get { return firstName;}
    }


When you access the firstName property, it is running the 'get' code which is calling firstName again. This will cause never ending recursion which ends up in a StackOverflow exception.

You should really name like this...

C#
private string firstName = string.empty;
public string FirstName
    {
        set{ txtFirst.Text = value;}
        get { return firstName;}
    }


Public fields should be capitalised, this will also avoid the StackOverflow since firstName and FirstName are treated differently now.

This also applies to your 'lastName' property.


NB: In future, you can figure out where StackOverflow exceptions are occuring in your code by using your debug window. When the exception causes the code to stop, from your Debug menu choose Debug->Windows->Call stack. This will show you all the calls that have happened up to this point, you will see lots of the same function calls in here (the recursive calls) which will tell you where the problem lies.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 25-Jun-12 6:28am    
Good one +5!
Maahi Sawarkar 25-Jun-12 6:56am    
Thanks for the reply Dylan..
i did the samething that u replied me but stil i m getting the same error on that line.
Also i m showing my code in as an example bt actually i have 45 fields in my first form that i tried to access on my second form but its nt working. Its show me the same error in my Form2.
Dylan Morley 25-Jun-12 7:17am    
OK, so use the debugger as I've mentioned and you should be able to find out where the problem is. From what you've posted, you are causing recursion through naming conflicts - so lookout for anything like that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900