This is a migrated thread and some comments may be shown as answers.

RadGrid + ObjectContainerDataSource Error Handling

1 Answer 177 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zac
Top achievements
Rank 1
Zac asked on 20 Mar 2009, 04:38 PM
I have a RadGrid setup using automatic insert/update with an ObjectContainerDataSource.  In my page's code-behind, I'm handling the data source's OnInserted and OnUpdated events and calling methods in my presenter (MVP pattern). 

The question is this - suppose the presenter detects some business logic violation while it's running OnInserted.  How exactly should this be handled so that the grid can display an error message?  I'd like the grid to remain in edit mode and for the data the user entered to remain on the screen.  I'm using popup edit mode, but I don't think it matters much.  I just want to display an appropriate error message and have the user correct their mistake and resubmit.

The core of my confusion is that I don't see how to get from (1) a presenter throwing an exception via the ObjectContainerDataSource.OnInserted() event handler to (2) a way to handle this exception in a meaningful way in the RadGrid.ItemInserted event handler.  Maybe some code will clarify.

protected void Page_Init(object sender, EventArgs e)
        {
            Grid.ItemInserted += new GridInsertedEventHandler(Grid_ItemInserted);
            UsersDataSource.Inserted += new EventHandler<ObjectContainerDataSourceStatusEventArgs>(UsersDataSource_Inserted);
        }

        void UsersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {            
            try
            {
                // This might throw a business exception.
                UserPresenter.OnUserInserted((UserDTO)e.Instance);
            }
            catch (Exception ex)
            {
                // If I suppress, the grid will never know about the exception.  If I rethrow, Grid_ItemInserted will never get called.
               // I really want to "bubble" the exception up to Grid_ItemInserted() so it can deal with it in a meaningful way.
            }
        }


        void Grid_ItemInserted(object source, GridInsertedEventArgs e)
        {

            // Handle errors.
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                DisplayMessage("User cannot be created. Reason: " + e.Exception.Message);
            }
            else
            {
                DisplayMessage("User created.");
            }
        }

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 24 Mar 2009, 04:22 PM
Hello Zac,

Unfortunately I could not reproduce this issue. I created a test project in my attempt to recreate the depicted abnormality. After I re-throw the exception the ItemInserted event is raised and I am able to catch the exception.

Actually the ObjectContainerDataSource sets the exception to the DataSourceViewOperationCallback. This callback is used by our event GridInsertedEventArgs to populate e.Exception.

Please examine the attached test project and let me know if I am missing something in your logic.

Greetings,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Zac
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or