This sample demonstrates how we can deal with these issues. This sample will show you how to handle errors with ActiveReports in a web environment. If there is an issue with ActiveReports, it will simply redirect the user to a custom error page that we can display IT contact information and also email the Errors to your support team or IT team without the user ever knowing.
To begin we ned to tell our website that we want to handle errors in a custom manner. This can be accomplished by setting the web.config file properly. In our System.Web tag in our Web.config we need to add the following entry:
<customErrors mode="On" defaultRedirect="~/CustomErrors/Oops.aspx" redirectMode="ResponseRewrite"> </customErrors>
Now that we have CustomError handling enabled and the link to the customer error page is setup lets move back to our Default.aspx page that holds the viewer for our ActiveReports Report. Lets go to the VB or CS code-behind for our Defualt.aspx page ( Default.aspx.cs ). In a basic Report viewing webpage, it would look like this
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim rpt As New UnboundDSInvoice() Me.WebViewer1.Report = New UnboundDSInvoice() End Sub
In this case, we create a new Object our our Report, Run the Report, and Bind it to the viewer. In order to handle any errors caused by this we need to add some Try, Catch, and Finally statements around that code. See below:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim rpt As New UnboundDSInvoice() Try rpt.Run() Catch ex As Exception Throw New Exception(ex.Message & "This is an unhandled exception FROM ME.") Finally Me.WebViewer1.Report = New UnboundDSInvoice() End Try End Sub
This way, any errors that are thrown by ActiveReports will be caught in ex as an exception and logged on the server. If an error is caught, the website will look into the web.config and redirect the user to the customError handler re-direct.
We can stop here if we only want to abstract the error away from the user. If we want to catch the error and display it on the new page or write the error to a file on disk or email a error report out, we need to add some code to the Oops.aspx.cs file ( the code behind for the Oops.asmx page ).
So basically what is going on here, is that we grab the LastError off the server and check to make sure it's not null. We return the string back to the Page_Load. This is where you as a developer can customize what you do with the error. In my code above, I insert the string into the page for debugging purposes, you can replace that code with a File.IO write or an email send function.