Sunday, August 18, 2013

Crystal Report in Asp.Net


Steps

1.Create a new website and right click on solution explorer > add new Item > Select Crystal Report
In the dialog box choose blank report



2.Now click on CrystalReports Menu in VS and select DataBase Expert


3.In database expert dialog box expend create new connection > OLEDB(ADO) section

4.Now select SQL Native client and enter you SQL server address , username , password and pick database name from the dropdown.

5.In next screen Expend your database objects in left pane and add the tables you want to use in right pane

6.Link your tables based on Primary keys (If any)

7.Click ok to finish the wizard.
Right click on Field Explorer and select Group Name Fields  > Insert Group

8.In next box select the field you to report to be grouped (in my case it's ProjectsName)

9.Click on OK to finish
Now design the report , drag and fields from Database fields in field explorer and which you want to show in report and drop them in Section3(Details), and preview the report,

10.Go to default.aspx page and drag and drop CrystalReportViewer from the toolbox, click on smart tag and choose new report source.

11.Choose you report from the dropdown menu and click ok to finish.
Now when you build and run the sample , it asks for the database password everytime.

12.To fix this we need to load the report programmatically and provide username and password from code behind .
Now run the report , it should look like this

13. the code is

protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
        crystalReport.SetDatabaseLogon
            ("amit", "password", @"AMIT\SQLEXPRESS", "TestDB");
        CrystalReportViewer1.ReportSource = crystalReport;
    }



Html



<form id="form1" runat="server">
<div>
  <CR:CrystalReportViewer ID="CrystalReportViewer1"
                          runat="server" AutoDataBind="True"
                          Height="1039px"
                          ReportSourceID="CrystalReportSource1"
                          Width="901px" />
  <CR:CrystalReportSource ID="CrystalReportSource1"
                          runat="server">
            <Report FileName="CrystalReport.rpt">
            </Report>
   </CR:CrystalReportSource>
   
    </div>
    </form>

No comments:

Post a Comment