Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3322180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:09:05+00:00 2026-05-17T23:09:05+00:00

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

  • 0

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();
        }            
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:09:05+00:00Added an answer on May 17, 2026 at 11:09 pm

    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";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty sure this is a simple question in regards to formatting but here's
This is a pretty simple question but I'm somewhat stumped. I am capturing sections
I think this is a pretty simple question but I'm having trouble finding the
I know this question seems subjective but it's really pretty simple. As a long
I have a probably pretty simple question but I am still not sure! Actually
Pretty sure this question counts as blasphemy to most web 2.0 proponents, but I
This seems like a pretty softball question, but I always have a hard time
Ok so I realize that this is a pretty vague question, but bear with
This is a pretty-much theoretical question, but.. How much of an operating system could
This question is pretty much the same as this .Net question exept for java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.