locked
crystal report RRS feed

  • Question

  • User26400917 posted

    i have 3 cols sectorno,plot size, consumername
    in sectorno there are 6 different sectors(like d-1,d-2,e-1 etc),and plot size is also of different type(like 5m,7m,10m,12m etc).
    i want to put them in crystal report in such a way that when i click on d-1 and 5m so shows all those consumernames which lies in that d-1 sector and 5m plot size. please tell me how is it possible in asp.net

    Tuesday, June 7, 2011 8:30 AM

Answers

  • User-578657687 posted

    I have created a simplest sample with VS 2008 which tested successfully. Please try this:

    Illustrate:

    1. Create table:

    CREATE TABLE testTable2
    (
    sectorNo [varchar](50) NOT NULL,
    plot_size [varchar](50) NOT NULL,
    consumerName [varchar](50) NOT NULL
    )

    2. Insert values into the above table:

    insert testTable2 values('d-1','5m','aa')
    insert testTable2 values('d-2','7m','bb')
    insert testTable2 values('e-1','10m','bb')
    insert testTable2 values('e-2','12m','cc')
    insert testTable2 values('f-1','5m','cc')
    insert testTable2 values('f-2','10m','aa')
    insert testTable2 values('g-1','7m','cc')
    insert testTable2 values('g-2','5m','cc')
    insert testTable2 values('h-2','10m','bb')
    insert testTable2 values('d-1','5m','bb')

    3. Create a web project

    4. Add DataSet

       1)Right click and select "add" and then click the "DataTable"

       2)Rename the DataTable with "MyTable"

       3)Right click the MyTable and select "Add" option and then click the "Column"

       4)Repeat the above step two times

       5)So the fields such as sectorNo, plot_size and consumerName is the name of above three columns

    5. Right click the Web project and Select the "Add New Item"

        1)Select the Crystal Report and then click the "Add"

        2)By default, select the "Cross-Tab" in the  "Choose an Expert" and then click "OK"

        3)In turn to click the Project Data, ADO.NET DataSets, DataSet1 and MyTable

        4)Right click the MyTable and select "Add to Report"

        5)In turn to click the Database Fields and MyTable in Filed Explorer.

        6)Drag the three fields  into the CrystalReport.rpt

    6. The Default.aspx like this:

    <div>
        
         sectorno: 
            <asp:DropDownList ID="dplSectorNo" runat="server" DataSourceID="SqlDataSource1" 
                DataTextField="sectorNo" DataValueField="sectorNo">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:TestDataBaseConnectionString %>" 
                SelectCommand="SELECT [sectorNo], [plot_size] FROM [testTable2]">
            </asp:SqlDataSource>
            <br />
            <br />
            plot size:<asp:DropDownList ID="dplPlot" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="plot_size" 
                DataValueField="plot_size">
            </asp:DropDownList>
            <br />
            <br />
            <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" 
                Text="Search" />
            <br />
            <br />
            <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                AutoDataBind="true"/>
        </div>

    7. The Default.aspx.cs like this:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;
    
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string sectorNo = dplSectorNo.SelectedValue;
            string plot_size = dplPlot.SelectedValue;
            ConfigureCrystalReport(sectorNo,plot_size);
        }
         private void ConfigureCrystalReport(string sectorNo,string ploz_size)
        {
            //connection string
            string conStr = ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString();
            SqlConnection con = new SqlConnection(conStr);
    
            //Create a sql statement 
            string sqlStr = "select * from testTable2 where sectorNo='" + sectorNo + "' and plot_size='" + ploz_size+"'";
            SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, con);
    
            //fill data into ds
            DataSet1 ds=new DataSet1();
            adapter.Fill(ds,"MyTable");
    
            ReportDocument myReport = new ReportDocument();
            string reportPath = Server.MapPath("CrystalReport1.rpt");
            myReport.Load(reportPath);
            myReport.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = myReport;
           
        }
    }
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2011 4:36 AM

