views:

49

answers:

1

This question is pretty simple, but I'm not seeing why the code isn't working?

Here's how I set the members of a ComboBox I have on my form:

private void LoadUsersToComboBox()
{
    ScansEntities3 db = new ScansEntities3();

    comboBox1.DataSource = db.People;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "ID";
}

And here's how I use the selected member:

private void CreateNewDepartment()
{
    if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
    {
        ScansEntities3 db = new ScansEntities3();  
        Department department = new Department()
        {
            Name = textBox1.Text,                    
            Person = (Person)comboBox1.SelectedItem,             
        };
        db.AddToDepartments(department);
        db.SaveChanges();
    }            
}

I'm getting this error:

System.InvalidOperationException was unhandled
  Message=An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.EntityObject.System.Data.Objects.DataClasses.IEntityWithChangeTracker.SetChangeTracker(IEntityChangeTracker changeTracker)
       at System.Data.EntityUtil.SetChangeTrackerOntoEntity(Object entity, IEntityChangeTracker tracker)
       at System.Data.Objects.ObjectStateManager.AddEntry(Object dataObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
       at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, Object entity, String argumentName)
       at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
       at SQLite_Testing_Grounds.ScansEntities3.AddToDepartments(Department department) in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Model2.Designer.cs:line 89
       at SQLite_Testing_Grounds.Form1.CreateNewDepartment() in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Form1.cs:line 64
       at SQLite_Testing_Grounds.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Form1.cs:line 51
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SQLite_Testing_Grounds.Program.Main() in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

There also another issue which might contribute to this code failing:

The 'Name' attribute is a string, expected since that's what it is in the database. The 'Person' attribute is a Department.Person type, when in the database it's a foreign key integer.

So this doesn't work:

    private void CreateNewDepartment()
    {
        if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
        {
            ScansEntities3 db = new ScansEntities3();  
            Department department = new Department()
            {
                Name = textBox1.Text,                    
                Person = ((Person)comboBox1.SelectedItem).ID,             
            };
            db.AddToDepartments(department);
            db.SaveChanges();
        }            
    }
A: 

I think you have to dispose your db. The problem here is that the the SelectedItem is still attached to a context. You can use the Detach() method of the context to detach the people from the context, but I think your real problem might be that your code should look like this:

private void LoadUsersToComboBox()
{
    using (ScansEntities3 db = new ScansEntities3())
    {
        comboBox1.DataSource = db.People;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
    }
}

If this doesn't solve it, try the following:

private void LoadUsersToComboBox()
{
    using (ScansEntities3 db = new ScansEntities3())
    {
        var people = db.People.ToList();

        foreach (var person in people)
            db.Detach(person);

        comboBox1.DataSource = people;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
    }
}
Pieter