vaquas
vaquas

Reputation: 29

System.StackOverflowException

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

Upvotes: 3

Views: 8674

Answers (4)

manji
manji

Reputation: 47978

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.

Upvotes: 1

Boris Modylevsky
Boris Modylevsky

Reputation: 3099


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

Upvotes: 1

Martin
Martin

Reputation: 40325

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

Upvotes: 1

Konamiman
Konamiman

Reputation: 50273

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

Upvotes: 16

Related Questions