Link to home
Create AccountLog in
Avatar of jaysch
jayschFlag for United States of America

asked on

Exception reporting - how to handle multiple errors

Hi,

I've built a console application that emails the stack error (ex) of any report that doesn't run on SSRS SQL Reporting Svc.

For example:

Try

....app code that runs and tests reports....


Catch ex as Exception

client.Send("info@nowheere.com", eAddresses, "Error In Report", ex.ToString)

end try

My problem is that there are over 200 reports and I want to report (email) the generated errors for all reports that don't run, not just the first bad report that is encountered.

How do I alter the error reporting so that the application continues to run through all 200 reports (good and bad). I'm only concerned about emailing the errors of the reports that don't run.

Thank you!





Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

trap the error by report code block, or inside the for e.g

For x = 0 to 10
    Try
       'runt your reports
    Catch Ex as Exception
       'do your error reporting
    End Try
Next
SOLUTION
Avatar of Den_HBR
Den_HBR

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of jaysch

ASKER

I've tried both solutions and basically the same process is repeated multiple times - it just stops at the first bad report and doesn't try to go on to the next report. I think what's missing here is a way to tell the main program to report the exception and then restart to the next report and keep going until all reports have run (or not run and reported errors). Again, I only need to include error messages.  In other words, how to you tell a program to keep executing even though an error has been thrown?
You will need to provide some code on your report generation then.
If ALL the routines to create a report are inside the try, then the code should work. You probably need to adjust the positioning on the try ... catch, thus please provide some more code.
Avatar of jaysch

ASKER

Here is the code that loads  in an xml file of report paths and report parameters. It attempts to run each report and send an email to a list of users for the reports that don't run. My problem is that this process stops at the first report that fails. I need execution to continue until all reports have either run or failed. The email that's sent should contain only errors of reports that failed to run.
Try 
            Dim xReports = XElement.Load(ReportPath)
            Dim OnErrorNotifyUsers = xReports.@OnErrorNotifyUsers 
            OutputPath = xReports.@OutputPath 
            DefaultReportParams = xReports.<DefaultParams>.Descendants("ReportParam") 

            Dim xDBName = DefaultParam("DBName") 
            xDBName.<Value>.Value = TestDbname
            Dim xDBServer = DefaultParam("DBServer") 
            xDBServer.<Value>.Value = TestDBServer

            Using service As ReportExecutionWS.ReportExecutionService = NewReportExecutionWS() 

                Dim historyId As String = Nothing 
                Dim mimeType As String = Nothing 
                Dim extension As String = Nothing 
                Dim encoding As String = Nothing 
                Dim warnings() As ReportExecutionWS.Warning = Nothing 
                Dim streamIds() As String = Nothing 

                Dim execHeader As New ReportExecutionWS.ExecutionHeader() 
                service.ExecutionHeaderValue = execHeader 

                Dim qRunReports = From r In xReports.Descendants("RunReport") _ 
                   Select r 

                For Each xRunReport In qRunReports 

                    FullReportPath = CodebaseRootReportPath + xRunReport.@ReportPath 
                    Dim ReportParams = xRunReport.Descendants("ReportParam") 
                    Dim execInfo As ReportExecutionWS.ExecutionInfo = service.LoadReport(FullReportPath, historyId) 

                    If ReportParams IsNot Nothing AndAlso 0 < ReportParams.Count Then 
                        Dim reportExecutionParameters(ReportParams.Count - 1) As ReportExecutionWS.ParameterValue 

                        For i As Integer = 0 To ReportParams.Count - 1 

                            Dim xReportParam = ReportParams(i) 
                            Dim rsExecutionParam As ReportExecutionWS.ParameterValue = New ReportExecutionWS.ParameterValue 
                            reportExecutionParameters(i) = rsExecutionParam 

                            rsExecutionParam.Name = xReportParam.<Name>.Value 
                            rsExecutionParam.Value = xReportParam.<Value>.Value 

                            If String.IsNullOrEmpty(rsExecutionParam.Value) Then 
                                xReportParam = DefaultParam(rsExecutionParam.Name) 
                                If xReportParam IsNot Nothing Then 
                                    rsExecutionParam.Value = xReportParam.<Value>.Value 

                                End If 
                            End If 
                        Next 

                        service.SetExecutionParameters(reportExecutionParameters, "en-us") 

                        Dim renderedReport As Byte() = Nothing 
                        renderedReport = service.Render(RenderFormat, GetDeviceInfo(RenderFormat), extension, mimeType, encoding, warnings, streamIds) 

                        Log.Output("Report completed OK : " & FullReportPath) 

                        If Not renderedReport Is Nothing Then 
                            File.WriteAllBytes(OutputPath, renderedReport) 
                        End If 
                    End If 
                Next 
            End Using 

        Catch ex As Exception 
            Log.OutputError(ex.ToString) 
            Dim client As New SmtpClient 
            client.Host = "smtp.nowhere.com" 
            Dim xdoc As New Xml.XPath.XPathDocument("C:ReportsTest\emailAddresses.xml") 
            Dim xnav As Xml.XPath.XPathNavigator = xdoc.CreateNavigator 
            Dim xni As Xml.XPath.XPathNodeIterator = xnav.Select("devemails/devname/*") 
            Dim eAddresses As String = "" 
            While xni.MoveNext 
                If xni.Current.Name = "email" Then 
                    eAddresses &= xni.Current.Value & "," 
                End If 
            End While 
            eAddresses = eAddresses.Trim.Trim(",").Trim() 
            client.Send("ReportAlerts@nowhere.com", eAddresses, "Error In Report @ " & Now().ToString, "Report: " & FullReportPath & vbCrLf & vbCrLf & "DB Server: " _ 
            & TestDBServer & " DB Name: " & TestDbname & vbCrLf & vbCrLf & "Error: " & ex.ToString) 
        End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
As codecruiser suggests, repositioning your try ...catch should trap the errors for each report.
Avatar of jaysch

ASKER

That iis what I needed. It now runs through all reports! Thanks!!

Right now, it's sending a separate email for each report that errors. Is it possible to add all errors to just one email and send it?
Looks like you did not understand many suggestions. Read the 34421161
I agree with codecruiser, read that in conjunction with the first response to your question.
Avatar of jaysch

ASKER

OK, looks like I need to do the sendmail just outside of the loop.
Avatar of jaysch

ASKER

Thanks again for the expert help, appreciate it!
Glad to help :-)