Solved

Exporting crystal report as PDF from C# Invalid Credentials

Posted on 2013-11-22
8
1,275 Views
Last Modified: 2013-11-27
I have been trying for hours to a bit of functionality to work. I am trying to programmatically generate a pdf from a crystal report and email it to a user. I am able to make that work if I choose a crystal report that doesn't require a database connection, but the minute I try to connect to a report when login credentials are required it fails. I have tried countless things such as the SetDatabaseLogon method as well as using the TableLogonInfo class and looping through the tables in the report. No matter what I do I continue to receive the error message below

Logon failed.
Details: 28000:[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'sa'.Error in File J:\WINDOWS\TEMP\hotpositions {01D7DA6A-BF42-4248-9297-D846B93BFA3F}.rpt:
Unable to connect: incorrect log on parameters.

Here is some of the code below, like I said I also tried the tablelogoninfo method as well to no avail. Please let me know if you see anything glaring. Of course I have modified the actual database info here since it is sensitive. Thanks for any help you can provide

 ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(Server.MapPath("~/reporttemplates/hotpositions.rpt"));
            crystalReport.SetDatabaseLogon("sa", "password", "servername", "databasename", true);
            CrystalReportViewer1.ReportSource = crystalReport;

            using (MailMessage mm = new MailMessage("aries@ariesdatabase.com", "mporter@nextmedicalstaffing.com"))
            {
                mm.Subject = "Crystal Report PDF example";
                mm.Body = "Crystal Report PDF example";
                mm.Attachments.Add(new Attachment(crystalReport.ExportToStream(ExportFormatType.PortableDocFormat), "Report.pdf"));
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "localhost";
                smtp.Send(mm);
            }
0
Comment
Question by:nextmedstaff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 39669646
Does the report have a subreport?

If so you have to do the same for each subreport.

does the report open in the viewer?

mlmcc
0
 

Author Comment

by:nextmedstaff
ID: 39669682
there are no sub reports, and it will show in a viewer, but it pops up with the login credential screen asking me to enter. For some reason the code is not passing through the login credentials to the right area, there is some sort of disconnect.
0
 
LVL 18

Expert Comment

by:vasto
ID: 39669693
You are setting integrated security ( I believe the last flag = true is for that). Change it to false and try again.

If you want to use integrated security then the easiest way is to remove the code which is setting the connection information and to point the report to use an ODBC DSN file which is using integrated security.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:nextmedstaff
ID: 39669705
I just switched to false and it still failed with the same error message. That sounded like a good option, unfortunately it still didn't correct the issue. Thanks for the input though.
0
 
LVL 18

Expert Comment

by:vasto
ID: 39669730
You may need to set also the location for the tables
0
 

Author Comment

by:nextmedstaff
ID: 39669754
Ok so I added a reportviewer and used the foreach statement to credential each TableLogOnInfo in the crystalreportviewer object. That actually worked. How do you apply those same credentials to a ReportDocument object, I believe that is where I am running into the issue, obviously it works for the viewer and that made the difference there so somehow I need to do the same thing to the reportdocument object if possible.
0
 

Accepted Solution

by:
nextmedstaff earned 0 total points
ID: 39669817
I finally figured it out. I kept trying to use the actual name of the server in the servername field. It actually wants the name of the ODBC Datasource from the control panel.

ConnectionInfo cnInfo = new ConnectionInfo();
            cnInfo.DatabaseName = "databasename";
            cnInfo.ServerName = "dsnodbcname";
            cnInfo.UserID = "sa";
            cnInfo.Password = "password";
            cnInfo.IntegratedSecurity = false;
           
            foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in  crystalReport.Database.Tables)
            {
                TableLogOnInfo logonIfo = tbl.LogOnInfo;
                logonIfo.ConnectionInfo = cnInfo;
                tbl.ApplyLogOnInfo(logonIfo);
            }

Just wanted to share in case someone else ran into this issue
0
 

Author Closing Comment

by:nextmedstaff
ID: 39680251
I found an article on the MSDN website that helped me find the solution.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question