Search This Blog

Thursday, August 19, 2010

Crystal Report

public void FillCrystalReports(string strStoredProc, string CRPPath, CrystalReportViewer CRPViewer)
{
CRPPath=System.Web.HttpContext.Current.Server.MapPath(CRPPath);

try
{
SqlDataAdr = new SqlDataAdapter(strStoredProc, SqlConn);
SqlDataAdr.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdr.SelectCommand.CommandTimeout = 0;

ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;


SqlDataAdr.Fill(ds);
ReportDocument Rep = new ReportDocument();
//Rep.SetDatabaseLogon("sa", "sa", ".", "SchoolDB");
Rep.Load(CRPPath);
Rep.SetDataSource(ds.Tables[0]);
CRPViewer.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
CRPViewer.ReportSource = Rep;
if (ds.Tables[0].Rows.Count == 0)
{
CRPViewer.Visible = false;
msgboxcs.MessageBox.Show("No Records Available");
}

}
catch (Exception ex)
{
msgboxcs.MessageBox.Show(ex.Message);
}
}
}





Print Crystal Reports 

using System;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            crConnectionInfo.ServerName = "YOUR SERVERNAME";
            crConnectionInfo.DatabaseName = "DATABASE NAME";
            crConnectionInfo.UserID = "USERID";
            crConnectionInfo.Password = "PASSWORD";

            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            cryRpt.Refresh();
            cryRpt.PrintToPrinter(2, true, 1, 2);

        }
   }
}
C# Crystal Report Print 
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);private void button2_Click(object sender, System.EventArgs e)
{
    //Open the PrintDialog
    this.printDialog1.Document = this.printDocument1;
    DialogResult dr = this.printDialog1.ShowDialog();
    if(dr == DialogResult.OK)
    {
        //Get the Copy times
        int nCopy = this.printDocument1.PrinterSettings.Copies;
        //Get the number of Start Page
        int sPage = this.printDocument1.PrinterSettings.FromPage;
        //Get the number of End Page
        int ePage = this.printDocument1.PrinterSettings.ToPage;
        //Get the printer name
        string PrinterName = this.printDocument1.PrinterSettings.PrinterName;

        crReportDocument = new ReportDocument();
        //Create an instance of a report
        crReportDocument = new Chart();
        try
        {
            //Set the printer name to print the report to. By default the sample
            //report does not have a defult printer specified. This will tell the
            //engine to use the specified printer to print the report. Print out 
            //a test page (from Printer properties) to get the correct value.

            crReportDocument.PrintOptions.PrinterName = PrinterName;


            //Start the printing process. Provide details of the print job
            //using the arguments.
            crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);

            //Let the user know that the print job is completed
            MessageBox.Show("Report finished printing!");
        }
        catch(Exception err)
        {
            MessageBox.Show(err.ToString());
        }
    }
}
Crystal Report Show to Date 
DS1 dts = new DS1();
            SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True");

            try
            {
                DateTime dt = new DateTime();
                dt = Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString());
                // SqlCommand cmd = new SqlCommand("SELECT * From JKL where Date='1/1/1900 12:00:00 AM' ", con);
                SqlCommand cmd = new SqlCommand("SELECT * From crtstaff where Date='" + dt + "'", con);

                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(dts.Tables["crtstaff"]);
                //  DataTable dt = new DataTable("JKL");
                // sda.Fill(dt);
                StaffDetails  cr = new StaffDetails();

                cr.SetDataSource(dts);
                crystalReportViewer1.ReportSource = cr;

                // cr.SetDataSource(dt);

            }
            catch (Exception ex)
            { }
        }

No comments:

Post a Comment