How to create a Report with Parameters

To begin, create a report that draws its information from the sample Microsoft
Access database that ships with Crystal Reports that ships with Crystal Reports.



Note Xtreme.mdb is the sample database that is provided with Crystal Reports. To
locate xtreme.mdb on your hard drive for your version of Crystal Reports

1. In Solution Explorer, right-click the project name that is in bold type, point to Add, and then click Add New Item.
2. In the Add New Item dialog box, in the Templates view, select the Crystal Report template.
3. In the Name field, enter the name "CustomersByCity.rpt" and click Add.
4. In the Create New Crystal Report Document panel of the Crystal Reports
Gallery dialog box, select Using a Report Wizard.
5. In the Choose an Expert panel, select Standard, and then click OK.
6. In the Available Data Sources panel of the Standard Report Creation Wizard
window, expand the Create New Connection folder.
7. From the subfolder that opens, expand the ODBC (RDO) folder.
8. In the ODBC (RDO) window, select the correct ODBC DSN entry for your version of
Crystal Reports and then click Finish.
The ODBC (RDO) folder expands and shows the Xtreme Sample Database.
9. Expand the Tables node, double-click the Customer table to move the table to the Selected Tables panel, and then click Next.
10. Expand the Customer table, then CTRL-click Customer Name, Contact Title,
Address1, Contact Last Name and City.
11. Click the > symbol to move these fields into the Fields to Display panel, then click the Next button.
12. In the Available Fields panel, under Report Fields, double-click Customer.City to
move the field into the Group By panel, and then click the Finish button.
The CustomersByCity report is created and loaded into the main window of Visual Studio.
You are now ready to add a parameter named City and populate it with default values.
The Field Explorer must be visible, because it provides access to the various features of
the report, including parameters. To make the Field Explorer visible, from the Crystal
Reports menu, click Field Explorer.

To add a City parameter

1. If the Field Explorer is not visible, on the Crystal Reports toolbar, click Toggle Field View.

Note Another way to display the Field Explorer is to go to the Crystal
Reports menu, and then click Field Explorer.
2. In the Field Explorer, right-click Parameter Fields and select New...
3. In the Create Parameter Field dialog box:
Set the Name to "City."
Set the Prompting Text to "Select one or more cities."
Set Value Type to String
Select Allow multiple values.
Select Discrete value(s).
Click Default Values...
4. In the Set Default Values dialog box:
Set the Browse table to "Customer."
Set the Browse field to "City."
Click >> (the double right arrow) to move the entire cities list into the Default Values list.
5. Click OK to close the Set Default Values dialog box.
6. Click OK to close the Create Parameter Field dialog box.
You have just set the Default Values to contain a large list of cities.

You now use the Select Expert to set a formula that connects the City database column to
your newly created City parameter field.

To connect the City parameter to the City database column

1. On the Crystal Reports toolbar, click Select Expert.
2. In the Choose Field dialog box, under Report Fields, select Customer.City, and
then click OK.
3. In the Select Expert dialog box, within the Customer.City tab, set the dropdown list
to "is equal to."
4. In the new dropdown list that appears to the right, select the first choice in the list,
{?City}, and then click OK.

Note This selection, {?City}, is the City parameter that you created earlier.

5. From the File menu, select Save All.


You are now ready to bind the report to the CrystalReportViewer control and set the city
parameter with two initial values, Paris and Tokyo.

Binding the Report

It is assumed that you have already placed a CrystalReportViewer control on the Web or Windows Form. In the previous steps, you added a CustomersByCity report to the project.
In this section, you bind the file directory path of the CustomersByCity report to the
CrystalReportViewer control. Then you test whether the report displays correctly when
current values have not been set for its parameter field.

To bind the file directory path of the CustomersByCity report to the
CrystalReportViewer control

1. Open the Web or Windows Form.
2. From the View menu, click Code to view the code-behind class for this Web or Windows Form. 3. Locate the Form Load method (Or any other method where you want to place the code).
4. Declare a string variable, name it reportPath, and assign to it a runtime path to the
local report. This path is determined differently for Web Sites and Windows projects:
For a Web Site, pass the name of the local report file as a string parameter into the
Server.MapPath() method. This maps the local report to the hard drive file
directory path at runtime.


