Solved

Exporting crystal report as PDF from C# Invalid Credentials

Posted on 2013-11-22
8
1,225 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now