DetailsView, CSLADataSource & handling stored proc's RAISERROR

DetailsView, CSLADataSource & handling stored proc's RAISERROR

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


Manny posted on Monday, February 09, 2009

Hi,

I'm currently in the process of developing a CSLA Web application and I'm running into some issues with my DetailsView. I have an Insert stored proc that checks some things before inserting the Business Object. If the outcome of these checks is negative I throw an error in the stored procedure using the Raiserror keyword in SQL.

I have a Try catch in my CSLA DataSource's InsertObject method in which I catch the error thrown by the stored procedure and put it in a label on the page. The problem I'm getting is that my DetailsView jumps into ReadOnly mode and all the dropdowns on the DetailsView are emptied. Not really what I'd want.

My goal in the end would be to show the errormessage but have my DetailsView in Insert/Update mode and my dropdowns filled. How would I best go about doing this?

Cheers,

-Manny

RockfordLhotka replied on Monday, February 09, 2009

CSLA is designed around the idea that a failure at the data access layer will result in an exception flowing up to indicate failure.

In fact, the transactional models CSLA uses (from .NET) also rely on an exception so the transactions roll back properly.

So your DataPortal_XYZ method can catch the database exception, but in that case it must rethrow some exception so the transaction fails, and the data portal knows that the operation was not successful.

In CSLA 3.5 and higher your UI code will be left with the original object (unchanged) and the information in the exception about what went wrong. Typically this is enough information so the UI can inform the user about what went wrong.

Manny replied on Monday, February 09, 2009

Does that mean that if I were to put a Try Catch mechanism in an overridden DataPortal_Insert method (I generate my bases) and throw a new exception there, my DetailsView will still be in the state it was before it's trip to the database went wrong?

I'm using CSLA 3.5 by the way.

Manny replied on Monday, February 09, 2009

I managed to get some time to try my above wishful thinking, but that didn't really do much as I kind of expected already. Still getting a DTV in ReadOnly and empty Dropdowns :( Now that I think about it it's not even the case that you're suggesting a solution, but just pointing something out.

I am however well aware of the fact that I have enough information to let the user know what has gone wrong, that is not the issue I'm having. The issue I'm having is that my DetailsView has the error rendered above it but goes into ReadOnly state thus making the entire page useless.

RockfordLhotka replied on Monday, February 09, 2009

The state of the UI control is different from the state of the business object.

 

I’m just saying that the data portal expects an exception, and (in 3.5 and higher) will ensure your object is in a consistent state in this scenario. You may still need to have UI code that detects the exception and sets the UI controls to the correct state based on what happened.

 

Rocky

 

 

From: Manny [mailto:cslanet@lhotka.net]
Sent: Monday, February 09, 2009 1:28 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] DetailsView, CSLADataSource & handling stored proc's RAISERROR

 

Does that mean that if I were to put a Try Catch mechanism in an overridden DataPortal_Insert method (I generate my bases) and throw a new exception there, my DetailsView will still be in the state it was before it's trip to the database went wrong?

DutchDiesel replied on Tuesday, February 10, 2009

The problem in this situation is the detailsview itself.
When an error occurs in the insert of a datasource the detailsview wil be rendered again.

All the filled in textboxes and chosen dropdownlist values wil become empty.

To prevent this from happening the ItemInserted event of the detailsview must be handled in the following way:

Protected Sub dtv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dtv.ItemInserted
   If e.Exception IsNot Nothing Then
      'Error handling 
      e.ExceptionHandled = True
      e.KeepInInsertMode = True
   End If
End Sub

When the ExceptionHandled and the KeepInInsertMode is set to true, the detailsview will remain in the state it was before inserting.

DutchDiesel

Manny replied on Thursday, February 12, 2009

Brilliant! That fixed it for me :D

Copyright (c) Marimer LLC