[C#]
string reportPath = Server.MapPath("CustomersByCity.rpt");

[Visual Basic]
Dim reportPath As String = Server.MapPath("CustomersByCity.rpt")

For a Windows project, concatenate the Application.StartupPath property with
a backslash and the local report file name. This maps the report to the same
directory as the Windows executable file.

Note At compile time you will copy the report to the directory containing
the executable file.


[C#]
string reportPath = Application.StartupPath + "\\" +
"CustomersByCity.rpt";

[Visual Basic]
Dim reportPath As String = Application.StartupPath & "\" & "CustomersByCity.rpt"

5. Assign the file directory path of the CustomersByCity report to the ReportSource
property of the CrystalReportViewer control.

[C#]
crystalReportViewer.ReportSource = reportPath;

[Visual Basic]
myCrystalReportViewer.ReportSource = reportPath

You are now ready to build and run your project. It is expected that the report loading will fail, because code has not yet been written to set a value for the City parameter field.

To test the loading of the CustomersByCity report

1. From the Build menu, select Build Solution.
2. If you have any build errors, go ahead and fix them now.
3. If you use a non-embedded report in a Windows project, locate the compiled Windows
executable in the \bin\debug\ subdirectory, and then
copy the report to that subdirectory.

Note To have the non-embedded report loaded by the Windows executable
at runtime, the report must be stored in the same directory as the Windows
executable.

4. From the Debug menu, click Start.

Note If you are developing a Web Site in Visual Studio 2005, and this is the
first time you have started debugging, a dialog box appears and states that
the Web.config file must be modified. Click the OK button to enable
debugging.


The CustomersByCity report does not display. It displays after you add a value for the
City parameter field later in this tutorial.

Note Results may vary, depending on the version of Crystal Reports that
you use. In more recent versions, you can see a form requesting that you
provide parameter values for that report. In earlier versions, a "Missing
parameter field current value" exception is thrown. In either case, you must
add further code to create a fully functional application.

5. Return to Visual Studio and click Stop to exit from debug mode.

How to create Crystal Report

Start your first VB.NET Crystal Reports .
All Crystal Reports programming samples in this tutorials is based on the following database (crystaldb) . Please take a look at the database structure before you start this tutorial - Click here to see Database Structure .
Open Visual Studio .NET and select a new Visual Basic .NET Project.




Create a new Crystal Reports for Product table from the above database crystalDB. The Product Table has three fields (Product_id,Product_name,Product_price) and we are showing the whole table data in the Crystal Reports.
From main menu in Visual Studio select PROJECT-->Add New Item . Then Add New Item dialogue will appear and select Crystal Reports from the dialogue box.






Select Report type from Crystal Reports gallery.

Accept the default settings and click OK.
Next step is to select the appropriate connection to your database. Here we are going to select OLEDB connection for SQL Server
Select OLE DB (ADO) from Create New Connection .



Select Microsoft OLE DB Provider for SQL Server .




Next screen is the SQL Server authentication screen . Select your Sql Server name , enter userid , password and select your Database Name . Click next , Then the screen shows OLE DB Property values , leave it as it is , and click finish.
Then you will get your Server name under OLEDB Connection from there select database name (Crystaldb) and click the tables , then you can see all your tables from your database.
From the tables list select Product table to the right side list .




Click Next Button
Select all fields from Product table to the right side list .

Click Finish Button. Then you can see the Crystal Reports designer window . You can arrange the design according your requirements. Your screen look like the following picture.







Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET through Crystal Reports Viewer control .
Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewer control to your form.



Select Form's source code view and put the code on top
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
VB.NET Source Code

Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As
System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class
NOTES: cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") The Crystal Reports is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here. After you run the source code you will get the report like this.









Hope this tutorial help you to create your first Crystal Reports.


How to load Crystal Reports dynamical in .Net

All Crystal Reports programming samples in this tutorials is based on the following database (crystaldb) . Please take a look at the database structure before you start this tutorial - Structure of the database are as follows:



First we have to create a database . Give the database name as "crystaldb"
Create a DataBase "crystaldb"
In the crystaldb database , create three tables
OrderMaster , OrderDetails , Product .

OrderMaster
OrderMaster_id
OrderMaster_date
OrderMaster_customer
OrderMaster_createduser
OrderDetails
OrderDetails_id
OrderDetails_masterid
OrderDetails_productid
OrderDetails_qty
Product
Product_id
Product_name
Product_price
The following picture shows the relations of each table :



In this section we are passing User ID , Password , Server Name and Database Name dynamically to Crystal Reports from vb.net .
SITUATIONS :
In many situations this is useful , for example if you develop a database project in a test server and later you have to migrate it , in such situations you have to give these information dynamically to Crystal Reports.
In this program we are using our earlier program and pass the values dynamically to Crystal Reports. Before we start this section take a look at the How to create Crystal Report in VB.NET .
In the How to create Crystal Report we created a report selecting all data from the Product table . There is no chance to change the server on runtime in that case because it is a static report . Here we are passing Server Name , UserID and Password dynamically to the Crystal Reports.
Here we use Crystal Reports ConnectionInfo .
Dim crConnectionInfo As New ConnectionInfo , and pass these arguments to ConnectionInfo
Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewer control to your form.
Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine
Imports
CrystalDecisions.Shared
Put the following source code in the button click
event
VB.NET Source Code
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub
Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Handles Button1.Click
Dim cryRpt As New ReportDocument
Dim
crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New
TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables
As Tables
Dim CrTable As Table
cryRpt.Load("PUT CRYSTAL REPORT PATH
HERE\CrystalReport1.rpt")
With crConnectionInfo
.ServerName = "YOUR
SERVER NAME"
.DatabaseName = "YOUR DATABASE NAME"
.UserID = "YOUR
DATABASE USERNAME"
.Password = "YOUR DATABASE PASSWORD"
End With
CrTables = cryRpt.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo =
crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class






NOTES: cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here. You have to replace the source code values of "YOUR SERVER NAME" , "YOUR DATABASE NAME" , "YOUR DATABASE USERNAME" , "YOUR DATABASE PASSWORD" with your correct values . When you run this program you will get the following screen.