Click here to Skip to main content
15,893,487 members
Articles / Web Development / ASP.NET

Binding parameters to a Crystal Report using SqlDataSource control – A reduced code approach

Rate me:
Please Sign up or sign in to vote.
2.48/5 (10 votes)
3 Nov 20072 min read 52.6K   446   20  
Reduced code approach to parameter binding in Crystal Reports
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Enterprise;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using System.Data.SqlClient;

public partial class CRParameters : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // ConfigureCrystalReports();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
        CrystalReportViewer1.DisplayGroupTree = false;
    }


    private void SetDBLogonForReport(ConnectionInfo connectionInfo)
    {
        ReportDocument rpt = new ReportDocument();
        rpt.Load(Server.MapPath("~/Reports/CustOrderHistory.rpt"));

        // When we build a report with a stored procedure that furnishes data
        // from many tables, we need to login to all the underlying tables
        // to avoid a dialog box asking the user for connection information.
        foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in rpt.Database.Tables)
        {
            TableLogOnInfo crTableLogOnInfo = crTable.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = connectionInfo;
            crTableLogOnInfo.TableName = crTable.Name;
            crTable.ApplyLogOnInfo(crTableLogOnInfo);
            crTableLogOnInfo = null;
        }
        CrystalReportViewer1.ReportSource = rpt;
    }


    private void ConfigureCrystalReports()
    {
        ConnectionInfo connectionInfo = new ConnectionInfo();
        connectionInfo.ServerName = "SQLServer2005";
        connectionInfo.DatabaseName = "Northwind";
        connectionInfo.UserID = "sa";
        connectionInfo.Password = "ispl123";

        SetDBLogonForReport(connectionInfo);
    }


    protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            e.Command.Parameters["@CustomerID"].Value = DBNull.Value;
        }
    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
    }

    protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
    }

    protected void CrystalReportSource1_Init(object sender, EventArgs e)
    {
    }

    protected void CrystalReportSource1_Load(object sender, EventArgs e)
    {
    }

    protected void CrystalReportSource1_PreRender(object sender, EventArgs e)
    {
    }

    protected void CrystalReportSource1_DataBinding(object sender, EventArgs e)
    {
    }

    protected void CrystalReportSource1_Unload(object sender, EventArgs e)
    {
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Viswanath Majeti works as a Project Lead in .NET Technologies for a software development company in Hyderabad, India

Comments and Discussions