views:

159

answers:

4

hi everyone, please help me regarding System.StackOverflowException i m desing a .aspx to write records into the database i used 4-tier archietecture to implement this everything are working but when im compiling the page then its displyae the fields to insert the data ,when i insert the data to those field and clik the submitt button then its showes System.StackOverflowException occured

public class Customers
{
    public Customers()
    {
        int CustomerID = 0;
        string Fname = string.Empty;
        string Lname = string.Empty;
        string Country = string.Empty;
    }
    public int CustomerID
    {
        get { return CustomerID; }
        set { CustomerID = value; }
    }
    public string Fname
    {
        get { return Fname; }
        set { Fname = value; }****
    }
    public string Lname
    {
        get { return Lname; }
        set { Lname = value; }
    }
    public string Country
    {
        get { return Country; }
        set { Country = value; }
    }

When page is executing a window is popuped and displayed System.StackOverflowException occured please give me anyone solution to this problem

+10  A: 
public int CustomerID
{
    get { return CustomerID; }
    set { CustomerID = value; }
}

You are assigning the value to itself recursively. And the same for the other properties.

You need to define a backup field with another name, for example:

private int _CustomerId;
public int CustomerID
{
    get { return _CustomerID; }
    set { _CustomerID = value; }
}

Or even better:

public int CustomerId {get; set;}
Konamiman
+1 for auto-implemented properties...
Robert Koritnik
A: 

there is infinite recusrive calls in your properties Get & Set like:

string Lname{ 
    get { return Lname; } 
    set { Lname = value; }
}

Lname = value; will call your property again & again.

najmeddine
+1  A: 

Try the following:

public class Customers
{

    private int _CustomerID; 
    private string _Fname;
    private string _Lname;
    private string _Country;

    public Customers()    
    {        
     int CustomerID = 0;        
     string Fname = string.Empty;        
     string Lname = string.Empty;        
     string Country = string.Empty;    
    }    

    public int CustomerID    
    {        
     get { return _CustomerID; }        
     set { _CustomerID = value; }    
    }    

    public string Fname    
    {        
     get { return _Fname; }        
     set { _Fname = value; }
    }    

    public string Lname    
    {        
     get { return _Lname; }        
     set { _Lname = value; }    
    }    

    public string Country    
    {        
     get { return _Country; }        
     set { _Country = value; }    
    }
Martin
+1  A: 
Boris Modylevsky
Doesn't work on C# 2.0.
Kobi
@Kobi It does when you use VS2008 (even targeting .NET 2.0)
hmemcpy