Crystal Reports In ASP.NET

This example shows how to Create Crystal Reports In ASP.NET 2.0,3.5,4.0 Using C# And VB.NET. I am generating Crystal 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.

Crystal reports in ASP.NET

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
If you like this post than join us or share

78 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


Unknown 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.


Unknown 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


Unknown 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...


Unknown said...

The reportdocument load method is not getting the path of your .rpt file .Please provide complete path of your .rpt file.
I am sure it will work.


Anonymous said...

Good Way of representation ,A beginner can generate report by reading this article
but focus on properties of report
thank you


Anonymous said...

if u need more exaples

VB.NET

http://vb.net-informations.com/crystal-report/vb.net_crystal_reports_tutorials.htm

C#

http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm

tks.


Anonymous said...

Thanks Dear for your Ernest effort to make it understandable.I am a beginner and it help me a lot.

Thanks again.

Pankaj Gupta


Anonymous said...

Logon failed. Details: crdb_adoplus : Object reference not set to an instance of an object. Error in File C:\DOCUME~1\SCHOWD~1\LOCALS~1\Temp\CrystalReport1

Hi I am getting this.


Anonymous said...

thanks


Anonymous said...

thanks for crystal report procedure


Anonymous said...

nice aryicle


Bilal Sohail said...

very nice ,,thanx for sharing


Anonymous said...

hi i am asp.net beginner i need to generate report when the user click the submit button
thanks in advance


skpraju said...

making blank crystal report from dataset using asp.net with c# .already i worked but i get the error like Report has no tables
string str = "select * from tempa";
SqlConnection cn = new SqlConnection("server= PENNANTSRV01;database=Practice;user id=sa;password=zxcvbn1@");
SqlDataAdapter da = new SqlDataAdapter(str, cn);
cn.Open();
DataSet dt = new DataSet();
da.Fill(dt,"dfg");
if (dt.Tables[0].Rows.Count > 0)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument crystalReport2;
crystalReport2 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
crystalReport2.Load(Server.MapPath("CrystalReport.rpt"));

//crystalReport2.Database.Tables[0].SetDataSource(dt.Tables[0]);

crystalReport2.SetDataSource(dt.Tables["dfg"]);// Error occured Here

CrystalReportViewer1.ReportSource = crystalReport2;
CrystalReportViewer1.DataBind();
}
cn.Close();


but i don't know how
can any one help me ???

If anyone have solution please mail(skpraju.bh@gmail.com) to me.


Anonymous said...

thanks for posting


Anonymous said...

its a nice article ..thanku


Umesh Chandurkar said...

Hi i m umesh chandurkar and the .net beginer So thanks a lot to share with me


Anonymous said...

hey after writing the code in the pageload method there still it asking for username and password...


Anonymous said...

DataSet dt = new DataSet();
da.Fill(dt,"dfg");
if (dt.Tables[0].Rows.Count > 0)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument crystalReport2;
crystalReport2 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
crystalReport2.Load(Server.MapPath("CrystalReport.rpt"));

//crystalReport2.Database.Tables[0].SetDataSource(dt.Tables[0]);

crystalReport2.SetDataSource(dt.Tables["dfg"]);// Error occured Here

CrystalReportViewer1.ReportSource = crystalReport2;
CrystalReportViewer1.DataBind();
}
cn.Close();

In that change
da.Fill(dt,"dfg"); to da.fill(dt);
crystalReport2.SetDataSource(dt.Tables["dfg"]);
to
crystalReport2.SetDataSource(dt);

It should work


savitha said...

Hi..nice article thanks for sharing...


Anonymous said...

Thank you very much!!!!!!!!!!
It helped me a lot.Appreciate your way of presentation.helps beginers also...


Anonymous said...

good one...


Anonymous said...

good one...


Anonymous said...

good one...


Anonymous said...

Am getting crystal report Version error in web.config


Ashutosh said...

Good enough for beginners...thanx... :)


Bhupal said...

hi skpraju,
do u got solution for ur problem ?
if yes please post answer or mail me the code to movnabbs@gmail.com

thank you


Unknown said...

Dear All
i, need procedure for calling crystal reports 2008 in asp.net3.5
i, am using crystal reports 2008, which is seperately installed, i, have query for that report, but, i, don't know, how to call the CR2008 in asp.net , which contains input fields has to display in report.
example: 2 dates, like fromdate and todate,
one dropdown list box, based on these selection, i, had a query also,, pls send me code for code behind for vb.net , how to call cr2008 and how to pass the input parameters to be display in the report.
if anyone knows kind mail to nalla_gangadhar@yahoo.co.in or nalla.gangadhar@gmail.com as soon as possible.

Thanks in advance

Warm Regards..


Anonymous said...

Sir,
I want to use crystal report in asp.net for bill generation. So I want to arrange fields vertically. How can I do this? Help me?
Thanks
Gopal Sharma,Jaipur


gudiya said...

nice artical


Unknown said...

hello amit..
i want ask u that can it is possible to find database link from Crystal report even u r not admin..if yes then how..

i m explaining again my problem:-
"That is any way to extract data from crystal report.i have different-2 link of reports i want to extract data from it but i m not a admin so What i am doing that going to all link and copy data.
But it is very difficult task so please suggest me any easy solution/way...."

thank you very much


Mahesh Lalwani said...

This is really great info abt crystal reports, it helps me lot to create crystal reports


Anonymous said...

nice


Anonymous said...

Nice article.Thanks for sharing


Unknown said...

hi


saurabh bahl said...

Thanks for sharing....
If you are looking for better reporting software, please take a look at Windward Reports. With Windward you design reports in Microsoft Word, Excel, or PowerPoint so report design is a lot faster and easier - and non-programmers can design reports.


Anonymous said...

hi,
how can i set report data source programttically in your example...


Anonymous said...

nice one....


Anonymous said...

thank for u


Biku said...

thanks friend for your blog. can u let how to pass multiple parameter.
and with where condition....


vijar rana said...

Thanks, it helps me alot


Anonymous said...

Thankyou,


Nipul said...

first we will go for new website and we open a new website.....then

through the solution explorer we do through the ADD NEW ITEM and we seclect SQL SERVER DATABASE and we add that....whose name is like "INVETORY.mdf"...then

we open a server explorer and we add the table in INVETORY.mdf database.....here table name is "LOGIN"....who has a two field......ID and PASSWORD.....then

now we open table LOGIN and SHOW TABLE DATA and we input some data in the table LOGIN....then

agin we go to solution explorer and we add CRYSTAL REPORT through ADD NEW ITEM....

now what i do for i want to create a crystal report for table LOGIN and its DATA mince ID and PASSWORD.....

i hope now you understand.....

please kindly consider my QUESTION and giving me answer....

i m waiting for your answer.....


Nipul said...

first we will go for new website and we open a new website.....then

through the solution explorer we do through the ADD NEW ITEM and we seclect SQL SERVER DATABASE and we add that....whose name is like "INVETORY.mdf"...then

we open a server explorer and we add the table in INVETORY.mdf database.....here table name is "LOGIN"....who has a two field......ID and PASSWORD.....then

now we open table LOGIN and SHOW TABLE DATA and we input some data in the table LOGIN....then

agin we go to solution explorer and we add CRYSTAL REPORT through ADD NEW ITEM....

now what i do for i want to create a crystal report for table LOGIN and its DATA mince ID and PASSWORD.....

i hope now you understand.....

please kindly consider my QUESTION and giving me answer....

i m waiting for your answer.....


Shweta said...

Hiiiii. i m creating a website. For that i have to create a crystal report. Bt i dn't know about it. this is a new topic for me. Plz help me out.


rock said...

using CrystalDecisions.CrystalReports.Engine;

//must be add this namespace


rock said...

i do this same but when i added page_load code
the the grouped fields did not worked properly.

so, please provide me solution immediately.............


Monik Gupta said...

I follow all the steps and my page load succceeded but while running the page it need password while i copy the code behing code on page_load event and when i entered the password it remains on same page n the password textbox become empty after loading.


Unknown said...

@Monik:

Hi monik, you doesn't have too provide password through textbox, just write code i mentioned above in Page_Load event and it will autometically take the password


Anonymous said...

Hi,

good article, can u describe that how to take a print out of data of a web form like... I am updating some data online, at last I want to take a print out of that in a report form including my updation....
thankx in advance...


Anonymous said...

thank you..


Anonymous said...

thanks a lot man...
i was looking for it...


Priya said...

can u plz tell me how to set the page size while creating the crystal report......


Unknown said...

@Priya: you can set the page size by setting the horizontal and vertical rulers bar


Anonymous said...

thanks alot for such a nice post. its greet job.


Anonymous said...

very good for beginners


Anonymous said...

i want to show 58 columns in the crystal report
in asp.net using c# 2008
anyone can help me????????????Pls give me solution as soon as possible i really need it.


Anonymous said...

Thanks great programming


Anonymous said...

hai!frnds...
it's really cool....i have one problem in my table i have two column..bt when i add the table one column only DISPLAY IN INSERT GROUP MESSAGE BOX the crystal report...any one help me?


Unknown said...

@Above: plz reconfigure your report and check whether all the fields are displayed in field explorer ?


Anonymous said...

sir,
please help me , getting error of logon failed..
what to do???


Unknown said...

@Above: Logon failed means you have entered wrong username or password of sql server login in code behind , plz try logging in in sql server management studio with the user name and password u have written in code


Anonymous said...

Excellent!!! But when I use one of the buttons in the CrystalReportViewer toolbar on runtime, like Searchbutton, refreshbutton or ToggleGroupTreeButton the page goes blank (All white). Any ideas why?
Thank you...


Anonymous said...

Thanks,
i got error on ReportDocument but by adding "using CrystalDecisions.CrystalReports.Engine;"
as namespace in code behind it worked .


Anonymous said...

This comment has been removed by a blog administrator.


RAVI ROSHAN said...

Can u please tell me how to generate the reports dynamically (taking inputs from user at run time )in asp.net and coding tutorials in vb.net

Thank you in advance,


asif said...

getting this message evry time please help
Object reference not set to an instance of an object.


asif said...

how can i use search in this?
i want to search ID in crystal report from text box how can i do this,??
and when i use parameter for search ID it is showing
Object reference not set to an instance of an object
please help


Unknown said...

@asif: Please refer Pass Crystal Report Parameters Programmatically To know how to search id and show report


Unknown said...

I have problem that print button ids not working..


Anonymous said...

thanx this code is very useful 4 me.........thnxzzz


Anonymous said...

he have un problem to print this is an erro message:
Error in file {ETAT_BON_DE_COMMANDE 2E8AACC2-77BC-4616-8111-03EE6174224D} rpt.:
Access denied to file report. There may be in use by another program.


Anonymous said...

this is me problem :
Error in file {ETAT_BON_DE_COMMANDE 2E8AACC2-77BC-4616-8111-03EE6174224D} rpt.:
Access denied to file report. There may be in use by another program.


Anonymous said...

super


Find More Articles