locked
How do I add an Entity from a Winforms details form? RRS feed

  • Question

  • Hi,

    Studying Julie Lerman's EF4 book and have encountered a simple scenario for which I cannot find a solution.

    I've dragged details of an Entity to a Windows Form. Along with a BindingSource, a BindingNavigator is added.

    I have no problem programmatically instantiating an Entity, providing values and saving it to the database. However, I can't figure out how to do this using the controls on the form itself.

    I'm handling BindingNavigatorAddNewItem.Click, wherein I instantiate a new Entity, provide a couple of auditing values such as the user who created it and the date created. I then add it to the ObjectSet:

      Dim newUnit As New Unit
    
    
    
      With newUnit
    
       .CreateUser = CurrentUser
    
       .CreateDate = Now
    
       .ModifyUser = CurrentUser
    
      End With
    
    
    
      context.Units.AddObject(newUnit)
    
    
    From here, I can't figure out what to do. If I provide all values in the form, then call context.SaveChanges, a System.Data.Update exception is thrown, reporting that I didn't provide a value for a required field. Can someone clue me in as to what to do, please? Julie explains how to do this in code but does not discuss how to add an entity from a form.
    Thanks
    Monday, August 15, 2011 12:08 AM

Answers

  • The problem was that both Ms. Lerman and the tutorial linked above would have us binding our DataGridView to the results of a query which are output to a List.

    While this method will result in a populated DataGridView, you will not be able to update or insert. Instead, bind your DataGridView directly to the to the results of the query (ie the ObjectQuery variable).


    • Marked as answer by vba-dev Monday, August 22, 2011 11:00 PM
    Monday, August 22, 2011 11:00 PM

All replies

  • Does anyone live here?
    Wednesday, August 17, 2011 2:28 AM
  • On 8/16/2011 10:28 PM, vba-dev wrote:
     
    Here man see if you can get this to work and then go back to your other
    tutorial and see if you see what went wrong.
     
     
    • Marked as answer by Larcolais Gong Monday, August 22, 2011 11:08 AM
    • Unmarked as answer by vba-dev Monday, August 22, 2011 10:57 PM
    Wednesday, August 17, 2011 2:55 AM
  • Any update? Was your question solved?

    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, August 18, 2011 9:57 AM
  • The problem was that both Ms. Lerman and the tutorial linked above would have us binding our DataGridView to the results of a query which are output to a List.

    While this method will result in a populated DataGridView, you will not be able to update or insert. Instead, bind your DataGridView directly to the to the results of the query (ie the ObjectQuery variable).


    • Marked as answer by vba-dev Monday, August 22, 2011 11:00 PM
    Monday, August 22, 2011 11:00 PM
  • darnold,

    Thanks very much for the link. I'll have a closer look.

    Monday, August 22, 2011 11:06 PM