I have this code. The code gets me some values from a table , table contents , some textboxes and other , things. When I am click button the submit button, I get a value and put in “st” (type class student) and put in the database. But it is showing me an exception in list attributes ” get {….}” the exception “System.StackOverflowException”
public StudentManager()
: base(ConfigurationManager.ConnectionStrings["con"].ConnectionString)
{
}
public override void Add(Student entity)
{
//add to database
}
protected void submitButton_Click(object sender, EventArgs e)
{
Student st = new Student();
st.id = Convert.ToInt32(IdTextBox.Text);
st.AVG = Convert.ToDouble(AVGTextBox.Text);
st.date = dateCalendar.TodaysDate;
st.educationInfo = educationInfoTextBox.Text;
faculty fa = new faculty();
fa.id = Convert.ToInt32(facultyDropDownList.SelectedValue);
st.faculty = fa;
st.fatherName = fatherNameTextBox.Text;
st.fName = fNameTextBox.Text;
st.lName = lNameTextBox.Text;
st.motherName = motherNameTextBox.Text;
st.password = passwordTextBox.Text;
st.personalInfo = personalInfoTextBox.Text;
StudentManager sm = new StudentManager();
sm.Add(st);
}
public class Student
{
public int id { get; set; }
public faculty faculty { get; set; }
public double AVG { get; set; }
public DateTime date { get; set; }
public string educationInfo { get; set; }
public string fatherName { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string motherName { get; set; }
public string password { get; set; }
public string personalInfo { get; set; }
private List<SqlParameter> Attributes;
public List<SqlParameter> attributes
{
get
{
Attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id",this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo",educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return Attributes;
}
}
}
It’s because the property in you class makes recursive calls.
To resolve this I feel it would be better to convert it to a method instead of using a property. It can be done like this:
Remove
private List<SqlParameter> Attributes;from your code