views:

32

answers:

1

Hi. I have a page like Order - Order lines. Order represents by some textboxes and ddls, Order lines represents by GridView.

I want to let users add order lines without save changes to database. For example: he adds 4 order lines, fill order info and then hits Save button. Only an that moment all information should be saved to DB.

When I use code like

using (Entities ctx = new Entities())
{
 //create new OrderLine
 OrderLine ol = OrderLine.CreateOrderLine(1, 1, "", 1);
 //add OrderLine to OrderLines collection
 ctx.CreateOrderLines.AddObject(ol);
}

newly created OrderLine does not appears in my object context, so I can't access it and bind GridView to new OrderList collection.

How can I solve this problem? Or maybe there are another ways to perform this task?

Thanks.

A: 

You could try to detach the Order object and work with it until you are ready to save it back to the database.

XSaint32
There is no problems with detaching. The problem is in GridView. How I must work with it when I need add, delete and update rows in it, but without instant save. All data should be saved to database at the end, when save button is clicked.
Smile
I have done something similar to this by creating a smaller version of the objects (with only the properties I need) and store them in a session. You don't want to store the entire GridView data in the session, but maybe a lightweight object. Then when the user commits, you could copy the values over to your ObjectContext and apply the changes.
XSaint32
I can't understand how should I work in this situation with GridView. Users should see that a newly added line is added in Grid too. Imagine: user adds 4 lines from my form => there are 4 postbacks => 4 new rows added to GridView. But at that moment no data saved to DB. Than user delete 1 line from this 4 => 1 postbask => 1 row has been deleted from GridView (still 3 there). Still no data saved. Now user decided to edit 1 line => again 1 postback => 1 row updated in GridView. And finally user clicks save button and all changed/created data saves to DB.
Smile
You would be databinding your GridView to the session object, which you would be updating with each add/edit/delete the user requests. In essence, you are using the ObjectContext the first time the page is requested. You would load your custom lightweight object from EF, and then use that during the remainder of the session with the user. If the user saves, you copy over your values back into EF. If the user does not save, then you simply discard the session object.
XSaint32
(Addition to my previous post) This was scenario when I create totally new Order. But can be another scenario: when I edit existing order. For example. User open order and there is already 3 lines. He adds 2 additional lines => 2 postbacks => 2 rows added to GV. No data saved. Then he deletes 1 line from first 3 => 1 postback => 1 row deleted. no data saved (in DB still 3 rows). Than user hits Save and all changes applies to DB. I don't ask the full solution, just some ideas. I haven't any ideas at all. Thanks.
Smile
XSaint32, it's all OK, but there is one problem. How can I use "include" in my query to combine orderlines with reseller (see my reply to another answer)? I need something like OrderList.Include("Reseller").ToList() but there is no relations (I think) cause data doesn't saved to DB.
Smile