Using EditableRootListBase without a grid and trapping errors

Using EditableRootListBase without a grid and trapping errors

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


frankhoffy posted on Friday, April 20, 2007

First off, I love the EditableRootListBase class.  It's exactly what I needed when dealing with grids.  The DataError event of the grid fires when a validation exception occurs, and I can trap the user in that grid row.  Perfect.

I also have a form with a binding navigator that is just a bunch of text boxes (Details versus DataGridView).  Since the row is auto-saving, I don't see a good way to trap the errors that happen on Save like I can with the grid.  I'd like to prevent the user from moving off of the current record if the data is not valid.  Does anybody have some suggestions for me?

RockfordLhotka replied on Friday, April 20, 2007

Have you looked at the ErrorProvider control?

frankhoffy replied on Friday, April 20, 2007

I did, and that works great as far as notifying what the errors are before the save occurs.  When moving from one row to the next though, the Save method will be called and will generate the exception "Object is invalid and can not be saved", which I suppose is OK.  However, it is an unhandled exception.  What I usually do in that case is loop through the broken rules collection to display a more useful error message.

The ErrorProvider control doesn't seem to give me the events I need when an error occurs, but I may be missing something.

RockfordLhotka replied on Friday, April 20, 2007

I think there is a validating event on the form itself you can handle. Otherwise look for events on the bindingsource control to see if there is a useful one there.

Rocky

-----Original Message-----
From: "frankhoffy"
To: "rocky@lhotka.net"
Sent: 4/20/2007 6:33 PM
Subject: Re: [CSLA .NET] Using EditableRootListBase without a grid and trapping errors

I did, and that works great as far as notifying what the errors are before the save occurs. When moving from one row to the next though, the Save method will be called and will generate the exception "Object is invalid and can not be saved", which I suppose is OK. However, it is an unhandled exception. What I usually do in that case is loop through the broken rules collection to display a more useful error message.

The ErrorProvider control doesn't seem to give me the events I need when an error occurs, but I may be missing something.


Devi replied on Tuesday, March 25, 2008

Which event that of BindingSourse, we can used for catching exception while saving through an EditableRootListBase ??

JohnB replied on Tuesday, March 25, 2008

What type of exception are you attempting to catch? You can override SaveItem() of the ERLB to insert your own exception handling code if you like.

John

Devi replied on Thursday, March 27, 2008

Hii.. John..

I tried Try-catch block in the RemoveItem() of ERLB to remove an item from the DataGridView..

But how can users view the Exception message ??

Regards..

Devi..

 

JohnB replied on Thursday, March 27, 2008

What exception are attempting to catch? General exceptions, Csla validation exceptions?

John

Devi replied on Thursday, March 27, 2008

I am trying to catch DataPortalException and sqlException while deletion.

Regards

Devi

JohnB replied on Friday, March 28, 2008

Devi,

I override SaveItem so that I can control when my object is saved. This is what I have in my ERLB.

Public Sub SaveMyItem(ByVal busObjTosave As MyBusinessObject)

    If Not Berth.CanWriteObject() Then
        Throw New SecurityException(String.Format("Cannot write object: {0}", "MyBusinessObject"))
    End If

    '-- Capture the objects index value within the collection which we will use to later save the object
    Dim pItemIndex As Int32 = Me.IndexOf(busObjTosave)


    '-- We need to clone the object before applying any edits so that we can recover the original object should the save fail for
    '-- any reason. General exception, validation or data portal exception.
    Dim pbusObjClone As Berth = busObjTosave.Clone

    '-- Apply any edits
    busObjTosave.ApplyEdit()

    '-- Attempt to save the object
    Try
        MyBase.SaveItem(pItemIndex)

    Catch ex As Csla.Validation.ValidationException
        Me.Item(pItemIndex) = DirectCast(ex.BusinessObject, MyBusinessObject)
        Throw ex

    Catch ex As Exception
        '-- Saved failed for some reason. Set the object back to the original clone and bubble up the exception
        Me.Item(pItemIndex) = pbusObjClone

        Throw ex
    End Try

End Sub

Public Overrides Sub SaveItem(ByVal index As Integer)
    '-- Do nothing
End Sub

Devi replied on Saturday, March 29, 2008

Hello,

I am just started a project in CSLA.net. I am new in CSLA. With ERLB, i am not using specific Event for save, edit or delete. Hence how can i call this method SaveMyItem() ? In wich event i can call this ?

And if threw the exception, the user cannot view the exception message. Can i show the message to the user ?

Regards

Devi

JohnB replied on Saturday, March 29, 2008

Devi,

I would recommend purchasing Rocky's books. They're invaluable for getting up to speed with the framework. What I did for ERLB was to change the default behavior of how it was meant to work.

John

phm replied on Saturday, March 29, 2008

JohnB,

At the end of your catch blocks, you should probably rethrow the exception with

Throw

instead of

Throw ex

The first one rethrows the exception with the original stack trace, the second recreates a stack trace corresponding to this instruction, thus loosing the place where the original exception occured.

JohnB replied on Saturday, March 29, 2008

Thanks!

Bayu replied on Saturday, April 21, 2007

Hi,

I would like to point you to a particular property on the Binding class: DataSourceUpdateMode. It is an enumerated value which you can also set to DataSourceUpdateMode.OnPropertyChanged. When set this will make your binding stay in sync as you type, as opposed to the default where the value is only copied when you leave the control. People often overlook it since it is not visible in the designer.

This is not a solution to your problem, I am aware of that. But perhaps it is one of the ingredients towards one. One thing that may help you is that validation occurs after every keystroke and thus that the ErrorProvider will appear/disappear as you type as well. You could also enable/disable the Next/Previous buttons by monitoring the IsSavable property as the user types.

Hope this helps.

Bayu


Jimbo replied on Sunday, April 22, 2007

Good call .. One bonus is also that you dont need to play around so much with  KeyPress events to do things like controlling  numeric input , triming  and Ucase etc when using this attribute. Users get a better experience when they dont have to leave the control for validation to fire. Another benefit is in (for example) a combo where  the index changed event will fire at a more useful time. Other wise it fires too soon if you are waiting for the binding validation event.
Make sure you have the BindingSourseRefreshControl extender to see the full benefits.

Jim

Copyright (c) Marimer LLC