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

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
fantasylanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
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
Todd GerbertIT ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

fantasylanAuthor Commented:
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
Éric MoreauSenior .Net ConsultantCommented:
>>Do I have to pass a specific path here in  report.Load("CrystalReport1.rpt")

Yes
0
fantasylanAuthor Commented:
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
Todd GerbertIT ConsultantCommented:
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
fantasylanAuthor Commented:
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
Todd GerbertIT ConsultantCommented:
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
fantasylanAuthor Commented:
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
Todd GerbertIT ConsultantCommented:
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
fantasylanAuthor Commented:
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
Todd GerbertIT ConsultantCommented:

report.SetDatabaseLogon("sa", "secret");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.