Property in UserControl:
public JobQuote quote
{
get
{
if (ViewState["Quote"] != null)
return (JobQuote)ViewState["Quote"];
else
{
JobQuote newQuote = new JobQuote();
return newQuote;
}
}
set { ViewState["Quote"] = value; }
}
Code being run in UserControl:
protected void button_UploadFile_Click(object sender, EventArgs e)
{
if (FileUploader.HasFile)
{
try
{
quote.JobFileNames.Add(System.IO.Path.GetFileName(FileUploader.FileName));
}
catch (Exception ex)
{
label_UploadStatus.Text = "Upload status: The file could not be uploaded. <br />The following error occurred: " + ex.Message;
}
}
}
Property from JobQuote.cs class:
public List<string> JobFileNames
{
get
{
return JobFileNames;
}
set { JobFileNames = value; }
}
The exception is being thrown in the Try block when the code tries to access the JobFileNames property of the JobQuote class.
The getter and setter of the
JobFileNamesproperty are referencing themselves, causing an infinite loop. When that loop exhausts the available stack space you get aStackOverflowException.You probably need some sort of backing field for the property. Either explicit…
…or, better still, auto-implemented…