Handling RowUpdated event to revert to OldValues

Posts   
 
    
M6rk
User
Posts: 37
Joined: 29-Sep-2004
# Posted on: 30-Jan-2007 20:39:08   

Greetings!

I am using GridView bound to the very sweet LLBLGenProDataSource.

I am handling the RowUpdated event in order to handle errors.

The GridViewUpdated event handler's GridViewUpdatedEventArgs argument contains an OldValues and NewValues.

If an exception occurs during the update, i would like to revert to the OldValues. In practice, the new values are sticking. (KeepInEditMode is not much help, because clicking cancel leaves the gridview with values that have not been saved).

Anyone know how to do this?

Thank you!

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 30-Jan-2007 22:14:15   

just to confirm:

  • edit row index > 0
  • user alters values
  • user clicks save
  • validation fails
  • invalid values are presisted to the db
  • grid is reloaded with invalid data this this correct?

where is the validation occuring? within the entity, or though asp.net vaidation controls?

if asp.net validation controls then you can call onUpdating.

if (!page.IsValid) e.Cancel = true;

this will prevent the datasource from firing the PreformWork event. If setting Cancel = true resets the EditIndex try this

if (!page.IsValid)
{
       e.Cancel = true;
       myGrid.EditIndex = e.RowIndex;
}
M6rk
User
Posts: 37
Joined: 29-Sep-2004
# Posted on: 30-Jan-2007 22:33:30   

[quote]

jmeckley wrote:

just to confirm:

  • edit row index > 0
  • user alters values
  • user clicks save
  • validation fails
  • invalid values are presisted to the db
  • grid is reloaded with invalid data this this correct?

No. The data is not persisted to the DB, it is persisted to the grid. The DB is correctly throwing the error through LLBLGen entity layer when the update or delete occurs. (in my test case - i am updating value to intentionally cause a unique contraint violation, or foreign key violation in case of delete).

Once an error occurs, i handle it in RowUpdated and RowDeleted events. This seems to be best practice. However, As I page back and forth, the invalid value (in the case of the RowUpdated event) hangs around in the table cell (gridview). I would like to poke the original values back in so the DataGrid reflects the current state of the DB.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-Jan-2007 07:05:33   

I think you should either re-set the field manually. Or refetch data from the DB:

LLBLGenProDataSource2_1.Refetch = true;
GridView1.DataBind();
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 31-Jan-2007 09:56:06   

Doesn't the grid have a facility to obtain the actual data element which is bound to the row? (don't remember the fine print here, but I think it was possible) In this case this would be the entity edited, which thus can be re-set by setting the old values back. You also could implement validator objects on the entity and validate in there, which could be more helpful as you in there could simply reject a value which isn't correct so you don't have to revert to old values. See field validation and entity validation in the using the generated code section in the manual.

Frans Bouma | Lead developer LLBLGen Pro
M6rk
User
Posts: 37
Joined: 29-Sep-2004
# Posted on: 31-Jan-2007 19:14:15   

That worked, thank you!

Walaa wrote:

I think you should either re-set the field manually. Or refetch data from the DB:

LLBLGenProDataSource2_1.Refetch = true;
GridView1.DataBind();