Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Crystal reports example/sample in asp.net

Jan 18, 2012
Introduction:

In this article I will explain how to create crystal reports example in asp.net.


Description:

In Previous posts I explained
how to install crystal reports in visual studio 2010 and how to create rdlc reports using asp.net and pass parameters to rdlc reports using asp.net. Now I will explain how to create basic crystal reports using asp.net. Crystal Report is standard reporting tool for visual studio by using these we can display reports regarding employee details and display charts etc and crystal reports need minimal coding to display result. 

To implement crystal reports first design the table in database and give name UserInfomation

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
Varchar(50)
LastName
varchar(50)
Location
varchar(50)

After completion of table creation enter some dummy data because we need to use that data to populate reports.

Now Open visual studio and create new website after that right click on your website and select Add new item in that select Crystal Report and click Add
 
After that add crystal report then it will prompt Crystal Report Gallery window in that select blank solution and click OK

A blank report will create in our application now click on CrystalReports menu under that select Database under that select Database Expert
 
After click on Database Expert now Database Expert wizard will open in that select Create New Section >> select OLE DB (ADO) >> in that click on + sign of OLE DB (ADO)

Now select Microsoft OLE DB Provider for SQL Server and click Next (Here we can select SQL Native client option also but sometimes during deployment if servers not contains this native client it will throw error).
 
Now enter SQL Server name, username, password and required database and click Next

After enter credentials for your required database click Next then click Finish (Here for my database I didn’t set any credentials for that reason I didn’t enter userid and password details don’t get confused).

After click Finish now our database loaded in OLEDB (ADO) section >> select your database >> select dbo >> select required tables

Now open tables in that select required table and move to selected tables section and click OK

After that Database Fields in Field Explorer populated with our required data table now drag and drop the required fields from data table to reports Details section

Now open your Default.aspx page drag and drop CrystalReportViewer control from Reporting tab.


Now select CrystalReportViewer and click on smart tag in right hand side and Choose new Report Source

Whenever we click on New report source one window will open in that select crystal report for Report Source from the available reports in dropdownlist and click OK.

After assign available report to CrystalReportViewer control check your code that would be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now run your application your report will be like this

In case your report prompt window for UserName and password before we access data in that situation we need to set those details in code behind instead of assign crystal report to CrystalReportViewer control

Drag and drop CrystalReportViewer control click on right side smart tag of your CrystalReportViewer control and uncheck EnableDatabaseLogonPrompt

Our aspx code will be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now Open your code behind file and set database connection settings and assign reports to the control before that first add following namespaces


using System;
using CrystalDecisions.CrystalReports.Engine;
After add namespaces write the following code in page load event

C# code


protected void Page_Load(object sender, EventArgs e)
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("CrystalReport.rpt"));
reportdocument.SetDatabaseLogon("username","password","SureshDasari","MySampleDB");
CrystalReportViewer1.ReportSource = reportdocument;
}
VB.NET Code


Imports CrystalDecisions.CrystalReports.Engine

Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim reportdocument As New ReportDocument()
reportdocument.Load(Server.MapPath("CrystalReport.rpt"))
reportdocument.SetDatabaseLogon("", "", "SureshDasari", "MySampleDB")
CrystalReportViewer1.ReportSource = reportdocument
End Sub
End Class
Now run your application and check your output that would be like this

 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

48 comments :

Pratik said...

No Challenge to this site....This should be the template that every site must follow....

Anonymous said...

really its very useful

Rajendra Pundir said...

g8t example buddy its easy to understand thanks !

Paramita Roy said...

Helped a lot. Thanks

ganesh said...

Its a very good example of crystal report....
Is there a way to create a crystal report that can be used in any remote computer system.

Whenever I try to use my crystal report created in my local pc to remote pc having the same database name and feilds I have to recreate the entire crystal report

darshan said...

I have one doubts
my server name is
DARSHAN-PC\SQLEXPRESS
when i write this as a string i am getting error..please help me how to solve this problem
thanks

rajitha said...

very very very nice excellent

Jeyapriya N said...

how to send sms to mobile..

Anonymous said...

hi suresh from which college you had complited your BE

Dipak

Jay said...

sample project is very help full

thanks

Anonymous said...

wewewewqe

dasu said...
This comment has been removed by the author.
Amar Prasad said...

Thanks SURESH Ji..!!
Nice steps to geneate crystal report

JP.SIVARAJ said...

Hi,

This steps of creation is well also run in the browser during the execution in the editor having MS-Dev. kit IDE. But, after this page upload to IIS, the crystal report viewer could not load properly on client PC. Is there any possible setting in Web.Config .... Pls inform...

Thanks in advance...

JP.SIVARAJ

Anonymous said...

hai.. how can i add my crystal report to templates.. am using crystal report 10...can u help me....

Anonymous said...

thank you sir, it is good example
i have one query
enable databse login prompting
is it when i have SQL Security
not Windows Security
please reply my query
i mean
string server = @"\SQLEXPRESS";
reportdocument.SetDatabaseLogon("", "", server, "CrystalReport");
in this case i have window integrated security
so i don't have user name or password
thank you

Anonymous said...

if you have Windows Security then just remove
reportdocument.SetDatabaseLogon("", "", server, "CrystalReport");

hope it will work...

Anonymous said...

i want to ask Question
my crystal report working fine on local computer
when i uploaded it to server (not published version)
it is give me blank page
please replay me sir
this is my mail
devnet84@yahoo.com
thank you very much

yashwanth said...

It's very useful to me
Thank u very much

Lakshman Rahul Devulapalli said...

Hi Suresh,

I am new to Crystal Reports,
can u please tell me how the Find button works in the report. If i type "a" and click on that find. it is not doing anything and displaying "End of Search". I have even added the OnSearch click event of that. But no use. Please help me.

Somasundarapandian S said...

Hi suresh ,

this is one good example to create Report generations operation . thanks for your guidance.

Lakshman Rahul Devulapalli said...

Hi Suresh,
I am new to crystal reports, we have a crystal report in that find button fuctionality is not working. Can you please help me. it is crystal 10.0.

DLR

Anu said...

HI I USED THE SAME PROCESS BUT UN ABLE TO MOVE TO THE NEXT PAGE AS WELL AS UNABLE TO EXPORT THE REPORT

Anu said...

hi suresh,
thank you your code helped me in parameter passing in crystal reports.
iam having a problem in crystal reports when iam passing the parameter i am un able to move to the next page as well as export.
iam using vs2010

rajen said...

I used the same code but am getting missing logon parameter error and when i enable log on prompt i get an input box to log on in database..... help me! my e-mail is
urban_hippy@hotmail.com

rajen said...

Hi Suresh....
The above error only appears when i move to the second page,the first page displays fine....

Anonymous said...

thanx for this hint..

amit said...

very easy to understand..

rifayee said...

hi suresh can u explain me how to display the selected record in a grid view and print the selected record in a crystal report...

H@R$H said...

I got more help in this your blog....Very fine...easy to understand...I give 1 vote to great site for IT developer....

Atul said...

I think @Suresh ur the best.....ur site is really bombastic......keep itup bro....

Senthil Murugan said...

I'm using a SAP Crystal Reports Web Viewer to display a CR report. It works perfectly fine in a CR Designer preview, however when I view it in a browser (any) the crosstab does not look as expected:

Anonymous said...

Can you tell me how to display images through crystal report in viewer. the images are stored in sql database

ayub said...

Hi Suresh this is good exampl but when i run the application to generate report using Crystelreport it is asking Sql Server Credientials ie Uid,Pwd everi time why ples help me

Balaji Thirunavukarasu said...

hi sir how to add cssStyle in crystal repot. please help me

Anonymous said...

Thanks a lot.
Very simple to understand (Step by Step)

Anonymous said...

Could u pls provide code for how to add button to crystal reportViewer toolbar?

Chitrakant Sahu said...

thanks for step by step explanation

Ravi Jha said...

Hi,
Suppose we have ever growing database. Can we generate crystal report in real time by choosing some parameters like column names from the database?

thanks

Adil shaikh said...

Hi Suresh... Went through your article,found it really useful.Need your help at the start up though.
---------------
When I add a new item (crystal report)to the website,I get a file to my solution explorer with an extension ( .mht ) though it shows a (.rpt) before adding it in the dialogbox where i can rename it and no such gallery to select the template shows.Am I missing out a plugin or something??..

Awaiting your valuable reply :D

Revathy said...

Hi Suresh,

Personally, This article very useful to me.I need a help,how to generate Payslip to more than one employee. Kindly give me some ideas like this article.

Thank u so much for your proper reply

thanks,
Revathy C

Anonymous said...

Hi sir,

This very good and i learn t from this and it is very useful to all like us. so keep it up


thanks.
UMESH KUMAR

Anonymous said...

Grt....Nice example...!
Good job...thanx..!!

Manas Jaiswal said...

thanx u so much...you give a very good example which is easy to use and understand.

arman suryadi said...

hi suresh..
I have a problem when uploading to the server, .. like this
BC30002: Type 'ReportDocument' is not defined.

Dim reportdocument As New ReportDocument()

Anonymous said...

Can we use Query to fetch data from multiple table.
If yes , than how?

beema said...

hi duresh garu i connected database is ado.net dataset but first page is displaying correctly when i ll click for next page it was asking logon failed incorrected parameteres.pls help me
advance thanks

abhilashblog said...

thanks for it. but not display report in web page please help me.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.