ARTICLE

How to run Crystal Report using Crystal Report Viewer in VS.NET 2005?

Posted by Sushmita Kumari Articles | Visual Basic 2010 January 24, 2006
This example is developed using Visual Studio 2005 and shows how to use Crystal Report Viewer in your applications to generate reports.
Reader Level:

Following example developed in Visual Studio.NET 2005. In the following code we will see that how to use crystal report viewer in .NET.

 

Steps to use the crystal report viewer:-

 

Import the following namespace for sql server configuration settings.

 

Imports System.Data.SqlClient 

'Code to configure the database settings

'Change the server="your server name where your sql server is running"

'database="Default" database "pubs" and sql "userid" and the "password"

    Dim strConnection As String = "server=dtpxp-skumari;database=pubs;uid=sa;pwd=;"

    Dim Connection As New SqlConnection(strConnection)

    Dim strSQL As String = "Select * From authors"

    Dim DA As New SqlDataAdapter(strSQL, Connection)

    Dim DS As New DataSet

'Variable to assign the report name

    Dim strReportName As String 

 

Next we are going to add the .rpt(Report)file. Right click on

 

The Project solution --> Add --> New Item --> .rpt File

 

It will open the following window. Select the Report wizard.

 

111.jpg

 

Specifying the data --> Create new connection --> OLEDB(ADO) --> specify the data provider by following screen.

 

222.jpg

 

 

Go next till the last screen. Select the style of report.

 

Now, after creating the .rpt file we need to write the code to fill the data source.

 

Create a datatable in your dataset. The datatable's name

 

        DA.Fill(DS)

        'Pass the reportname to string variable

        strReportName = "CrystalReport2"

 

        'Get the Report Location

        Dim strReportPath As String = Application.StartupPath & "\" & strReportName & ".rpt"

        'Check file exists

        If Not IO.File.Exists(strReportPath) Then

            Throw (New Exception("Unable to locate report file:" & vbCrLf & strReportPath))

        End If

 

        'Assign the datasource and set the properties for Report viewer

        Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument

 

        rptDocument.Load(strReportPath)

        rptDocument.SetDataSource(DS.Tables(0))

        rptViewer.ShowRefreshButton = False

        rptViewer.ShowCloseButton = False

        rptViewer.ShowGroupTreeButton = False

        rptViewer.ReportSource = rptDocument 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Susmita Kumari, Good Evening, I have the same problem what Mr. Santhose quoted. That is I want to put the "Select Expert" in my crystal reports viewer control. So that my users can filter the records as they want. I am using Visual Studio 2005. Please suggest as possible as. Thanks in advance, Regards, Tajudeen.

Posted by tajudeen noorudeen Nov 28, 2011

hi madam, how can I show a result of sql query in a crystal report.

Posted by mohammed abdullah Jul 05, 2011

but how i get the selected data in the crystal report with the selection formula..

Posted by abhishek sharma Mar 11, 2011

you halp me a lot.. thanks for your code i run the crystal report very well thanks again..

Posted by abhishek sharma Mar 11, 2011

Article seamed stright forward, still I am seeing a blank report.   I want to pass in to the report all of the data as parameters.  In the debugger when I step out of frmReport_Load the blank report is displayed.  Since this is new to me I assume that there should be buttons on the report and there are not, the body is white.

Below is part of my code.



Option Strict Off
Option Explicit On

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

 

Friend Class frmReport
 Inherits System.Windows.Forms.Form
 
 ' g_bTempLabel
 ' frmPackaging.lblRM
 ' frmPackaging.lblReleaseWO
 
 'UPGRADE_ISSUE: BoxLabel object was not upgraded. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6B85A2A7-FE9F-4FBE-AA0C-CF11AC86A305"'
    Dim report8x11 As New BoxLabel ' 8.5" x 11"
 'UPGRADE_ISSUE: BoxLabel4x6 object was not upgraded. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6B85A2A7-FE9F-4FBE-AA0C-CF11AC86A305"'
    Dim report4x6 As New BoxLabel4x6 ' 4" x 6"
 Dim report As Object
 
    Friend WithEvents crViewer2 As New CrystalDecisions.Windows.Forms.CrystalReportViewer


 Private Sub frmReport_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
  
        Dim S As String

        'Dim crViewer1 As New CrystalDecisions.Windows.Forms.CrystalReportViewer '= New CrystalDecisions.Windows.Forms.CrystalReportViewer
        Dim crDoc1 As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        'Dim crDoc1 As New ReportDocument
        Dim pvCollection As New CrystalDecisions.Shared.ParameterValues()
        Dim crParName As New CrystalDecisions.Shared.ParameterValues
        Dim crParValue As New CrystalDecisions.Shared.ParameterDiscreteValue

        'UPGRADE_WARNING: Screen property Screen.MousePointer has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6BA9B8D2-2A32-4B6E-8D36-44949974A5B4"'
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

        S = "C:\Documents and Settings\bmurphy\Desktop\UpGrades\Packing\packingVB8\BoxLabel4x6.rpt"
        crDoc1.Load(S)
        Debug.Print(crDoc1.DataDefinition.ParameterFields.Count)

        S = "fldBoxNumber_barcode"
        crParValue.Value = "BoXnUmBeR_BC"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldBoxNumber_formated"
        crParValue.Value = "BoXnUmBeR_fmt"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldCustomerPN_barcode"
        crParValue.Value = "CuStOmErPN_BC"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldCustomerPN"
        crParValue.Value = "CuStOmErPN"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldCustomerRev"
        crParValue.Value = "cUsToMeRREV"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        'S = "fldCustomerRev_barcode"
        'crParValue.Value = "cUsToMeRREV_barcode"
        'crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        'crParName.Add(crParValue)
        'crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldBoxQty"
        crParValue.Value = "fldBoxQty"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldMeterRev"
        crParValue.Value = "MR"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldRadioRev"
        crParValue.Value = "RR"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldDatePacked"
        crParValue.Value = "1/1/1900"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldDescription"
        crParValue.Value = "fldDescription"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        S = "fldFreeForm"
        crParValue.Value = "fldFreeForm"
        crParName = crDoc1.DataDefinition.ParameterFields(S).CurrentValues
        crParName.Add(crParValue)
        crDoc1.DataDefinition.ParameterFields(S).ApplyCurrentValues(crParName)

        crViewer2.ReportSource = crDoc1

        crViewer2.Show()
        crViewer2.Zoom(50)


    End Sub
 
 'UPGRADE_WARNING: Event frmReport.Resize may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
 Private Sub frmReport_Resize(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Resize
        'CRViewer1.Top = 0
        'CRViewer1.Left = 0
        'CRViewer1.Height = ClientRectangle.Height
        'CRViewer1.Width = ClientRectangle.Width
  
 End Sub
End Class

Posted by Bob Murphy Nov 18, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor