Solved

Exporting crystal report as PDF from C# Invalid Credentials

Posted on 2013-11-22
8
1,262 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
  • 5
  • 2
8 Comments
 
LVL 100

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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