Sorry, i will try to post in proper forum next time.
here is the code (this is not the actual code from my application, but similar which has the same issue).
i have a Form that has a checkbox to display active (bit), Two TextBox for FirstName, LastName and a combo for status. and a button to show the employee...here is how it starts... after form loads.
// When the button is clicked, i am displaying the employee.
private void button1_Click(object sender, EventArgs e)
{
ShowEmployee();
}
private void ShowEmployee()
{
DataAccessAdapter adapter = new DataAccessAdapter();
employee = new EmployeeEntity(new System.Guid("685D3A76-D749-4A49-8C2A-512C67C83E68"));
adapter.FetchEntity(employee);
adapter.Dispose();
BindData();
}
private void BindData()
{
RemoveBindings();
textBox1.DataBindings.Add("Text", employee, "FirstName");
textBox2.DataBindings.Add("Text", employee, "LastName");
checkBox1.DataBindings.Add("Checked", employee, "Active");
comboBox1.DataBindings.Add("Value", employee,"EmployeeStatusConfigChoiceID");
DataIsBinded = true;
employee.PropertyChanged += new PropertyChangedEventHandler(CurrentEmployee_PropertyChanged);
}
private void RemoveBindings()
{
if (DataIsBinded)
{
textBox1.DataBindings.RemoveAt(0);
textBox2.DataBindings.RemoveAt(0);
checkBox1.DataBindings.RemoveAt(0);
comboBox1.DataBindings.RemoveAt(0);
}
}
private void CurrentEmployee_PropertyChanged(object sender, EventArgs e)
{
SaveCurrentEmployee();
}
private void SaveCurrentEmployee()
{
if (employee.IsDirty)
{
if (employee.Fields["EmployeeStatusConfigChoiceID"].IsChanged)
{
/* from this part as soon as i update the employee.Active to something it again goes back to CurrentEmployee_PropertyChanged, which makes sence because the property changed again..but its not working...this will be in a continious loop and throws
"An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
*/
switch (comboBox1.Text)
{
case "Active":
employee.Active = true;
break;
case "Fired":
employee.Active = false;
break;
case "Prospect":
employee.Active = true;
break;
case "Inactive":
employee.Active = false;
break;
}
}
//it Never gets saved.
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.SaveEntity(employee, true);
ShowEmployee();
}
}