Skip to Content
0
Former Member
Apr 06, 2011 at 03:43 AM

Can't override report database and server settings

43 Views

Please help, I can't override database and server settings from the original report.

I'm using CrystalReportViewer Object Model. Please don't present a solution for ReportDocument Object. I need this Object so the Viewstate is retained.

I copied the Microsoft and SAP examples for DBLogon.

When this code executes it still pulls from the built in datasource specified in the Crystal Report regardless of what I set the connectionInfo to.

There also seems to be an intermittent problem where I received 'Invalid Report Path' when myCrystalReportViewer.LogOnInfo is used. But this only occurs sometimes. Most of the time the report runs fine, only pulling from the original source.

I'm using Crystal Reports for Visual Studio 2008. Version 10.5.3700.0 on Windows 7 SP1 & SQL Server 2005

Here is the code

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports System.Collections
Imports System.Web.Configuration

Partial Public Class PAReportsAccounts
    Inherits BasePage

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        checkSessionStat()
        If Not IsPostBack Then
            If Session.Contents("reportPath") IsNot Nothing Then
                Session.Contents.Remove("reportPath")
            End If
            Dim myReportPath As String = Server.MapPath("PAGlobalReports/")
            Dim reports() As String = Directory.GetFiles(myReportPath, "*.rpt")
            Dim mySortedList As IDictionary = New SortedList
            For Each path As String In reports
                Dim reportNamePrefix As Integer = path.LastIndexOf("\") + 1
                Dim reportNameLength As Integer = path.Length - reportNamePrefix
                Dim reportName As String = path.Substring(reportNamePrefix, reportNameLength)
                mySortedList.Add(path, reportName)
            Next
            reportsList.DataTextField = "value"
            reportsList.DataValueField = "key"
            reportsList.DataSource = mySortedList
            reportsList.DataBind()
        Else
            ConfigureCrystalReports()
        End If
    End Sub

    Protected Sub display_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles display.Click
        Dim reportPath As String
        reportPath = reportsList.SelectedValue
        Session("reportPath") = reportPath
        LogActivity("View report '" & Mid(reportPath, reportPath.LastIndexOf(CChar("\")) + 2) & "'")
        ConfigureCrystalReports()
    End Sub

    Private Sub ConfigureCrystalReports()
        If Session("reportPath") IsNot Nothing Then
            Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
            myConnectionInfo.ServerName = WebConfigurationManager.AppSettings(Session("DBServer").ToString).ToString
            myConnectionInfo.DatabaseName = WebConfigurationManager.AppSettings(Session("Database").ToString).ToString
            myConnectionInfo.UserID = ""
            myConnectionInfo.Password = ""
            myConnectionInfo.IntegratedSecurity = True
            'myCrystalReportViewer.DisplayGroupTree = False
            myCrystalReportViewer.ReportSource = Session("reportPath").ToString
            SetDBLogonForReport(myConnectionInfo)
        End If
    End Sub

    Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo)
        Dim myTableLogOnInfos As TableLogOnInfos = myCrystalReportViewer.LogOnInfo
        For Each myTableLogOnInfo As TableLogOnInfo In myTableLogOnInfos
            myTableLogOnInfo.ConnectionInfo = myConnectionInfo
        Next
    End Sub

Edited by: Brentmsdn on Apr 5, 2011 11:44 PM

Edited by: Brentmsdn on Apr 5, 2011 11:55 PM