?
Solved

How do I send crystal report from c sharp console application?

Posted on 2010-04-06
13
Medium Priority
?
1,060 Views
Last Modified: 2012-05-09
Hello,

I have a ASP.net C sharp console application. I added and created a crystal report at design time and linked it to my SQL server database. Now, I want to distribute it by email(SMTP) through the console application. How can I do this? Can I send it as PDF and a RPT ?
Code snippets would be very helpful.

Regards
0
Comment
Question by:fantasylan
  • 6
  • 5
  • 2
13 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29926475
I have a ASP.net C sharp console application
Is it a web site application, or a program that runs on a computer in a "DOS" box?
 
You should be able to export your Crystal Report as a PDF, then attach it to an e-mail.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1600 total points
ID: 29927220
Here's a super-simple Console application example. Putting this into actual use would require error checking/exception handling, at the least.
using System.IO;
using System.Net.Mail;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			ReportDocument report = new ReportDocument();

			report.Load("CrystalReport1.rpt");

			report.ExportToDisk(ExportFormatType.PortableDocFormat, "Report.pdf");

			SmtpClient smtp = new SmtpClient("mail.domain.com");
			MailMessage message = new MailMessage("reportgenerator@domain.com", "recipient@domain.com");

			message.Attachments.Add(new Attachment("Report.pdf"));

			smtp.Send(message);

			File.Delete("report.pdf");
		}
	}
}

Open in new window

0
 
LVL 71

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 29927222
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:fantasylan
ID: 29932317
Hi tgerbert,

I got a run time error with the following text:

"Load report failed."

Any suggestions. The crystal report that I created is also called "CrystalReport1.rpt".  Do I have to pass a specific path here in  report.Load("CrystalReport1.rpt") ?
0
 
LVL 71

Expert Comment

by:Éric Moreau
ID: 29932420
>>Do I have to pass a specific path here in  report.Load("CrystalReport1.rpt")

Yes
0
 

Author Comment

by:fantasylan
ID: 29938584
Hi emoreau,

The file is  located in the root folder. I tried
String path = HttpContext.Current.Request.MapPath("~/CrystalReport1.rpt") and
path = HttpContext.Current.Server.MapPath("~/CrystalReport1.rpt");

but I am getting a simple Null reference error which I cant seem to figure out.

Please help.

Regards.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29939064
So, it's an ASP.Net web application - not a console application?
I think Server.MapPath("~/anyfile.ext") would return a file path, whether the file exists or not.
Can you put a breakpoint in your code and verify "path" is being correctly set, and that the Crystal Report actually exists where "path" thinks it does?
0
 

Author Comment

by:fantasylan
ID: 29939219
Hi tgerbert,

It is a console application.

I tried your code but I was getting the "File Not found" error, so i tried it this way but it IS a console application.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29939558
Then you can't use Server.MapPath or Request.MapPath, they're exlusively for use in a web site application (your null reference exception is because the "Server" and "Request" objects are both null).
In Visual Studio, in the Solution Explorer section, select your Crystal Report; then in the properties window for that file change the "Copy to Output" setting to "If Newer"
By default, the report does not get copied to the \bin\debug or \bin\release folder along with your .EXE.
0
 

Author Comment

by:fantasylan
ID: 29943534
Hi tgerbert,

Thanks... I got through that step but I got stuck on the next one which says "logon failed for user sa".

Regards,

Nitin
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29945543
You need to provide a database login for your report.  I'm not sure off the top of my head how that's done when using a SQL database.
0
 

Author Comment

by:fantasylan
ID: 29947760
I have done that and I can preview the report in Visual Studio itself. Its only when  I run the application that I am getting the error.

Regards.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29947905

report.SetDatabaseLogon("sa", "secret");
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

588 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