none
problems add new record on entity framework 4.1 RRS feed

  • Question

  • Hello !
    i have a sql server 2008r2 database ( stu) with a table (st) with these columns :
    stID - identity (int autoincrement value)
    name - char(10)
    com - char(20)
     
    i have a project in vb.net 2010 , and i have generated entity framework model with the database.
    I have a form with a listbox and 2 textbox , and 2 buttons Addrecord and Saverecord.
    The listbox is bound with an object from entity (stBindingSource) and displayMember: name , Valuememeber : stID. The form is also binded with stbindingsource. ( textbox1:name , Textbox2: com)
    I want to add new record.The code is :
    ……-----------------------------------
    Dim stu As stuEntities
     
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    stu= New stuEntities
     
    stBindingSource.DataSource = stu.sts
    End Sub
    ---------------------------------------------
    Private Sub Addrecord_Click(sender As System.Object, e As System.EventArgs) Handles Addrecord.Click
    stBindingSource.AddNew()
    End Sub
    -------------------------------------------------
    Private Sub Saverecord_Click(sender As System.Object, e As System.EventArgs) Handles Saverecord.Click
    stBindingSource.EndEdit
    stu.SaveChanges(System.Data.Objects.SaveOptions.None)
     
    End Sub
     
    So ,first i click to "Addrecord" , and after i fill the 2 textbox , i click Saverecord.
    The first record is saved with no problem.If a add another record , the record is saved , but when I open the table on database , the first record is saved 2 times , the second record is saved 1 time.
    I try to add 3,4… records and everytime the last record is saved 1 time the others are all duplicated
     
    If I call stu.SaveChanges() , with no parametres , I can’t add any records because I get an error :
    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.
    Please help me.
     
    Thank you.
    Tuesday, August 30, 2011 1:28 PM

All replies

  • Hello,

    IMO, I suggest you use SQL Server Profile if it inserted duplicated data into database.

    Please feel free to let us know how it goes.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, September 1, 2011 6:47 AM
  • it's strange .

    i have described in my post that i have listbox and form both bounded with stbindingsource. if i remove binding from listbox , the problem is resolved. if both are bounded i have the problem with duplicates record .

    What can i do ?

    Thursday, September 1, 2011 11:40 AM
  • first i think we should check this question in the SQL Server profiler. i redemo your question on my side. but i think this wasn't a bug for sql server or others. i suggest you check this link about AddNew method in VB. i think you will find helpful here. http://msdn.microsoft.com/en-us/library/ms677536(v=VS.85).aspx

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Monday, September 5, 2011 2:31 AM
  • Any update? Was your question solved?

    Please feel free to let us know if you need further support.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 7, 2011 3:13 AM
  • The addnew method work ok , but the problem is that when both form and listbox inside are binded with bindingsource i have problem , when only form is binded , no problem .
    i have described my code , if someone can suggest to me what can i change ?

     

    Wednesday, September 7, 2011 10:41 PM