Pages

Showing posts with label Crystal Report Example. Show all posts
Showing posts with label Crystal Report Example. Show all posts

Wednesday, November 24, 2010

Crystal Report Example

In Aspx file
  <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" PrintMode="ActiveX" DisplayGroupTree="False"></CR:CrystalReportViewer>

In Aspx.cs  file

ReportDocument rptDocument = null;
protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            rptDocument = new ReportDocument();
            Session["rptDocument"] = rptDocument;
        }
        else
        {
            rptDocument = (ReportDocument)Session["rptDocument"];
            if (rptDocument == null)
            {
                rptDocument = new ReportDocument();
                Session["rptDocument"] = rptDocument;

            }
        }
        if (IsPostBack)
        {
            CreateReport();
        }

    }
private void CreateReport()
    {
       
rptDocument = new ReportDocument();
            string Query = string.Empty;
rptDocument.Load(Server.MapPath("report path"));
using (OdbcConnection OCN = new OdbcConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString))
            {
                OCN.Open();

                OdbcCommand ocmd = OCN.CreateCommand();
                OdbcDataAdapter sadpt = new OdbcDataAdapter(ocmd);
dsReport DSTEMP = new dsReport();
                DSTEMP.Clear();
                ocmd.CommandText = Query;
                sadpt.Fill(DSTEMP, "datatable1");
                rptDocument.SetDataSource(DSTEMP);
                rptDocument.SetParameterValue("",””);//parameter nameAnd val
                CrystalReportViewer1.ReportSource = rptDocument;
                CrystalReportViewer1.Visible = true;
            }
    }
    protected void cmdPrint_Click(object sender, EventArgs e)
    {
        rptDocument.PrintToPrinter(1, false, 0, 0);
    }



Search This Blog