?
Solved

Exporting crystal report as PDF from C# Invalid Credentials

Posted on 2013-11-22
8
Medium Priority
?
1,285 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
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!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 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