To Use this Class For Crystal Report. You have to Design one sample Report using Crystal Report Wizard. Once your report is ready . you have to put Crystal Report Viewer into WebForm or Windows form. and Use my class to generate report.
Show All Records into Report To show all records in report. you have use this function of class by passing two paramters.
ByVal CrystalReportViewerObject As Object, ByVal ReportName As String
Once you pass CrystalReportViewerObject that you have placed on Win or web form. and pass report name.
ClsCrystalReport Here i am Writing Code for Class Library. You have to set some basic paramters to use this class, Like Report DirectoryPath, SQL Server login Information.
'' Description: This Class is used to Establish Connection to Crystal Report and Run the Parameterized Report. by passing SQL query and Report name '' Detail Description: To Use this Class, we have to specify some Parameters like SQL data Connection information, Report Directory full path . ''===========================================================
Imports CrystalDecisions.CrystalReports.Engine Imports System.Data.SqlClient Imports System.Data Imports CrystalDecisions.Shared Imports System.Configuration
Public Class ClsCrystalReport
''' ''' EstablishReportConnection: This Function is Call , When Displaying Report With All data from table. ''' ''' ''' ''' we have to pass Crystal Report Viewer and Report Name as a Parameter Public Shared Sub EstablishReportConnection(ByVal objCrystalRptVw As Object, ByVal ReportName As String) Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument objReport.Load(AppFuntions.AppReportDir & ReportName) ApplyLogonToTables(objReport) objCrystalRptVw.ReportSource = objReport End Sub
''' ''' ApplyLogonToTables: This Function Call , When We Apply login to Tables for Report ''' ''' ''' it will take parameter as a ReportDocument to ApplyLogOnto Table ''' we have to specify the SQL Server Connection Information Like SQL Server UserId, Databasename, Servername and Password ''' SQL Password is Optional , if Integrated Security = True ''' ''' Private Shared Sub ApplyLogonToTables(ByVal objReport As ReportDocument) Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo Dim crTables As Tables = objReport.Database.Tables For Each crTable As Table In crTables With ConInfo.ConnectionInfo .ServerName = AppFuntions.AppServerName .DatabaseName = AppFuntions.AppDatabaseName .UserID = AppFuntions.AppDatabaseUserID .IntegratedSecurity = True End With crTable.ApplyLogOnInfo(ConInfo) Next End Sub
''' ''' ShowReport: This Function Call , when we want to Execute Query and Display Data into Report. ''' ''' ''' ''' ''' It will take Three Parameters as CrystalReportViewerObject , SQL Query and Report Name Public Shared Sub ShowReport(ByVal objClsCrystalRptVw As Object, ByVal StrSql As String, ByVal ReportName As String) Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument objReport.Load(AppFuntions.AppReportDir & ReportName) ApplyLogonToTables(objReport) Dim objds As DataSet = ExexcuteSQLQuery(StrSql) objReport.SetDataSource(objds.Tables(0)) objClsCrystalRptVw.ReportSource = objReport End Sub
''' ''' OpenSQLConnection: This Function will open the database Connection with SQL Server according to COnnection String Setting in app.Config file ''' ''' it will return the SQLCOnnection Object ''' Private Shared Function OpenSQLConnection() As SqlConnection Dim objConn As New SqlConnection objConn.ConnectionString = ConfigurationSettings.AppSettings.Get("dbConnection") objConn.Open() Return objConn End Function
''' ''' ExexcuteSQLQuery: This function will Execute the SQL Qeury and Return the DataSet Object ''' ''' ''' it will return the SQL Query Result into DataSet ''' Private Shared Function ExexcuteSQLQuery(ByVal StrSql As String) As DataSet Dim objDA As SqlDataAdapter = New SqlDataAdapter(StrSql, OpenSQLConnection()) Dim objDS As DataSet = New DataSet() objDA.Fill(objDS, "myDataset") Return objDS End Function
End Class
Another Class To Pass basic Paramters LIke Report Dir Path, SQL Server Login Information
Public Class AppFuntions ' All Global and Public Variables Public Shared AppReportDir As String = "c:\Temp" Public Shared AppServerName As String = "localhost" Public Shared AppDatabaseName As String ="TestDB" Public Shared AppDatabaseUserID As String = "sa" Public Shared AppDatabasePassword As String = "sa" End Class
We can Call This two Functions to use this class ' We can call this function on PageLoad, when we want to show report on page load event
Private Sub frmClientReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ClsCrystalReport.EstablishReportConnection(CrystalRptVwObject, ReportName) End Sub
This function we can call on any event like click etc.
Dim strSQL As String Dim StrName As String = textbox1.text strSQL = "select * from TableName where FieldName1=" & StrName ClsCrystalReport.ShowReport(CrystalRptVwObject, strSQL, ReportName) CrystalRptVwObject.Refresh()
Please Contact me, if you are facing any problem to use this class or any kind of help.
|
| Author: virendra vaswani 02 Apr 2007 | Member Level: Bronze Points : 0 |
this page is not working properly. my problem is that i want to report for a employee. the report will depend on empno entered by the user in textbox then only that record should be in report. plz reply
|
| Author: Jatin S Prajapati 04 Apr 2007 | Member Level: Bronze Points : 0 |
its so easy to use this class, you just have to Use the ShowReport Function with Required parameters. and as per your requirements in Emplyee report, you have to just pass the SQL Query as parameters like dim strs
|
| Author: Jatin S Prajapati 04 Apr 2007 | Member Level: Bronze Points : 0 |
its so easy to use this class, you just have to Use the ShowReport Function with Required parameters. and as per your requirements in Emplyee report, you have to just pass the SQL Query as parameters like dim strsql as string = "select * from tablename where empid=" & textbox1.text , thats it
|
| Author: meen meen 18 Nov 2007 | Member Level: Bronze Points : 0 |
Hello, your code is good. i'm quite new to vb.net however, i'm using MS Access Database. How to do it with Access? Help please thanks
|