Click here to Skip to main content
13,898,354 members
Click here to Skip to main content
Add your own
alternative version

Stats

156.4K views
15 bookmarked
Posted 12 Dec 2013
Licenced CPOL

Beginners tutorial on how to load Crystal Reports using Crystal Report Viewer in Visual Studio 2012

, 12 Dec 2013
Rate this:
Please Sign up or sign in to vote.
Simple article to load a crystal report rpt file using crystalreportviewer in Visual Studio 2012

Introduction

Loading a report using C# is not much of a big deal. But as far as beginners are concerned, it's a task to be dealt with. I had trouble once trying to get this on as I was working on Crystal Reports and Visual Studio.

This tutorial is basically for beginners who are having trouble implementing Crystal Reports in Visual Studio 2012.

Background

I am a beginner in C# and couldn't find much help the first day of Google-ing. May be I am not a good Google-er :)

OK, let me come to the point now. The second day, I found some references and I realized the problems I was facing.

My problem was: Crystal Reports for Visual Studio was necessary and I only had the Crystal Reports re-distributable for Visual Studio. With that installed, I was trying to find the Crystal Reports Viewer in my toolbox, and I realized later from my references that the re-distributable is not enough on a development PC.

Using the Code

First you should download the developer version of Crystal Reports for Visual Studio 2012 from the below link:

Next, add the below namespaces in your project:

using System.Data.Odbc;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

After downloading Crystal Reports from the above link, install it in your PC. After the installation is complete, when you open the toolbox you will notice that more controls are added in the toolbox and you can find the Crystal Reports Viewer in the toolbox. Drag and drop the Crystal Reports Viewer into your form.

Add a button in your project and insert the below code in your button click event:

string test = "select * from tablename";
DataSet testds = new DataSet();
SqlConnection cnn = new SqlConnection("user id=username;password=pwd;server" + 
  "=dataserver;Trusted_Connection=false;database=dbname;connection timeout=30");
SqlCommand testcmd = new SqlCommand(test, cnn);
testcmd.CommandType = CommandType.Text;
SqlDataAdapter testda = new SqlDataAdapter(testcmd);
testda.Fill(testds, "testttable");
cnn.Open();
ReportDocument myReportDocument;
myReportDocument = new ReportDocument();
myReportDocument.Load(@"D:\Reports\rptitemintrans.rpt");
myReportDocument.SetDataSource(testds);
myReportDocument.SetDatabaseLogon("username", "pwd");
crystalReportViewer1.ReportSource = myReportDocument;
crystalReportViewer1.DisplayToolbar = true;

I assume you are familiar with the SQL connection string and how to fill the datasets. Open your connection and load the report. Bang! Done....

Oops..  That was so fast.

Now the code in detail:

For you to load the Crystal Reports report in the Crystal Reports Viewer, you need a ReportDocument, which in my case as per the code is myReportDocument. ReportDocument acts as the report and loads in the Crystal Reports Viewer.

I created and initiated the ReportDocument here as:

ReportDocument myReportDocument;
myReportDocument = new ReportDocument();

Mention the path of the report to be loaded by the ReportDocument:

myReportDocument.Load(@"D:\Reports\rptitemintrans.rpt");

Set the datasource of the ReportDocument:

myReportDocument.SetDataSource(testds);

Set the database logon by giving the database username and password for the ReportDocument. Now this step is very important because without this code when you run the application, you will be prompted to enter the username and password to fetch data from the SQL Server.

myReportDocument.SetDatabaseLogon("username", "pwd");

Finally set the report source to the Crystal Reports Viewer and if you want the toolbar to appear in the viewer, set it to true:

crystalReportViewer1.ReportSource = myReportDocument;
crystalReportViewer1.DisplayToolbar = true;

Last but not the least, add the below code to your app.config file:

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>>

Points of Interest

Well, all done here. I had to spend a day trying to find out how to load Crystal Reports in Visual Studio. But now with this tutorial I guess a beginner should be able to easily get through the hurdle.

Good day to all !!! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rahul Krishnan R
Network Administrator
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
csharpbd25-Aug-16 5:01
professionalcsharpbd25-Aug-16 5:01 
Questionprint particular rows Pin
Member 118893697-Aug-15 20:55
memberMember 118893697-Aug-15 20:55 
QuestionThanks Pin
Member 1184341017-Jul-15 1:05
memberMember 1184341017-Jul-15 1:05 
QuestionNo controls in the toolbox Pin
Manju052022-May-15 16:47
memberManju052022-May-15 16:47 
Bugload report Pin
Member 114522999-Apr-15 3:40
memberMember 114522999-Apr-15 3:40 
Questioncrystal report Pin
Member 952783726-Dec-14 4:59
memberMember 952783726-Dec-14 4:59 
QuestionError Occured Pin
Member 1109125728-Sep-14 21:32
memberMember 1109125728-Sep-14 21:32 
AnswerRe: Error Occured Pin
wozzarvl15-Dec-14 3:28
memberwozzarvl15-Dec-14 3:28 
QuestionGetting error. Pin
SandeepKushwah19-Sep-14 2:43
memberSandeepKushwah19-Sep-14 2:43 
Questionrpt-file Pin
Member 1067668111-Sep-14 23:11
memberMember 1067668111-Sep-14 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 12 Dec 2013
Article Copyright 2013 by Rahul Krishnan R
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid