GridView does not see the object that was just added to an EntitySet RRS feed

  • Question

  • Hello,


    I am trying to add a new object to an existing EntitySet with a given ObjectContext and bind its values to a GridView without saving it to the database first. However, the GridView does not "see" the object as being part of the EntitySet until I do a SaveChanges() on it, which is exactly what I am trying to avoid. My goal is to create the object in memory, display it to the user, and upon the user's review persist it in the database in a Button_Click event. Can this be accomplished?


    Below is the code I'm using:



       ValuationPOCEntities summaryEntities = new ValuationPOCEntities(); //create Object Context
       Summary newSummary = generateObjects(); //create an in-memory entity
       summaryEntities.AddToSummarySet(newSummary); //add it to entitySet
       GridView1.DataSource = SummaryDataSource; //specify an EntityDatasource that is configured to use the given Object Context
       GridView1.DataBind(); //bind


    The GridView bound in the way described above returns no rows. However, if I include the step of SaveChanges() after I add the object to the set then the GridView displays just fine. So does this mean that SaveChanges() is necessary in this scenario, or is there a way to accomplish what I am trying to do -- bind a GridView to an in-memory entity?


    Thank you in advance


    • Edited by yasvintus Wednesday, June 8, 2011 12:44 PM
    Tuesday, June 7, 2011 10:07 PM


All replies

  • Hi!

    The code you are posting looks fine, but I'm missing some information. Can you post the code for the summaryEntities EntitySet and how you bind summaryEntities to SummaryDataSource?

    Also, if you haven't already done it, consider using the EntityDataSource instead of a ObjectDataSource, it may suit you better.

    Wednesday, June 8, 2011 5:55 AM
  • Hi Rune and thank you for your response. I misspoke -- SummaryDataSource is indeed an EntityDatasource. I've edited my original post to correct it.


    As to your other questions, I did it all through point-and-click configuration rather than writing code. When I dropped an EntityDataSource to my form I simply selected ValuationPOCEntities (which my summaryEntities object is a type of) as my DefaultContainerName, SummarySet as the EntitySetName and that was it.


    Also, in the code above summaryEntities is an ObjectContext, not an EntitySet. I also have an EntitySet called summarySet as you can see from the name of the method I use to add the new object to the set , but then again -- I had Visual Studio generate all these objects for me.


    Here is the EntityDataSource tag, if that helps:

      <asp:EntityDataSource ID="SummaryDataSource" runat="server" 
        DefaultContainerName="ValuationPOCEntities" EnableDelete="True" 
        EnableInsert="True" EnableUpdate="True" EntitySetName="SummarySet">

    Looking forward to further suggestions.


    Thanks again.

    Wednesday, June 8, 2011 1:12 PM
  • Hi,


    I suspect that the reason this isn't working is that your code and the EntityDataSource is working with different ObjectContexts, so changes you're doing in your own DataContext isn't reflected into the EntityDataSource before you store the data to the database so that the EntityDataSource can retrieve it on a refresh.

    Unfortunatly, I don't have that experience with EntityDataSource to give you an answer myself on how to fix this, but after a search, I found this post that may help you further on how to add a new record to your entitydatasource.


    Hope this helps!

    Wednesday, June 8, 2011 6:31 PM
  • Thanks again Rune. Your theory about different ObjectContexts makes perfect sense. Now if I only could figure out a way to reconcile them...


    As to the link you posted (thank you for doing the research!) -- I don't think it applies to my situation. It's my understanding that the AddObject method (or AddToSummarySet in my case) perform the role of the insert. Is that not the case?


    I will do some research following up on your suggestion and in the meantime look forward for more pointers.


    Thank you.


    Thursday, June 9, 2011 3:33 AM
  • You're right the AddObject/AddToSummarySet are doing the job for you if you should add a row to the database only.

    But since you want the row to be visible in your DataGrid first, I think you should do it as the link I gave states. Since your EntityDataSource has EnableInsert set to true I suspect it does the work you have in your code-behind automatically. I would have given it a try to see how it worked :)

    Thursday, June 9, 2011 5:22 AM
  • Hmm... Still can't get it to work. I implemented the extension class and am able to call the Insert() method. It's not working for me for two reasons:


    1. Seems that Insert() does persist the new object in the database, which, again, is something I am trying to avoid.

    2. The DetailsView bound to the EntityDataSource in which I am doing the insert still does not show the new record unless I re-databind it which at that point pulls the records from the database (I am monitoring SQL activity via the Profiler).


    I'll probably give up at this point. I guess I can just wrap my new object into a List of some sort and bind it to the DetailsView this way. I will then only add the new entity to the EntitySet upon user's confirmation that all the object properties are correct.


    I will still welcome any suggestions that would solve my problem more elegantly.


    Once again -- thank you Rune for all your help.

    Thursday, June 9, 2011 1:08 PM
  • Hi yasvintus,

    I think you can use ObjectContext.ObjectStateManager Class  to achieve it. EF enables you to execute custom business logic before changes are saved to the database. Please check this document about How to: Execute Business Logic When Saving Changes.

    Hope this helps.

    Best Regards,

    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.

    Friday, June 10, 2011 10:04 AM
  • Hi Larcolais,


    Thank you for your response. Your suggestion seems like a step in the right direction and I was successfully able to capture the SavingChanges() event. However, I'm still not clear on how using that method I would prevent the entry from being written to the database, but still have it show up in the EntityDataSource.


    Can you please elaborate just a little bit more?


    Thank you!


    Friday, June 10, 2011 5:14 PM
  • Hi Yasvintus,

    I think you can detach the added entities first, the savechanges will ignore them. In EF4.1 there is Local method to show the added entities, you can refer here: and

    have  a nice day.

    Alan Chen[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.

    Monday, June 20, 2011 1:02 PM
  • Hi Yasvintus,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.

    Alan Chen[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, June 30, 2011 6:28 AM
  • Thank you Alan and sorry for the delay in my response. The project got postponed, so for the past few weeks I have not been actively involved with it. The Local method seems to be EXACTLY what I was looking for. I will install the new EF release and give it a shot.

    Thursday, June 30, 2011 12:35 PM