SelectObject event and null BusinessObject?

SelectObject event and null BusinessObject?

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


jas_nhs posted on Thursday, July 13, 2006

I am doing some evaluation of the csladatasource (acknowledging its advantages over objectdatasource) and have a simple page with a csladatasource associated with a gridview, set up in design view etc all as normal. Being new to CSLA I may be missing something, but, if, in

CslaDataSource1_SelectObject

I call a method like

MyThings.GetAll

and it returns data, then all is fine.
BUT, if GetAll throws an exception (e.g. because CanGetObject is false) I do catch the exception but it is then (is it?) too late to prevent the resultant page error:

Value cannot be null.
Parameter name: container


which is presumably due to the absence of a datasource for the grid
.
What to do? Maybe I should not be in this page if I am not privileged to read the data, but the test (CanGetObject) is in the GetAll call and thus the exception is a possible outcome of the call.
I would like to gracefully display to the user the actual text of the exception ("User not authorized...") rather than diverting to some generic error page, but I can't see how.

TIA,

James.


RockfordLhotka replied on Thursday, July 13, 2006

But you know you are going to call GetAll() right? So in your page Load event, can't you call CanGetObject() and maybe set the grid control to Visible=false? If you do that it shouldn't bind.

Then, assuming your grid control is in a div along with a normally invisible label, you can turn that label to Visible=true and set some text to tell the user why the grid is missing.

jas_nhs replied on Friday, July 14, 2006

Thanks for your quick reply to my late-night problem (brain defrag overdue) - your suggestion certainly works; another idea which I tried is, in the catch block, to create a default MyThing (singular) object  then set the CslaDataSource.TypeName to the MyThing type - and set e.BusinessObject to this object. Not particularly elegant, and also the catch block is not the place for page-presentation logic.  The big advantage is that the code survives any other unpredicted exception arising in GetAll without branching off to a generic error page.

However, I do have an aversion to setting the binding in the aspx file, and I might prefer to set TypeName and DataSourceID in code - I notice that the SelectObject event does not fire unless the data consumer (gridview) needs it (e.g. when you make the gridview visible), so you can make sure that when it does, data is supplyable.

Thanks for your willingness to help!

James.


Copyright (c) Marimer LLC