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
Solved

Exporting crystal report as PDF from C# Invalid Credentials

Posted on 2013-11-22
8
1,257 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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