User's Data is lost when insertion fails in Formview

User's Data is lost when insertion fails in Formview

Old forum URL: forums.lhotka.net/forums/t/3026.aspx


Moe posted on Tuesday, June 12, 2007

I would like to share this with everyone because I had a very hard time finding answers and please feel free to correct me if I am wrong.

I was stock for two days trying to figure out how to handle a Formview view state when insertion fails. Every time my insert fails, I would lose all the user's data.

The only way I was able to retain this data was to move the code form the CSLA data source InsertObject event handler to the FormView ItemInserting event handler and then I set e.cancel = true if the effected rows returned from the Save() helper method is 0.

Protected Sub fvProjectInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvProjectInsert.ItemInserting

Dim RowEffected As Integer = 0

Try

Dim obj As ProjectBO

obj = ProjectBO.NewProjectBO()

Csla.Data.DataMapper.Map(e.Values)

RowEffected = SaveProject(obj)

If RowEffected = 0 Then

   e.Cancel = True

End If

Catch ex As Data.SqlClient.SqlException

Me.lblError.Text = ex.Message

e.Cancel = True

Catch ex As Csla.DataPortalException

Me.lblError.Text = ex.BusinessException.Message

e.Cancel = True

Catch ex As Exception

Me.lblError.Text = ex.Message

e.Cancel = True

End Try

End Sub

 

av_harris replied on Tuesday, July 10, 2007

I can hardly believe that this is the optimal solution. I'm having the same situation when my code catches a rules exception and I want to display the original data in my formview.

Has anyone else encountered this?

Skafa replied on Tuesday, July 10, 2007

I worked around it by using EditTemplate instead of InsertTemplate for new objects...

av_harris replied on Wednesday, July 11, 2007

Did you just replace the code in the InsertTemplate on the form or did you change the initial mode to edit? Or both?

When I changed the initial mode to edit, I got a blank screen. Obviously, the code in UpdateObject needs to be changed too. Anybody got a sample? Where I'm a little lost is how/why the datasource gets fooled by the change in mode. I see that the "insert" or "update" buttons are how the events get started, but I don't think I really understand what needs to be done (differently) in the UpdateObject so that the initial insert can be performed.

All this being said, has no progress been made on this issue since last year. I've seen one extensive thread discussing this a year ago and, since it is such a vital part of data entry, I would have thought some changes would have been made. Anybody know of anything?

Moe replied on Wednesday, July 11, 2007

first you need to make sure that you have writing some UI code in your <EditItemTemplate> and <InsertItemTemplate> Not only <ItemTemplate>. in your buttons you should only make the calls for the form view to change from one template to another. Now, in your ItemInserting event handler you would place the code that you would normally write in the CSLA Data Source InsertObject event handler. Just look at the example I have posted at the top of this thread and that should give an idea. If you still need help, let me know I will to explane more. Good Luck.

av_harris replied on Thursday, July 12, 2007

Moe,

Thanks for clearing up some of my confusion. I created the ItemInserting and it works as advertised.

My only problem now is the code doesn't know what to do once the save is successful! Ideally, I want the saved record displayed in formview readonly mode. I guess my question is: what code do I use to re-display the form and where should I place it?

Moe replied on Friday, July 13, 2007

av_harris,

I don't know how you are switching between your templates but what I have done is as fallows,

In my <ItemTemplate> which is my read only view, I have three buttons

<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Edit"Text="Edit" />

<asp:Button ID="Button3" runat="server" CausesValidation="False" Text="New" CommandName="New" />

<asp:Button ID="Button4" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClick="DeleteButton_Click" OnClientClick="return confirm('Are you certain you want to delete this project?');" />

In <EditItemTemplate>

<asp:Button ID="UpdateButton2" runat="server" Text="Save" CommandName="Update" />

<asp:Button ID="UpdateCancelButton2" runat="server" Text="Cancel" CommandName="Cancel" OnClick="UpdateCancelButton_Click" />

In <InsertItemTemplate>

<asp:Button ID="InsertTempBtnInsert" runat="server" Text="Insert" CommandName="Insert" />

<asp:Button ID="InsertTempBtnCancel" runat="server" Text="Cancel" CommandName="Cancel" />

Whatever "CommandName" you assign to your button, your Form view should handle by default without having to write any server side code.

Now when your save is sucessful (don't set e.cancel = True) the form view will automatically switch back to the default mode (which is by default <ItemTemplate>). if you set your default mode to <EditItemTemplate> or whatever then you need to explicitly call the fallowing method inside your ItemUpdating event handler and below your update method call,

FormView1.ChangeMode(FormViewMode.ReadOnly)

I hope this answers your question!

Good Luck,

Moe

av_harris replied on Sunday, July 15, 2007

Moe,

Thanks for all the help. I think I have it under control now. As usual, it is the combination of something new (CSLA) and something incredibly convoluted (.Net events!) that makes implementation of any solution more difficult than it needs to be.

Copyright (c) Marimer LLC