cancel
Showing results for 
Search instead for 
Did you mean: 

[Crystal Report C# API] How to get exception thrown from Stored Procedure called from Crystal report

Former Member
0 Kudos

Hi,

I am having troubles trying to retrieve all the exception messages from C# API ReportDocument.ExportToDisk.

Under certain conditions Stored Procedure which is the source of the data for the report throws an exception (MSSQL RAISERROR). When running from Crystal Reports 2008 application several messages are popping up in the following order:

1. Failed to retrieve data from the database.

2. Failed to retrieve data from the database.

3. Database Connector Error: '42000: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server] [TEXT OF THE EXCEPTION FROM STORED PROCEDURE]'

But when running form C# API by calling ReportDocument.ExportToDisk exception is thrown only for the first message (Failed to retrieve data from the database.) and execution stops. Inner exception of this exception does not contain any additional information.

What I need is to get the text of the message from Stored Procedure which is the 3rd message when running from Crystal Report 2008 application.

Crystal Runtime 12.7.3.2302.(2008)

Windows 10

.Net 4.7

MS SQL

Any advise on the topic is really appreciated.

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Answers (6)

Answers (6)

Former Member
0 Kudos

Don,

I am aware of Inner Exception and we actually use it. The problem is it is not informative enough. I created the request for improvement.

https://influence.sap.com/sap/ino/#/idea/220911

Thanks a lot for your help.

0 Kudos

You can use a try/catch around your code andf catch the inner exception now, here's an example:

private void btnOpenReportInViewerOnly_Click(object sender, EventArgs e)
{
    openFileDialog.Filter = "Crystal Reports (*.rpt)|*.rpt|Crystal Reports Secure (*.rptr)|*.rptr";
    //openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;

    DateTime dtStart;
    TimeSpan difference;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        btnOpenReport.Enabled = false;
        btnSaveRptAs.Enabled = false;
        btnCloserpt.Enabled = false;
        object rptName = openFileDialog.FileName;

        dtStart = DateTime.Now;

        try
        {
            try
            {
                if (cbP2P.Checked)
                    crystalReportViewer1.PrintMode = PrintMode.PrintToPrinter;
                else
                    crystalReportViewer1.PrintMode = PrintMode.PrintOutputController;
                crystalReportViewer1.ReportSource = rptName;
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString() == "Object reference not set to an instance of an object.")
                    MessageBox.Show("ERROR: Object reference not set to an instance of an object.");
                else
                    if (ex.Message.ToString() == "External component has thrown an exception.")
                    {
                        MessageBox.Show("ERROR: External component has thrown an exception.");
                    }
                    else
                    {
                        {
                            if (ex.InnerException.Message != null)
                            {
                                MessageBox.Show("ERROR: " + ex.Message + " ;" + ex.InnerException.Message);

                                ////cool but of no use to release mode
                                //string myURL = @"http://search.sap.com/ui/scn#query=" + ex.InnerException.Message + "☆tindex=1&filter=scm_a_site%28scm_v_Site11%29&filter=scm_a_modDate%28*%29&timeScope=all";
                                //string fixedString = myURL.Replace(" ", "%20");

                                ////System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe", myURL);
                                //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", fixedString);

                                ////string myURL = @"C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports 2011\Help\en\crw.chm";
                                ////System.Diagnostics.Process.Start(myURL);
                                ////cool but of no use to release mode

                            }
                            else
                            {
                                MessageBox.Show("ERROR: " + ex.Message);
                            }
                        }
                    }
Former Member
0 Kudos

Don, big thanks for the suggestion.

The thing is using CrystalReportViewer is not an option for us due to the system has a feature of saving reports to a file with later observation. So it takes to save a report to file or obtain all the exception messages. Could I send somehow a request for enhancement of ReportDocument.ExportToDisk method to return all the exceptions with inner exception property.

Thank you.

0 Kudos

In the Viewer Error Event Trigger, not sure if CR 2008 had this one, but see if this catches it for you.

We don't have a Export event trigger, but Idea Place you may be able to request one:

private void crystalReportViewer1_Error(object source, ExceptionEventArgs e)
{
    string eError = "";
    int StringLen = 0;
    eError = e.Exception.Message.ToString();
    StringLen = eError.Length;
    if (StringLen >= 61)
    {
        if (eError.Substring(0, 61) == @"The remaining text does not appear to be part of the formula.")
        {
            MessageBox.Show("Possible missing UFL or actual error in formula");
            eError = eError.Substring(0, StringLen - 2);
            ////cool but of no use to release mode unless you have your own help file
            string myURL = @"http://search.sap.com/ui/scn#query=" + eError + "☆tindex=1&filter=scm_a_resourceType(scm_v_resType252)";
            string fixedString = myURL.Replace(" ", "%20");

            //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe", myURL);
            System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", fixedString);
            e.Handled = true;
        }
        else
        {
            MessageBox.Show("Trigger Event - Error in Viewer: " + e.ToString());
        }
    }
    else
    {
        if (eError.ToString() == "The system cannot find the path specified.\r")
        {
            MessageBox.Show(eError);
            eError = eError.Substring(0, StringLen - 2);
            ////cool but of no use to release mode unless you have your own help file
            string myURL = @"http://search.sap.com/ui/scn#query=" + eError + "☆tindex=1&filter=scm_a_resourceType(scm_v_resType252)";
            string fixedString = myURL.Replace(" ", "%20");

            //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe", myURL);
            //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", fixedString);

            ////string myURL = @"C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports 2011\Help\en\crw.chm";
            ////System.Diagnostics.Process.Start(myURL);
            ////cool but of no use to release mode
        }
        else
            if (eError.ToString() == "Object reference not set to an instance of an object.")
            {
                MessageBox.Show(eError);
                eError = eError.Substring(0, StringLen - 2);
            }

        //else
        //    MessageBox.Show("Break on this line to get the actaul error being trapped");

        e.Handled = true;
    }
}
Former Member
0 Kudos

Dell, thanks for your answer.

Don, please help on the matter. Could it be that it is a problem of Crystal Runtime 2008 and upgrading might help.

DellSC
Active Contributor
0 Kudos

I don't think you'll be able to get the error you're looking for, but I'm not absolutely certain. don.williams from SAP might know for sure.

-Dell