简体   繁体   中英

Why am I getting failed to open connection in Crystal Reports method ExportToDisk

I have a report that users say was working. This application is new to me and Crystal Reports is new to me. The database was backed up and restored to a new SqlServer. The orphaned users were relinked and the report code (shown below) changed to point to the new server. Now it get's failed to connect error. the report is run from c# code. Here is the code that calls the report, followed by the values at the point the code threw the exception.

        private void laborRptByJobBtn_Click(object sender, EventArgs e)
    {
        if (rptFolderDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptObj = new LaborRptByJob();
                CrystalDecisions.Shared.TableLogOnInfo tliEZIS = new TableLogOnInfo();


                tliEZIS.TableName = rptObj.Database.Tables[0].Name;
                tliEZIS.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["DatabaseServerName"];
                tliEZIS.ConnectionInfo.DatabaseName = "EZIS";
                tliEZIS.ConnectionInfo.UserID = "EZIS";
                tliEZIS.ConnectionInfo.Password = "EZIS";
                rptObj.Database.Tables[0].ApplyLogOnInfo(tliEZIS);
                rptObj.SetParameterValue("JobNumber", division.Text.Trim());
                string testPath = rptFolderDialog.SelectedPath + "\\Job " + division.Text.Trim() + " - Labor Report.pdf";
                rptObj.ExportToDisk(ExportFormatType.PortableDocFormat, testPath);
                //rptObj.ExportToDisk(ExportFormatType.PortableDocFormat, rptFolderDialog.SelectedPath + "\\Job#: " + division.Text.Trim() + " - Labor Report.pdf");
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show(ex.Message, ex.Source);
            }
        }
    }

}

I am getting this seemingly interesting error message in the watch list upon exception:

        'rptObj.Database.Tables[0].Location' threw an exception of type 'System.InvalidCastException'   error CS1012: Too many characters in character literal  

I know that implies the use of single quotes to set a string but I looked through the code and didn't find a direct setting of the location parameter at all or any single quotes. Also at the same time I set a watch to location and got this

        rptObj.Database.Tables[0].Location  "LaborReportView"   string

other values are:

ServerName = "\\\\appdev02\\mssql2017dev"
DatabaseName = "EZIS"
UserId = "EZIS"
Testpath = "\\\\Mac\\Home\\Desktop\\Job 2849 - Labor Report.pdf"

In the watch list there are several rows of invalidcastexceptions such as one on Location and one on Name. I have used SSMS to verify that the EZIS login to \\appdev02\\mssql2017dev EZIS database exists with the same password and has rights to the view.

Well I found the answer with help. The error message was couldn't connect so I spent time looking at why that might be. The real problem was the source of the data was a view and the view was broken. In the old database the view linked to a separate server using openquery(). In the new server the two databases were on the same server so the "link" was not available. A simple change to a fully qualified name made it work.

I'm posting this in spite of the fact that I'm embarrassed I didn't try to run the view much earlier in the diagnosis of the problem.

Prime learning (relearning) never trust an error message to indicate exactly what the problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM