Below is the tutorial on how to use Crystal Report in Visual Studio 2008. The following sample is based on the Northwind SQL Server database (http://northwinddatabase.codeplex.com).

Before, we start with the report, you have to make sure that the data that need to be extracted are available. In order to do that, you have to create a DataSet.

Go to Data Sources windows, and click Add New Data Source. (Alternatively, within Visual Studio menu, find Data >> Add New Data Source)



The following window will appear:


Choose Database and click Next.

Click New Connection Button and the following windows will appear:

Pick Data source as Microsoft SQL Server Database File (SqlClient) and browse your mdf file (in this case, you need to locate your mdf file within your project i.e. Northwind.mdf). Click OK and then Next.


Click Yes (or No depends on how you would like your project to work with the database) if the above window appear and Next after the following window.


The following window will then appear:


You can either choose the specific tables or you can just tick All like the above image and then press the Finish button. Once done, the selected items will be appear in the Data Sources windows.

Then, within your project, from main menu in Visual Studio select PROJECT >> Add New Item. Add New Item dialogue will appear and select Crystal Reports from the dialogue box.


Select report type from the Crystal Reports gallery.


Accept the default settings and click OK.

Next step is to select the appropriate connection to your database. Since the dataset has been created, you need to pick the History >> Select the DataSet that has been created before. (You can also seeing the connection of the Dataset in Current Connection Folder or you can Create New Connection if there is no items available within History folder).


Pick the table that you want to use and click the >> button:


Click Next and pick the fields that you want.


Pick the group information (optional)
Record Selection (Optional)
Choose your report style.


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.


Now you want the report that have been created before loaded once the project is run. So, you need to put the following codes:

Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rpt As New NorthwindDataSet
        Dim adp As New NorthwindDataSetTableAdapters.EmployeesTableAdapter
        Dim country As New NorthwindDataSet.EmployeesDataTable
        Dim cryRpt As New ReportDocument
        Dim ds As New DataSet()
        adp.Fill(country)
        ds.Tables.Add(country)
        Dim path As String = AppDomain.CurrentDomain.BaseDirectory
        Dim reportPath As String = path.Replace("\bin\Debug\", "\CrystalReport1.r
pt")
      
  cryRpt.Load(reportPath)
        cryRpt.SetDataSource(ds)
        CrystalReportViewer1.ReportSource = cryRpt
        CrystalReportViewer1.Refresh()
    End Sub
End Class

The Crystal Reports is in your project location, there you can see CrystalReport1.rpt

After you run the source code you will get the report like this.


Good Luck!

0 comments

Recent Entries

Recent Comments

Recent Readers