Asp – How to use DataBound controls with a disconnected architechture in ASP.NET

asp.netdata-bindinglinq

When I'm developing ASP.NET applications I often create forms that allow users to create, retrieve, or update records in a database. Many times there's a need to be able to add child records to an item I am creating, such as adding a category to a product or some sort of order / order detail relationship. Normally I use some sort of DropDownList to show a list of possible child records, and then a DataGrid to show which children are already a part of the parent. Currently, the only way I know to accomplish this in ASP.NET requires that I first save the parent record to the DB so that I can assign PK/FK relationships so I am able to DataBind the DataGrids that hold the child records.

What I'd like to know is if there is some way I could implement the form so that I could add or remove records from these DataGrids but not actually commit the changes until the user clicks 'Save' to create or update all the required records at once. I'm starting to migrate from using SubSonic to LINQ if that would work better for the DataBinding. It seems pretty difficult with the statelessness of HTTP.

Any thoughts are greatly appreciated,

Mike

Best Answer

You could use a cached DataSet for this. They're pretty much made to be used as in-memory representations of table-based relational data. I actually really don't recommend this approach for a variety of reasons, but you can cache your DataSet on the server, allow the user to interact with it (adding, editing, deleting and so forth) through postbacks, and then only persist the changes to the real underlying database at the end.

I think a better way of doing this, however, is to make all the changes to the underlying database as the user enters them within a transaction, and then only commit the transaction at the end.