Partial Class ViewReport
Inherits System.Web.UI.Page
'Crystal report configuration code needs to be called earlier, during the Page.Init event.
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ConfigureCrystalReports()
End Sub
'The ConfigureCrystalReports() method enables users to interact with the report at runtime.
' It also controls programmatic interaction with the report.
Private Sub ConfigureCrystalReports()
Dim CRReport As ReportDocument
CRReport = New ReportDocument
Dim reportPath As String = Server.MapPath("Reports/ProductNutritional.rpt")
'Loads a new report. If a report is already loaded, then it is closed and a new one is opened.
CRReport.Load(reportPath)
'Don't allow the user to enter the login information
CReportViewer.EnableDatabaseLogonPrompt = False
'Set up an object to use the connecton info
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
Dim sName As String = System.Configuration.ConfigurationManager.ConnectionStrings("ItemMasterConnectionString").Name
'Set the DatabaseName, UserID, and Password properties of the ConnectionInfo instance.
myConnectionInfo.ServerName = "myserver"
myConnectionInfo.DatabaseName = "ItemMaster"
myConnectionInfo.UserID = "me"
myConnectionInfo.Password = "mypassword"
'Enter a call to the SetDBLogonForReport() method, by passing in the ConnectionInfo instance and the NorthwindCustomers report.
SetDBLogonForReport(myConnectionInfo, CRReport)
CRReport.SetParameterValue("parmItemNumber", "72012345")
CReportViewer.ReportSource = CRReport
End Sub
Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
'**************************************************************************
'Copied from: [URL unfurl="true"]http://msdn2.microsoft.com/en-us/library/ms227783(VS.80).aspx[/URL]
'**************************************************************************
'Tables is an indexed class that contains instances of the Table class.
Dim myTables As Tables = myReportDocument.Database.Tables
'Create a foreach loop that loops through each Table instance in the Tables indexed class instance.
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
'Within the foreach loop, retrieve the TableLogonInfo instance from the LogOnInfo property of the Table instance.
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
'Within the foreach loop, set the ConnectionInfo property of TableLogonInfo to the ConnectionInfo parameter.
myTableLogonInfo.ConnectionInfo = myConnectionInfo
'Within the foreach loop, pass the TableLogonInfo instance as a parameter to the ApplyLogonInfo method of the Table instance.
myTable.ApplyLogOnInfo(myTableLogonInfo)
Next
End Sub
End Class