Saving Entity from DataGrid on Update

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 15-Dec-2005 23:35:43   

So here I am in a stateless environment where the user is editing a single row in my DataGrid. I have bound my DataGrid to an EntityCollection, but by the time the web page is rendered to the screen, that EntityCollection has since disappeared.

When they click the "Update" link in the grid, obviously my UpdateCommand event fires in the grid and I have a DataGridCommandEventArgs argument in this method to deal with.

What do you all do in this case to save your changes to an entity?

I could call a method in my manager class called SaveMyEntity(MyEntity ent) and let it do the work, BUT that means that my presentation layer has to create an instance of MyEntity, fill up the columns that I want to change, and call the SaveMyEntity method in the BLL. This then has to fetch the persisted data, loop through the ent instance passed in as a parameter, set the values, and then persist that to the database.

OR

I could call a method in my manager class that is SaveEntity(string col1Value, string Col2Value, ... string ColnValue)

which would fetch the persisted data into a local entity, assign the values and save that data, BUT what if I add a column to the underlying table? I then have to make sure I go add a parameter to this save method.

OR

I could pass in an Hashtable with my column value pairs and loop through this inside of the BLL method and assign the values. That way, all my presentation layer has to do is send in the column value pairs that changed.

....what am I missing? These steps seem a bit involved. Am I missing a whole piece to this? Here are my assumptions about the datagrid:

1) Once rendered to the page, it loses all connections to the datasource that built it. 2) The only way to get at data on the screen is to interrogate the widgets that contain the displayed data (ie, textbox.Text and droplist.selectedValue) 3) I basically have to "start over" again when they click update because I have to go fetch my persisted data back into entities in order to operate against them.

...please help me understand if I'm missing some key element here.

P.S. I'm accostomed to dealing with a client/server environment so this whole stateless world is still strange and unhelpful to me.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 16-Dec-2005 03:16:49   

I take the information from the row and load the entity then update it and save. You can do this by going through your BL, but I use the entities in my presentation layer for my application. When I do an insert it is through fields in the footer so I can tell when the item is going to perform an update on existing entity or an insert for a new entity.

NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 16-Dec-2005 20:34:58   

bclubb wrote:

I take the information from the row and load the entity then update it and save. You can do this by going through your BL, but I use the entities in my presentation layer for my application. When I do an insert it is through fields in the footer so I can tell when the item is going to perform an update on existing entity or an insert for a new entity.

Thanks for your reply. I guess part of what I'm hoping to get from this post is A) confirmation that I am understanding my problem correctly and B) to see how others are handling this.

So, make me feel good by affirming the following:

1) Even though during a page init I set the data source of a data grid (or now gridView simple_smile ) to an EntityCollection, that once the user subsequently takes action against a row, my only option for get the contents of the affected row is to loop through the cells and grab the text of the widget.

2) If the above is true, then I must also instantiate any Entities I want to deal with.

...and if those assumptions are correct then

3) Knowing that filling up my Session space creates the potential for uncontrollable memory use, are there "tricks" that you use to store this information outside of the Session space? If not, why are you comfortable storing user variable data in the Session space?

NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 20-Dec-2005 16:50:09   

Anybody got any helpful hints on how to do what I'm after?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Dec-2005 07:16:13   

in a web scenario i tend to do the following:

1- Load datagrids with EntityCollection or better with a datatable returned via TypedList 2- Save updates done to the datagrid, row at a time. 3- You don't need to fetch persistant data into an entity before updatig it, just create an object fill it with the PK & the new data, don't forget to set IsNew=false, then call SaveEntity()

4- Or if you want to save all the updates done on the datagrid rows once, then you should keep track on which rows has changed, then upon save, create an entity collection with the changed rows and save it.

NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 21-Dec-2005 16:04:52   

Ok, thanks. I'll take this approach.