Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Crystal Reports » Introductions to Crystal Reports in .NET

Introductions to Crystal Reports in .NET

There have been many enquiries about Crystal Reports on the site. Here i will show you how to create a simple report using Crystal Reports and Visual Studio .NET.

Author Rank:
Technologies: Crystal Reports,Visual C# .NET
Total downloads : 2275
Total page views :  238106
Rating :
 4.19/5
This article has been rated :  26 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
IntroToCR.zip
 
Become a Sponsor




There have been many enquiries about Crystal Reports on the site and unfortunately there are not too many technical articles and source code samples available on Crystal Reports on C# Corner or any where else. As usual, I decided to dedicate some time to Crystal Reports and fill the Crystal Reports section of C# Corner and VB.NET Heaven sites. As a result, in next couple of weeks you will see some Crystal Reports articles from me. Hopefully, you will enjoy these articles. Again, I encourage you to share your knowledge with other developers to make our lives easier.

Unlike previous versions of Visual Studio, Crystal Reports is an integral part of Visual Studio .NET and ships as a part of it. By making Crystal Reports a part of Visual Studio .NET suite, Microsoft has added one more useful tool to the Visual Studio family. In this article, I will show you how to create a simple report using Crystal Reports and Visual Studio .NET. In my following article, I shall dig more into Crystal Reports .NET.

To follow this article, first create a Windows application by selecting File->New->Project->Visual C# Projects->Windows Application template from Visual Studio .NET project templates. I give my project name IntroToCR.

Adding a Report to a Project

Crystal Report is a part of Add New Item templates available in Visual Studio. To add a report to the project, add a new item by Right Clicking on the project in Solution Explorer and selecting Add->Add New Item->Crystal Report as you can see in Figure 1. I change the name of the report to Customers because we will be displaying customers related information from Northwind database.

Now click the Open button.

Figure 1. Adding a Report to the Project

The next step is to select the report document type from Crystal Report Gallery. As you can see from Figure 2, Crystal Report Gallery provides three options Using the Report Expert, As a Blank Report, or From an Existing Report.

For our first application, lets keep all options as default. I will be discussing rest of the options in my following articles.

Figure 2. Selecting a report type.

By clicking the OK button adds Customers.rpt file to the project and launches Standard Report Expert wizard as you can see in Figure 3, where you can select a data source. The Standard Report Expert wizard has 8 different tabs. I will discuss these tabs in more details in my following articles. The main purpose of this article is to show you how to create your first report in a few simple and quick steps.

Figure 3. Standard Report Expert wizard

Adding a Data Source

Now our next step is to link a data source to the wizard. There are different ways to link a data source to the wizard. The simplest way is to click on the OLE DB(ADO) tree item in Figure 3.

Clicking on OLE DB(ADO) item launches OLE DB Provider dialog where you can select a data source as you can see in Figure 4.

Figure 4. OLE DB Provider dialog

Now on the OLE DB Provider, you can select a provider. I select Microsoft OLE DB Provider for SQL Server. If you do not have access to SQL Server, you can select Access or other data providers.

After select Microsoft OLD DB Provider for SQL Server, you will see the Next button is activated on the wizard.

The Next button allows you to provide the connection information about a data source. As you can see in Figure 5, we select Northwind database.

Figure 5. Connection Information dialog

Now you can click the Finish button.

This option adds the Northwind database to the list and if you expand the database item, you will see the database tables.

Now on this page, I select Customers table and add it by clicking Insert Table button. Now you can see the table listed in the right list box. See Figure 6.

Figure 6. Northwind database tables

Now click on Next button which moves to the Fields tab. On this page, you can select what field you want to use in the report. For simplicity, I add only 5 columns to the report CustomerId, CompanyName, ContactName, ContactTitle, and City. See Figure 7. On this page, you can change the heading of a column by selecting a column and changing its name in the Column Heading text box.

Figure 7. Adding columns to report

Now we can continue through different pages but we will stop here. Click Finish here. This option adds the report with all columns to the project. The final report file looks like Figure 8.

Figure 8. Customers.rpt

Adding a Crystal Report Viewer Control

Now our next step is to add Customers.rpt to the Form. For this, we need to make sure the size of the Form is big enough to fit the report. So you may want to go ahead and modify the size of Form.

After resizing the Form, we need to add Crystal Report Viewer from Toolbox to the Form. Simply drag the Crystal Report Viewer control from Toolbox to the Form and resize it accordingly. My final Form looks like Figure 9.

Figure 9. Form with Crystal Report Viewer

Attaching Report to Crystal Report Viewer Control

The last step is to attach Customers.rpt to Crystal Report Viewer control. For this, we need to set the ReportSource property of Crystal Report Viewer control. On the Forms load event handler, we create an Instance of Customers class, and set it as the ReportSource property of Crystal Report Viewer control.

Just write the following two bold lines on your Forms load event handler.

private void Form1_Load(object sender, System.EventArgs e)
{
Customers custReport =
new Customers();
crystalReportViewer1.ReportSource = custReport;
}

Now compile and run the project. The output looks like Figure 10. Now you can see the report with data. With the help of Crystal Report Viewer, you can select from various options such as zoom in, zoom out, print, move next, move last, move previous, and move first.

Figure 10. The final report

Conclusion

By adding Crystal Report support to Visual Studio .NET, Microsoft had made developers like easier. In this article, we saw how easy it is to generate a report from a SQL Server database using Crystal Report available in Visual Studio 2003.

In my next articles in this series, I will be exploring more options of Crystal Reports.

Note: To run the attached source code, you MUST change the database information.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDFâ„¢ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
IntroToCR.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Crystal Reports by vasundhara On March 19, 2007
How to create customized crystal reports in C#
Reply | Email | Delete | Modify | 
Re: Crystal Reports by Mahesh On March 20, 2007
Vasundhara,
What do you mean by customized? You use Crystal Report to create all kind of reports. Visual Studio has just limited support of CR.
Reply | Email | Delete | Modify | 
Article by vasundhara On March 19, 2007
By this article iam clear about creating crystal reports
Reply | Email | Delete | Modify | 
Re: Article by Mahesh On March 20, 2007

Glad, it was helpful.

Reply | Email | Delete | Modify | 
Dynamic Crystal report by miscaccountrec On April 9, 2007
I have a XML file specified as Database File for a Crystal report. The problem is, the XML is having only 2 columns. But i want a report wherein the columns might get added or deleted based on the condition(selection of some attrbiutes). Please let me know how I can go about this. Also kindly tel me whether my approch of using the XML file is correct or not.
Reply | Email | Delete | Modify | 
Dynamic Crystal report by miscaccountrec On April 9, 2007
I have a XML file specified as Database File for a Crystal report. The problem is, the XML is having only 2 columns. But i want a report wherein the columns might get added or deleted based on the condition(selection of some attrbiutes). Please let me know how I can go about this. Also kindly tel me whether my approch of using the XML file is correct or not.
Reply | Email | Delete | Modify | 
Re: Dynamic Crystal report by Mahesh On April 9, 2007
The way I would deal with it is create an XML file with maximum number of columns. After that, you can pass parameters to the report from your code and show or hide or suppress the columns you do not wish to see in the report.
Reply | Email | Delete | Modify | 
Re: Re: Dynamic Crystal report by miscaccountrec On April 11, 2007
thanks for the suggestion
Reply | Email | Delete | Modify | 
Help by Uwayo On April 9, 2007
Hi ive made a report using Crystal report viewer and crystal report tool and my Database is Oracle 9i but the problem is that before it shows the report it asks the Database login first and i dont want my users to know the Oracle password .wat can i do? Please Help me
Reply | Email | Delete | Modify | 
Help by Uwayo On April 9, 2007
Hi ive made a report using Crystal report viewer and crystal report tool and my Database is Oracle 9i but the problem is that before it shows the report it asks the Database login first and i dont want my users to know the Oracle password .wat can i do? Please Help me
Reply | Email | Delete | Modify | 
Re: Help by Mahesh On April 11, 2007

I think Crystal Reports section of this site has an article, which shows you to pass SQL Server user id and password programatically. Check out all articles on Crystal Reports section.

 

Reply | Email | Delete | Modify | 
supressing columns dynamically at run time by Arijit On April 11, 2007
Please discuus elaborately about supressing the columns in report in accordance with the need
Reply | Email | Delete | Modify | 
Re: supressing columns dynamically at run time by Mahesh On April 11, 2007
You have to use Formula editor next to Suppress to suppress a field dynamically. Formula will return True for supress and False for not suppress.
Reply | Email | Delete | Modify | 
CrystaViewer Control by miscaccountrec On April 11, 2007
I am trying to do this : paramField.set_ParameterFieldName("test"); But I get the following error : 'CrystalDecisions.Shared.ParameterField.ParameterFieldName.set': cannot explicitly call operator or accessor Please let me know what is the meaning of this and how can i rectify.
Reply | Email | Delete | Modify | 
CrystaViewer Control by miscaccountrec On April 11, 2007
I am trying to do this : paramField.set_ParameterFieldName("test"); But I get the following error : 'CrystalDecisions.Shared.ParameterField.ParameterFieldName.set': cannot explicitly call operator or accessor Please let me know what is the meaning of this and how can i rectify.
Reply | Email | Delete | Modify | 
Re: CrystaViewer Control by Mahesh On April 11, 2007

try paramField.ParameterFieldName = "test";

 

Reply | Email | Delete | Modify | 
Re: Re: CrystaViewer Control by Emmanuel On October 8, 2008

the property "paramField.ParameterFieldName" does not exist. I read from other forums that I should try "paramField.Name", but the "Name" property does not exist as well, only "paramField.set_ParameterFieldName".

 

When I type in "paramField.set_ParameterFieldName("test"), it's giving me this error:

'CrystalDecisions.Shared.ParameterField.ParameterFieldName.set': cannot explicitly call operator or accessor

I've been stucked in this for days. Please help! I am using Visual Studio 2003, btw.

 

Thank you so much..

 

 

Reply | Email | Delete | Modify | 
Crystal Reports by Mitesh On April 23, 2007
How do i pass the Data need to run a crystal report from another web page?
Reply | Email | Delete | Modify | 
Re: Crystal Reports by Mahesh On April 24, 2007

If you have data on Page1 and report is being generated on Page2, you can store data in a Session variable and get it from session on Page2 and pass it to the report.

For example, you can store data in session on Page1:

Session("ReportData") = DataObject

On Page2, you can retrieve data back from Session like this:

DataObject = Session("ReportData")

You will need to convert object types.

Reply | Email | Delete | Modify | 
How to Suppress Parameter Dialog in Crystal Reports.net ? by Bhuvan On June 1, 2007
I'm migrating my application from VB6 to VB.Net 2005 during which Crystal reports is being upgraded to CRX!-R2 with in VS2005. I have suppress the Parameter Prompt as the application manages the parameters and is not transparent to users. This was done by setting the property oReport.EnableParameterPrompting = False '(In VB6) I'm trying to achieve equivalent in CR.Net I could not figure out how to Achieve this. Some one please help me. thanks in advance.
Reply | Email | Delete | Modify | 
Crystal report by Zainab On July 8, 2007
i did a crystal report how can i edit it?
Reply | Email | Delete | Modify | 
Re: Crystal report by Mahesh On July 11, 2007
To Edit it, you have to open it in Visual Studio or Crystal Reports designers. Double click on the .rpt file and it should open it is designer. After that, you can edit it.
Reply | Email | Delete | Modify | 
Compatibility between .NET 2003 and crystal reports 11.0 by Habib On July 11, 2007
My VC++ app. developed in .NET2003 works well with crystal reports version 8.0. After I migrated to ver 11, the application does not run properly. It totally fails to create the reports. Is there compatibility issue with using .NET2003 & crystal reports 11 ? What is the least painful workaround ?
Reply | Email | Delete | Modify | 
Re: Compatibility between .NET 2003 and crystal reports 11.0 by Mahesh On July 12, 2007

I know if you develop your application in a different version of CR and you do not have same CR DLLs on the machine, there are many problems. Best bet for you would be re-create/open your reports in CR11 and add them back to VS project. This is just a guess. I hope it works ;)

Reply | Email | Delete | Modify | 
Re: Compatibility between .NET 2003 and crystal reports 11.0 by vijayasree On December 13, 2008
I am getting similar kind of error. I am not able to work with previous versions of crystal reports in visual studio integrated environment. In order not to change users machine, what should i do? I appreciate your help much. Thanks.
Reply | Email | Delete | Modify | 
Crystal Report 8.0 with VB.NET 2005 Microsoft Access/SQL 2005 Database by JAGDISHCHANDRA On August 3, 2007
Dear Mahesh, I am glad to see your helping atitutde. My email is jhpatel1951@gmail.com I am using Crystal Report 8.0 with my Single user Windowa Application witth VB.NET 2005 Microsoft Access/SQL 2005 Database. At Design Time I am getting View Report with some unknown data. My Dataset is ok. My Data connection is Ok. My data varification is OK. At Run Time I am detting only column heading which is Text. I am not getting any Data at all. Pl. guide me. Waiting for your response. Regards from Jagdish Patel Mobile - 0091 98256 25965
Reply | Email | Delete | Modify | 
Name space con not be found by John On August 27, 2007
I have a report created in Crystal 8 and a C# project. I open the report in Crystal 11 and save it. Then add it to my .net project. When I try to run it I get "Namespace "Report" can not be found. If I leave the report in Version 8 the report at least tries to open but I get a connection error. Can you help please?
Reply | Email | Delete | Modify | 
Re: Name space con not be found by vijayasree On December 13, 2008
Did you get the solution? If so, please share with us. I have problem working with crystal reports created in version 9 in visual studio IDE. I am able to open the file but I am getting runtime errors. I appreciate your help much.
Reply | Email | Delete | Modify | 
How i generate report by Mohammad On August 30, 2007
Hi;i read your mail its very helpful but sir still i am unable to geneate report.please help me hoe i code,which class or namespaces should i add.if you send me some piece of code that will be very help ful for me; Anees..!
Reply | Email | Delete | Modify | 
CR by Anuradha On September 2, 2007
Explain About ADO,RDO& DAO I m confused abt this let me know the difference Thanks
Reply | Email | Delete | Modify | 
Re: CR by Mahesh On September 4, 2007
ADO, RDO, and DAO are older technologies to connect to a database. In C# and .NET, new technology is called ADO.NET. Post your non article related questions on Forums by clicking on forums on the header.
Reply | Email | Delete | Modify | 
Heading for a cross tab by sree On September 16, 2007
Hi, i have a cross tab in  my report.The first column, which shows the checknames from the database.I need to show a heading for that column.Usually in cross tab there is no heading for the first column.I need immediate help.
Reply | Email | Delete | Modify | 
Sql Queries in Crystal report by Sreejith On October 31, 2007

Hi, I am new to Crystal report.

I have a crystal report which has a chart displays data from Database based on the date seleted from the form. How can I give an sqlquery, so that I get my chart in desired format. Could some one please help me in this.

Thanks in Advance,

Sreejith Manjalavil

Reply | Email | Delete | Modify |