All replies

  • User-1364446067 posted

    Hi,

    These both tutorials are for beginners and will surely add much to your knowledge.

    http://csharpdotnetfreak.blogspot.com/2009/07/creating-crystal-reports-in-aspnet.html

    http://www.beansoftware.com/ASP.NET-Tutorials/Using-Crystal-Reports.aspx

     

    --

    Hope this helps..

    Tuesday, June 7, 2011 9:44 AM
  • User-578657687 posted

    Hi,

    Based on above information, I assume that you have a table including sectorNo, plot_size and consumerName. At that case you can follow several steps below:

    Note: Push model will be adopted(How to use Crystal Report please refer to the post of hassanmehmood)

    1. Create a Dataset

    2. Create the .rpt file and associate it with the Dataset that we created in the previous step.

    3. Create a .aspx page and drag two DropDownList control into the page

    4. One of DropListControl bound the sectorNo filed of table and another bound the plot_size filed of table

    5. Place a CrystalReportViewer control on the .aspx page and set its properties to point to the .rpt file that we created in the previous step.

    6. In your code behind page, write the subroutine to make the connections to the database and populate the dataset with two parameters from both DropDownList  that we created previously in step one.

    7. Call the Databind method from your code behind page.

    Friday, June 10, 2011 2:33 AM
  • User26400917 posted
    that is right. could you please give the code???
    Saturday, June 11, 2011 2:56 PM
  • User-578657687 posted

    Hi,

    Ok, I will try to build a sample for you.

    Monday, June 13, 2011 1:53 AM
  • User-578657687 posted

    I have created a simplest sample with VS 2008 which tested successfully. Please try this:

    Illustrate:

    1. Create table:

    CREATE TABLE testTable2
    (
    sectorNo [varchar](50) NOT NULL,
    plot_size [varchar](50) NOT NULL,
    consumerName [varchar](50) NOT NULL
    )

    2. Insert values into the above table:

    insert testTable2 values('d-1','5m','aa')
    insert testTable2 values('d-2','7m','bb')
    insert testTable2 values('e-1','10m','bb')
    insert testTable2 values('e-2','12m','cc')
    insert testTable2 values('f-1','5m','cc')
    insert testTable2 values('f-2','10m','aa')
    insert testTable2 values('g-1','7m','cc')
    insert testTable2 values('g-2','5m','cc')
    insert testTable2 values('h-2','10m','bb')
    insert testTable2 values('d-1','5m','bb')

    3. Create a web project

    4. Add DataSet

       1)Right click and select "add" and then click the "DataTable"

       2)Rename the DataTable with "MyTable"

       3)Right click the MyTable and select "Add" option and then click the "Column"

       4)Repeat the above step two times

       5)So the fields such as sectorNo, plot_size and consumerName is the name of above three columns

    5. Right click the Web project and Select the "Add New Item"

        1)Select the Crystal Report and then click the "Add"

        2)By default, select the "Cross-Tab" in the  "Choose an Expert" and then click "OK"

        3)In turn to click the Project Data, ADO.NET DataSets, DataSet1 and MyTable

        4)Right click the MyTable and select "Add to Report"

        5)In turn to click the Database Fields and MyTable in Filed Explorer.

        6)Drag the three fields  into the CrystalReport.rpt

    6. The Default.aspx like this:

    <div>
        
         sectorno: 
            <asp:DropDownList ID="dplSectorNo" runat="server" DataSourceID="SqlDataSource1" 
                DataTextField="sectorNo" DataValueField="sectorNo">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:TestDataBaseConnectionString %>" 
                SelectCommand="SELECT [sectorNo], [plot_size] FROM [testTable2]">
            </asp:SqlDataSource>
            <br />
            <br />
            plot size:<asp:DropDownList ID="dplPlot" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="plot_size" 
                DataValueField="plot_size">
            </asp:DropDownList>
            <br />
            <br />
            <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" 
                Text="Search" />
            <br />
            <br />
            <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                AutoDataBind="true"/>
        </div>

    7. The Default.aspx.cs like this:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;
    
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string sectorNo = dplSectorNo.SelectedValue;
            string plot_size = dplPlot.SelectedValue;
            ConfigureCrystalReport(sectorNo,plot_size);
        }
         private void ConfigureCrystalReport(string sectorNo,string ploz_size)
        {
            //connection string
            string conStr = ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString();
            SqlConnection con = new SqlConnection(conStr);
    
            //Create a sql statement 
            string sqlStr = "select * from testTable2 where sectorNo='" + sectorNo + "' and plot_size='" + ploz_size+"'";
            SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, con);
    
            //fill data into ds
            DataSet1 ds=new DataSet1();
            adapter.Fill(ds,"MyTable");
    
            ReportDocument myReport = new ReportDocument();
            string reportPath = Server.MapPath("CrystalReport1.rpt");
            myReport.Load(reportPath);
            myReport.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = myReport;
           
        }
    }
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2011 4:36 AM
  • User26400917 posted

    thats great. thank you

    Tuesday, June 14, 2011 6:38 AM