Wednesday, July 8, 2009

Crystal reports in ASP.NET


In this example i m going to describe how to create crystal reports in ASP.NET.

In this i am generating report by fetching data from two tables and grouping them based on Project Name. Database tables are just for demo purpose you can create your own tables with whatever schema you want
Two tables are as shown below.


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

 
Now click on CrystalReports Menu in VS and select DataBase Expert 
  
In database expert dialog box expend create new connection > OLEDB(ADO) section
 
Now select SQL Native client and enter you SQL server address , username , password and pick database name from the dropdown. 
 
  
In next screen Expend your database objects in left pane and add the tables you want to use in right pane 
 
Link your tables based on Primary keys (If any)

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

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

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, it should look like show below.


Go to default.aspx page and drag and drop CrystalReportViewer from the toolbox, click on smart tag and choose new report source.
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.

 
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 


Html markup of default.aspx look like
<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>

C# code behind

Write this code in the event you find appropriate , i m writing it in Page_Load , you can write this code in click event of button or in pagePreRender event
The code to provide password programmatically.
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;
    }

VB.NET code behind
Protected Sub Page_Load
(ByVal sender As Object, ByVal e As EventArgs)

Dim crystalReport As New ReportDocument()

crystalReport.Load(Server.MapPath("CrystalReport.rpt"))

crystalReport.SetDatabaseLogon
("amit", "password", "AMIT\SQLEXPRESS", "TestDB")

CrystalReportViewer1.ReportSource = crystalReport

End Sub

Hope this helps


related posts :
Crystal Reports in Winforms Windows Forms with Parameters C#.NET VB.NET

SubReports in Crystal Reports in ASP.NET
www.tips-fb.com
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks vote it on WebDevVote.com add to del.icio.us saved by 0 users
Subscribe to Feeds

7 comments:

काशिफ़ आरिफ़/Kashif Arif said...

WoW! U did the superb Job.

Please provide your post full feed.

I need your help..can u help me please

1. how can I add more links above the comment box?

2. how can I add these links & images below my post like you added in your post "digg it, tweet it, mixx it, Email subscription, etc etc

please mail me your answer


Himanshu said...

i got the problem in code behind in this line

ReportDocument crystalReport = new ReportDocument();

error is : Error 1 The type or namespace name 'ReportDocument' could not be found (are you missing a using directive or an assembly reference?). is there any namespace i

hv to add in code behind

my report is not well design all fields are coming 2 times in report.when i draged the fields in section 3. they automatically coming in section 2.


amiT
amiT jaiN said...

@Himanshu:

You need to add namespace in code behind


using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


When you drag field from database fields in section3 than same fields name comes in section 2 for heading , you can change fields name in section 2 to whatever you want to display


amiT
amiT jaiN said...

@काशिफ़ आरिफ़/Kashif Arif:

It requires editing html code of template, if you can do that ,let me know.
btw do u belongs to Agra ?


Anonymous said...

hey well i think this is a slower way
i think there is another way for making crystal report using data set but i don't know how
can any one help me ???


Anonymous said...

Dude thank u.... anyways for beginners just to create a basic report this is good...


Anonymous said...

i got an error when i try to load the report. It says failed to load repor...


About Me

My Photo
amiT jaiN
Hi, I am amiT jaiN Software engineer working on C#.NET and ASP.NET technologies
View my complete profile

Comments

.NET Resources

Find More Articles


Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN