Results 1 to 40 of 46

Thread: Crystal Report In VB.NET

Threaded View

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Arrow Crystal Report In VB.NET

    Often user get problem for making the report using Vb.Net so here I am providing the simple code for using the report in the VB.NET
    This code simply on the Form Load event, you can change it according to the button also. You have to add just connection and the query according to your project.
    If you do not want to use the SQL query then you can use the selection formulas also
    If any problem related to this code then post me in The PM do not post here Contact me at the PM or post in the reporting section I will check your problem there.


    Step By Step Crystal Report In VB.NET New Code
    1) Add a from in your project Name CrystalReportForm
    2) Add a panel Set the Dock Property To Fill
    3) Add the Code

    VB.NET Code:
    1. Imports System.Data.OleDb
    2. Public Class CrystalReportForm
    3.  
    4.     Public Function GetDataAdeptor(ByVal QueryString As String) As OleDbDataAdapter
    5.         Dim DataAdapter As New OleDbDataAdapter
    6.         Try
    7.             Dim NewConnection As OleDbConnection = OpenNewConnection()
    8.             DataAdapter = New OleDbDataAdapter(QueryString, NewConnection)
    9.             Return DataAdapter
    10.         Catch ex1 As OleDbException
    11.             Throw New Exception("Error Getting The Table", ex1)
    12.         Catch ex As Exception
    13.             Throw New Exception("Error Getting The DataAdapter", ex)
    14.         End Try
    15.  
    16.     End Function
    17.  
    18.     ''' <summary>
    19.     ''' Open Connection Here
    20.     ''' </summary>
    21.     ''' <returns></returns>
    22.     ''' <remarks></remarks>
    23.     Public Function OpenNewConnection() As OleDbConnection
    24.         Dim NewConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=StartUpPath\Database.mdb;Jet OLEDB:Database Password='ShaktiSinghDulawat';")
    25.         Try
    26.             NewConnection.Open()
    27.             Return NewConnection
    28.         Catch ex As Exception
    29.             Throw (ex)
    30.         End Try
    31.     End Function
    32.  
    33.     ''' <summary>
    34.     ''' 1)Add Windows Form Name CrystalReportForm
    35.     ''' 2)Add A Panel Set Dock Proprty To Fill
    36.     ''' </summary>
    37.     ''' <param name="ReportName">Report Name Contain The Name</param>
    38.     ''' <param name="TableName">Table Name In Array</param>
    39.     ''' <param name="QueryString">Query String In Array</param>
    40.     ''' <param name="Parameter">Parameter If Any</param>
    41.     ''' <remarks></remarks>
    42.     '''
    43.  
    44.     Friend Sub ViewReport(ByVal ReportName As String, ByVal TableName() As String, ByVal QueryString() As String, Optional ByVal [Parameter] As String = "")
    45.         Me.MdiParent = MainForm
    46.         If Not UBound(TableName).Equals(UBound(QueryString)) Then MessageBox.Show("Passed Variable Are Not Correct", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information) : Exit Sub
    47.         Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
    48.         Dim CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer
    49.         CrystalReportViewer.ActiveViewIndex = 0
    50.         CrystalReportViewer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    51.         CrystalReportViewer.DisplayGroupTree = False
    52.         CrystalReportViewer.Dock = System.Windows.Forms.DockStyle.Fill
    53.         CrystalReportViewer.Location = New System.Drawing.Point(0, 0)
    54.         CrystalReportViewer.Name = "CrystalReportViewer"
    55.         Dim Adapter As New OleDb.OleDbDataAdapter
    56.         Dim DataSet As New DataSet
    57.         For I As Integer = 0 To UBound(TableName)
    58.             Adapter = GetDataAdeptor(QueryString(I))
    59.             Adapter.Fill(DataSet, TableName(I))
    60.         Next
    61.        'Report In the report Folder
    62.         Report.Load(Application.StartupPath & "/Report/" & ReportName & "")
    63.         Report.SetDataSource(DataSet)
    64.         If Not [Parameter] = "" Then Report.SetParameterValue(0, [Parameter])
    65.         CrystalReportViewer.ReportSource = Report
    66.         Me.Panel1.Controls.Add(CrystalReportViewer)
    67.     End Sub
    68.  
    69. End Class

    How to Call It
    1) Make a Crystal Report and save it in the Report folder at the application startup Path, Just add the Name of the report as CrystalReport1
    2) Make a new form add a command button

    Call This Function for single Table

    VB.NET Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ReportForm As New CrystalReportForm
    3.         Dim TableName(0) As String
    4.         Dim QueryString(0) As String
    5.         TableName(0) = "TableName" 'Pass The Table That you used in the crystal Report
    6.         QueryString(0) = "SELECT * FROM TableName" ' Pass the Query
    7.         'ReportForm.MdiParent = MainForm 'Pass For Mdi True
    8.         ReportForm.ViewReport("CrystalReport1.rpt", TableName, QueryString, )
    9.         ' You can pass the  Parameter Value It is Optional
    10.         ReportForm.Show()
    11.     End Sub

    For Multiple table Increase the array


    VB.NET Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ReportForm As New CrystalReportForm
    3.         Dim TableName(1) As String
    4.         Dim QueryString(1) As String
    5.         TableName(0) = "Table1"
    6.         TableName(1) = "Table2"
    7.         QueryString(0) = "SELECT * FROM Table1"
    8.         QueryString(1) = "SELECT * FROM Table2"
    9.         ReportForm.MdiParent = MainForm
    10.         'Here I am Passing the value of Parameter
    11.         ReportForm.ViewReport("CrystalReport1.rpt", TableName, QueryString, "UnRegister")
    12.         ReportForm.Show()
    13.     End Sub
    Still I am Making some modification In the Code as it work successfully I will Tell.

    EDIT :14 June 2007
    Add the following reference in your project.

    VB.NET Code:
    1. CrystalDecisions.CrystalReport.Engine
    2. CrystalDecisions.Enterprise.Framework
    3. CrystalDecisions.Enterprise.infoStore
    4. CrystalDecisions.ReportSource
    5. CrystalDecisions.Shared
    6. CrystalDecisions.Web
    7. CrystalDecisions.Windows.Forms

    Code For The MS Access And The SQL Server 2005 Thanks

    Thanks
    Last edited by shakti5385; Jun 25th, 2007 at 08:02 AM. Reason: Add Reference list On 14 June 2007 For running Crystal Report

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width