I have a data entry form with many textbox’s and some dropdowns for the user to input data. When the user selects a “Location” from the dropdownlist, they can click a button on top of the form to view a popup with more details according to that location. The data successfully automates when the popup loads but when the user tries to close the popup and continue with the main form, an unhandled exception occurs for the system.InvalidOperationException. The error specifically occurs because “The collection I’m Enumerating through has been changed”. Although I’m not changing anything I guess something behind the scenes is happening, here is my code to retreive the data:
string postalCode;
string phone1;
string phone2;
string supervisor;
var ObjectContext = new ObjectContext();
var qry = (from i in ObjectContext.TableLocation
where i.LocationName == LocationValue
select i).ToList();
foreach (var data in qry)
{
postalCode = data.postalCode;
phone1 = data.phoneNumber1;
phone2 = data.phoneNumber2;
supervisor = data.supervisor
}
txtPostalCode.Text = postalCode;
txtPhone1.Text = Phone1;
txtPhone2.Text = Phone2;
txtSupervisor.Text = supervisor;
The LocationValue is linked to a Public variable that the parent form fills with whatever is selected in the location dropdownlist:
public string CountyValue
{
get { return txtCountyName.Text; }
set { txtCountyName.Text = value; }
}
Is there a better way to enumerate through this list of values and supply them to textbox.text? I have tried everything to fix this error.
EDIT
Also all my database columns are Varchars so there was no need to convert data types.
And I only get this error when I deploy my app via ClickOnce to clients PC.
From what I see, that Location is a single value, and there is no need to create a List. So that means you can avoid iterating over a list, and do this instead:
ADDED:
Also check this MSDN Reference, according to it, there are several scenarios where ShowDialog() could throw an InvalidOperationException, that are unrelated to LINQ-to-SQL or EF.
ADDED: From that MSDN article it says this: