Click here to Skip to main content
15,886,518 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.5K   446   20  
Reduced code approach to parameter binding in Crystal Reports
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CRParameters.aspx.cs" Inherits="CRParameters" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Crystal Report Parameters</title>
    <link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css" rel="stylesheet" type="text/css" />
    <link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
        rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong></strong>
        <asp:DropDownList ID="DropDownList1" runat="server" 
                            AutoPostBack="True" DataSourceID="SqlDataSource1" 
                            DataTextField="CompanyName" DataValueField="CustomerID" AppendDataBoundItems="true">
            <asp:ListItem Text="-- Customer Order History --" Value="NULL" Selected="True" />
        </asp:DropDownList>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                            SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]"
                            SelectCommandType="Text">
        </asp:SqlDataSource>
        <br />
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" ReuseParameterValuesOnRefresh="True" DisplayGroupTree="False" ReportSourceID="CrystalReportSource1" />
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server" OnDataBinding="CrystalReportSource1_DataBinding" OnInit="CrystalReportSource1_Init" OnLoad="CrystalReportSource1_Load" OnPreRender="CrystalReportSource1_PreRender" OnUnload="CrystalReportSource1_Unload">
            <Report FileName="Reports\CustOrderHistory.rpt">
                <DataSources>
                    <CR:DataSourceRef DataSourceID="SqlDataSource2" TableName="CustOrderHist;1" />
                </DataSources>
            </Report>
        </CR:CrystalReportSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                            SelectCommand="CustOrderHist" SelectCommandType="StoredProcedure" OnSelecting="SqlDataSource2_Selecting" OnSelected="SqlDataSource2_Selected">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
    </div>
    </form>
</body>
</html>

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