locked
An easiest way to create a parameterized crystal report ? RRS feed

  • Question

  • User245461779 posted

    Hello !

    I have create a following parameterized crystal report using StoredProcedure on Northwind database.

    Is there any better and simplest way to create this report ?

    The report should provide print , export and refresh button facility.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
            .style1
            {
                width: 151px;
            }
            .style2
            {
                width: 320px;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <table border="1" bgcolor="#FFFFCC">
                <tr>
                    <td class="style1">
                        Select Category :
                    </td>
                    <td class="style2">
                        <asp:DropDownList ID="drpCategory" runat="server" Height="26px" Width="299px" 
                            DataSourceID="SqlDataSource1" DataTextField="CategoryName" 
                            DataValueField="CategoryID">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                            SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td class="style1">
                        Select Order Year :
                    </td>
                    <td class="style2">
                        <asp:DropDownList ID="drpOrdYear" runat="server" Height="26px" Width="299px">
                            <asp:ListItem Selected="True">1996</asp:ListItem>
                            <asp:ListItem>1997</asp:ListItem>
                            <asp:ListItem>1998</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="style1">
                    </td>
                    <td class="style2">
                        <asp:Button ID="btnShowRep" runat="server" Text="Show Report" Height="31px" />
                    </td>
                </tr>
            </table>
            <br />
            <hr />
            
            <br />
            <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                AutoDataBind="True" HasRefreshButton="True" ToolPanelView="None" 
                Visible="False" EnableDatabaseLogonPrompt="False" 
                EnableParameterPrompt="False" GroupTreeImagesFolderUrl="" Height="1202px" 
                onload="CrystalReportViewer1_Load" ToolbarImagesFolderUrl="" 
                ToolPanelWidth="200px" Width="903px" />
        </div>
        </form>
    </body>
    </html>

    --------- cs code ----------------

     void BindRep()
            {
                DataTable dt = new DataTable();
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    
                SqlCommand cmd = new SqlCommand("SalesByCategory", conn);
                cmd.CommandType = CommandType.StoredProcedure;
    
                cmd.Parameters.AddWithValue("@CategoryName", drpCategory.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@OrdYear", drpOrdYear.SelectedItem.Text);
    
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
    
                ReportDocument rdoc = new ReportDocument();
    
                rdoc.Load(Server.MapPath("RptSalesByCatChart.rpt"));
    
                rdoc.SetDatabaseLogon("sa", "1", "mayurpc\\sqlexpress", "Northwind");
    
                rdoc.SetParameterValue("@CategoryName", drpCategory.SelectedItem.Text);
                rdoc.SetParameterValue("@OrdYear", drpOrdYear.SelectedValue);
    
                rdoc.SetDataSource(dt);
    
                CrystalReportViewer1.ReportSource = rdoc;
    
                CrystalReportViewer1.Visible = true;
    
            }
            protected void CrystalReportViewer1_Load(object sender, EventArgs e)
            {
                if (IsPostBack)
                    BindRep();
            }
    Saturday, December 31, 2011 11:08 AM

Answers

  • User-231977777 posted

    Is there any better and simplest way to create this report ?

    it is up to you ,

    reading data from database each time you need , Or storing Data into DataTable and use Rowfilter Property with DataView to filter data , all is ok. and your way is Ok.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 31, 2011 8:17 PM