Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Report that I made in Crystal Reports 2013. I also have a web project that I have made as well. When a user logs into the system they can fill out the forms and submit the data to the database. While in a session, then they can click on print to print a report of the data they just submitted. I made a report using Crystal Reports program, (not through ASP.NET). I have added the reports to the project and add a CrystalReportViewer to a blank web form. Using the report that I made, How can I get the report to print from my web project with the data that is in the database that the user submitted? I looked through Google and came up with how to make Crystal Reports in ASP.NET but not on how to import a current report to print. Please Help.

This the update for what I have done so far.

I have added the three reports to the project by adding a new item, clicking reporting, clicked on Crystal Reports Visual C#, renamed the report at the bottom and clicked add button. The Wizard pops up and I choose From an Existing Report. Clicked ok and chose the report I needed. I did this to all three reports that I needed because each user has a different report to print. Then I added a new item again but this time it is three web forms that I also added the CrystalReportViewer to each web form. That is what i have so far. How can I get the data the user just submitted into the database to these Reports to be printed on button click?

Here is a code I found on Google but I don't have a data set because I created the Reports in Crystal Reports 2013 and added them to the project. How can I modify this code to work for me?

C#
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        CrystalReportViewer1.Visible = false;

    }

    protected void cmdcrystal_Click(object sender, EventArgs e)

    {

        CrystalReportViewer1.Visible = true;

        ReportDocument rDoc = new ReportDocument();

        Mydataset dset = new Mydataset(); // dataset file name

        DataTable dtable = new DataTable(); // data table name

        dtable.TableName = "Crystal Report ";  // Crystal Report Name

        rDoc.Load(Server.MapPath("mycrystal.rpt")); // Your .rpt file path

        rDoc.SetDataSource(dset); //set dataset to the report viewer.

        CrystalReportViewer1.ReportSource = rDoc;

    }

}
Posted
Updated 22-Jan-14 4:32am
v3

I hope you figured how to show or render a crystal report. But to print it on server side i guess you are trying for. you need the following code
C#
using (Impersonation imp = new Impersonation(impersonationUsername, impersonationDomain, impersonationPWD))
{
 if (!string.IsNullOrEmpty(printerName))
   {
       myreport.PrintOptions.PrinterName = printerName;
   }
  myreport.PrintToPrinter(copies, false, 0, 0);
}


Since your process runs under IIS localhost user Here am doing Impersonation so that i can access the SYSTEM/print. Generally crystal report prints to the default printer on the machine.

IMHO dont use crystal reports for printing they didn't implemented atleast basic W3C standards like you cant get styles, bulleted data etc. A simple details can be done but if you want bit look and feel none in world had the compatibility.

I recommend http://stackoverflow.com/questions/10024273/crystal-report-with-html-content-formatting[^] and http://stackoverflow.com/questions/11208167/html-or-pdf-printing-on-server-side-c-sharp[^]

Regards,
Pavan N
 
Share this answer
 
Comments
Computer Wiz99 22-Jan-14 9:35am    
Ok Thanks Pavan N Kumar!! When the print button it clicked I don't want to show a print preview screen, just print the report. Plus I am new to Crystal Reports. Will this print the Report in a session to where the user and only that user will print the data that they have?
Computer Wiz99 22-Jan-14 9:47am    
Pavan, I have updated my question and added the steps taken.
Pavan_N 22-Jan-14 9:53am    
So you are actually trying to print on server side.Is it an intranet application you are developing for?
In my case am doing server side printing for which i wrote windows service to fetch the printer names available in app server PC.

In the UI there is settings page in which we will select a default printer name. So when ever user tried to print the default rinter will be changed and print job is processed and the previous default printer will be reverted back. Mostly i did a workaround kind of thing.

I suggest go with singleton pattern.

Regards,
Pavan N
Computer Wiz99 22-Jan-14 10:08am    
I have Crystal Reports 2013 on my computer that i am programming on and I installed the Crystal Reports plugin for VS 2010. When I Publish the web site to my web server everything should work. I have it up now but I updated it every month and they need a report of the data that was submitted. Like I said earlier that I have users that are on different levels and I have them in sessions. So when they log into the system they can print a report off of the data that they just submitted with their name on it. Do I have to put the session code behind on each ReportForm.aspx along with the connection to the database?
Computer Wiz99 22-Jan-14 12:16pm    
Hey Pavan. I know you gave me a solution but can you check out my new question? How to print Crystal Reports in ASP.NET using C#?
Check these
Automatically Printing Crystal Reports in ASP.NET[^]
How to print Crystal Reports in C#[^]

EDIT


You forgot to add the DataTable to DataSet, Change the code like below
C#
protected void cmdcrystal_Click(object sender, EventArgs e)
{
    CrystalReportViewer1.Visible = true;
    ReportDocument rDoc = new ReportDocument();
    Mydataset dset = new Mydataset(); // dataset file name
    DataTable dtable = new DataTable(); // data table name
    dtable.TableName = "Crystal Report ";  // Crystal Report Name
    dset.Tables.Add(dtable);//Newly added line--------------------
    rDoc.Load(Server.MapPath("mycrystal.rpt")); // Your .rpt file path
    rDoc.SetDataSource(dset); //set dataset to the report viewer.
    CrystalReportViewer1.ReportSource = rDoc;
}

Besides, TableName should be same as the Report Table. I have explained that in my past answer, check it out.
display data in crystal reports[^]
 
Share this answer
 
v2
Comments
Computer Wiz99 22-Jan-14 10:30am    
Thatraja, Thanks for the links. I have read these before. I have a code that I have added to my question.
thatraja 22-Jan-14 10:31am    
Code? I don't see any
Computer Wiz99 22-Jan-14 10:33am    
Refresh again. I just added it.
Computer Wiz99 22-Jan-14 10:38am    
I have the reports in a web form with Crystal Report Viewer on the form but no data set. Do I need a data set?
thatraja 22-Jan-14 10:46am    
Check updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900