in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Exception adding record to BindingSource if BS bound to list/combobox

Last post 02-10-2010 3:57 AM by Dionis4ever. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 09-11-2009 2:20 AM

    Exception adding record to BindingSource if BS bound to list/combobox

    Hi,

    Just started with .NET,C#, ADO.NET, etc, etc, so possible something incredible trivial I'm missing here, in which case my apologies...

    I've encountered an odd problem whereby I cannot add records to a BindingSource if that BindingSource is also used as the DataSource for a listbox/combo, etc.  If I disable the data binding it works fine.

    I've produced a minimalist example which produces the error: http://media.tanima.co.uk/dblistboxtest.zip

    All I'm doing is:

            private void button1_Click(object sender, EventArgs e)
            {
                SoundingHeader sh = new SoundingHeader();
                sh.name = "New Identifier";
                soundingHeaderBindingSource.Add(sh);
                db.SaveChanges();
            }

    And the call to SaveChanges() raises InvalidOperationException...

    " The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. "

    The database is (in my example) as simple as pie - that one table, no triggers, and the primary key is definitely flagged as the Identity with the framework designer.  If I add the record to 'db' directly, it works fine, but then my listbox won't update without recreating the BindingSource (which seems to totally defeat the object of having one afaics).

    I'm running VS2010 Beta1, which may be the cause, but skipped out 2008, so that's the only version I have with the entity framework.

    Any ideas? Cheers, Matthew.

  • 09-18-2009 6:35 AM In reply to

    • ejwipp
    • Not Ranked
    • Joined on 09-18-2009
    • Posts 1

    Re: Exception adding record to BindingSource if BS bound to list/combobox

     Hey Matthew,

     

    Were you able to get your problem figured out?  I'm experiencing the exact same thing and have not yet been able to find a solution. 

     

    Thanks,

     

    Eric

  • 09-18-2009 7:09 AM In reply to

    Re: Exception adding record to BindingSource if BS bound to list/combobox

    Sorry Eric, I haven't.  I've given up for the time being and am just adding it into the EntityCollection directly, which is rather a pain.  Bizarrely I have similar circumstances where it DOES work, but I can't for the life of me see what the difference is between the two.

    Hopefully with another person having the same issue someone with a bit more experience might pop up with the answer :-)

  • 09-18-2009 7:32 AM In reply to

    Re: Exception adding record to BindingSource if BS bound to list/combobox

    It's gotta be something to do with the query being generated, but I haven't been able to figure it out yet.  I can't get anyone with a reproducable test project to send me their files so I can debug it.

    If someone can though, e-mail to: robert at blackcastlesoft dot com

     

  • 09-18-2009 7:41 AM In reply to

    Re: Exception adding record to BindingSource if BS bound to list/combobox

    [copy of messsage sent to Robert by email, just for future reference]

     

    Hi Robert,

    If you have VS2010 beta, I've got a complete (and minimal) test project which exhibits the problem here:

    http://media.tanima.co.uk/dblistboxtest.zip

    I tried to install the free version of C# 2008 to see if that did likewise but the IDE didn't work (just got some error about not having the relevant permissions as soon as I try and create a project) - can only assume the presence of 2005 and 2010 beta is throwing it off.

    Cheers,
    Matthew.

  • 09-23-2009 11:16 AM In reply to

    • Eluim
    • Top 50 Contributor
    • Joined on 10-06-2008
    • Posts 28

    Re: Exception adding record to BindingSource if BS bound to list/combobox

     I cant load your project as I have 2008 VS but try to change this:

     
                context = new Entities(ebuilder.ToString());
                ObjectResult<SoundingHeaders> headers = context.SoundingHeaders.Execute(MergeOption.AppendOnly);
                soundingHeaderBindingSource.DataSource = header;

     use ObjectResult objects for DataBinding not ObjectQuery see Microsoft:"

    We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method. Binding in this manner prevents a query from being executed multiple times during binding.The following example binds a ComboBox to the ObjectResult that is returned from executing an ObjectQuery of type SalesOrderHeader:"

    http://msdn.microsoft.com/en-us/library/bb738469.aspx

    you can do this: soundingHeaderBindingSource.Add(sh); as you add the entity to the entitycollection.

     

    BUT try this too:

    context.AddtoSoundingHeaders(soundingHeaderObject);

    context.SaveChanges();

     

  • 02-10-2010 3:57 AM In reply to

    Re: Exception adding record to BindingSource if BS bound to list/combobox

     Hi,

     I had the same problem as you guys. After some research here are the conclusions:

     1) A BindingSource is not suitable for EntityModels

     2) When adding a new item by using AddNew() Method of the BindingSource, it creates a detached entity. When you call EndEdit() method it adds it to the Entity object. 

     Up untill here, everything is fine and normal.

      BUT !!!

    When you call SaveChanges() method, even with "false" argument, if you look inside the ObjectStateManager of the EntitySet you'll see that you have in the addedEntityStore your line freshly added, and in the unchangedEntityStore, the SAME line with the EntityState.Unchanged.. So the AcceptChanges() method fails because there is an obviuous conflict between the two lines that have the same Key. This only happens if you're using a BindingSource.

      MSDN however does not say anything about using BindingSources, but instead it talks about QueryObjects.

    Still if you want to use the BindingSource, call SaveChanges(false) method, and then simply reload the DataSource of the BindingSource...... Yeah, I know it stinks.... but what can man do ....against MS.

     Regards,

Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems