2012/03/23

Create Crystal Report (with sub report) In ASP.NET

Step1- Register the Crstal Report DLL (Not Need to include the DLL In Bin Folder) by using the following         lines
<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %> 
Step2- Take the Crystal Report Viewer control onto the page Like this
<CR:CrystalReportViewer ID="Crv" runat="server" AutoDataBind="true" />
Step-3- Create Report: first of Create an XSD then bind the report with the XSD (here not need to create a new connection)
Step-4 Use these function to bind the report
private DataSet GetReportData()
    {
        try
        {
            DataSet dsReport = new DataSet();
            DataTable dtParent = new DataTable();
            dtParent.Columns.Add("ParentID");
            dtParent.Columns.Add("ParentName");
            for (int i = 1; i <= 3; i++)
            {
                dtParent.Rows.Add(i, "Parent " + i.ToString());
            }
            dsReport.Tables.Add(dtParent);

            DataTable dtChild = new DataTable();
            DataTable dtSubReport = new DataTable();
            dtChild.Columns.Add("ParentID");
            dtChild.Columns.Add("ClientID");
            dtChild.Columns.Add("ClientName");
            dtChild.Columns.Add("ClientAddress");
            dtChild.Columns.Add("DOB");
            dtSubReport.Columns.Add("ClientID");
            dtSubReport.Columns.Add("ProductDetails");
            for (int j = 1; j <= 3; j++)
            {
                for (int i = 21; i <= 23; i++)
                {
                    dtChild.Rows.Add(j, j.ToString() + i.ToString(), "ClientName " + i.ToString(), "ClientAddress " + i.ToString(), "DOB " + i.ToString());
                    dtSubReport.Rows.Add(j.ToString() + i.ToString(), "ProductDetails " + i.ToString());
                }
            }
            dsReport.Tables.Add(dtChild);
            dsReport.Tables.Add(dtSubReport);

            return dsReport;
        }
        catch 
        {
            
            throw;
        }
    }

    private void ShowReport()
    {
        try
        {
            string ReportName = string.Empty;
            this.Title = "MMNDY:RMSC Medicines List";
            DataSet dsReport = new DataSet();
            dsReport = GetReportData();

            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("") + "\\Reports\\GroupBylReport.rpt";
            rpt.Load(path);
            rpt.Database.Tables[0].SetDataSource(dsReport);
            rpt.SetParameterValue("Title", "My Test Report");
            rpt.SetParameterValue("DesignedBy", "Harish & Asvini");
            Crv.ReportSource = rpt;
            Crv.DataBind();
        }
        catch 
        {
            
            throw;
        }
    }

This will show the report on the page.

0 comments:

Post a Comment