.NET Tips


ASP.NET "Parse error message: Could not load type"

TAGS: aspx, .net, parse error


If you run across this one, make sure you have built your project and that your dll's are in the 'bin' directory off the root of your web application.

Placing the 'bin' directory in a sub directory besides the root, does not work as a friend found out when trying to implement the Crystal Reports code I have below into his existing project.

Crystal Reports & .NET 2.0 "object doesn't support this action"

TAGS: crystal reports, .net, toolbar, export, print


If you are missing toolbar graphics or are unable to export or print from your report, try the following.

This should certainly help if you've already followed other advice on the web about creating an 'aspnet_client' directory under your project to resolve the issue of your toolbar not showing up correctly.

If it does not already exist under your project directory, create/copy the directory structure of ..
'aspnet_client\system_web\2_0_50727\CrystalReportWebFormViewer3'
.. along with the typical sub-directories (css, html, images, images\toolbar, images\tree, js) and their respective files.

That should fix your missing toolbar graphics and solve your exporting/printing issue.

ASP.NET & Crystal Reports Script

TAGS: aspx, .net, crystal reports


Here's an easy way to have one page that can view multiple reports.
This way you can simply have a page with links to your various reports, such as:

href="../ReportViewer.aspx?crDB=database&crRPT=report&rptVar=1"

Change the 'Partial Class ReportViewer' entry to match your inherits property on your '.aspx' page.
Drag and drop a Crystal Reports viewer onto your '.aspx' page, and viola.

Note: Make sure you have the Crystal Reports entries in your 'web.config' file or you will receive an error.
To easily get these in, add a CR page to your project, then remove it. It will leave the required entries in place.

Copy or replace, as appropriate, the following into the '.vb' portion of your page:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class ReportViewer
    Inherits System.Web.UI.Page

    'How to use:
    'ReportViewer.aspx?crDB=myDatabase&crRPT=myReport&rptVar=1

    'crDB = Database
    'crRPT = Report (Do NOT include the .rpt extension)
    'Remaining parameters, rptVar in this case, will be passed to the report

    'WARNING: Passing a variable the report is not setup to handle will cause an error

    'Var to hold a single report
    Public oReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim crDB As String          'URL passed DB Name
        Dim crRPT As String         'URL passed Report Name
        Dim defaultSRV As String    'Default Server
        Dim defaultID As String     'Default User ID
        Dim defaultPW As String     'Default Password
        Dim defaultDB As String     'Default DB Name
        Dim defaultPATH As String   'Default Report Path
        Dim defaultRPT As String    'Default Report Name

        'defaultDB & defaultRPT are not required, however, if they are included, you
        'will NOT have to specify the crDB & crRPT variables for that report in the URL.
        'You could simply use: "ReportViewer.aspx?rptVar=1" instead.

        defaultSRV = "server"           'Default Server
        defaultID = "userid"            'Default User ID
        defaultPW = "password"          'Default Password
        defaultDB = "database"          'Default DB Name
        defaultPATH = "c:\reports\"     'Default Report Path
        defaultRPT = "report.rpt"       'Default Report Name

        crDB = Request.QueryString.Item("crDB")     'Database to link to for CR
        crRPT = Request.QueryString.Item("crRPT")   'Report name and sub-path if required

        'Set a default database or use specified
        If crDB Is Nothing Then
            crDB = defaultDB
        End If

        'Set a default report or use specified
        If crRPT Is Nothing Then
            crRPT = defaultPATH & defaultRPT
        Else
            crRPT = defaultPATH & crRPT & ".rpt"
        End If

        'Process a report (Load, Login, View)
        oReport.Load(crRPT)
        DoCRLogin(defaultSRV, crDB, defaultID, defaultPW, oReport)
        CrystalReportViewer1.ReportSource = oReport

    End Sub

    Public Sub PassQStoCR()
        'Parse QueryString and pass to Crystal Reports
        Dim loop1, loop2 As Integer
        Dim arr1(), arr2() As String
        Dim coll As NameValueCollection
        'Load variables into NameValueCollection
        coll = Request.QueryString
        'Place key names into an array
        arr1 = coll.AllKeys
        For loop1 = 0 To arr1.GetUpperBound(0)
            'Get all values under this key.
            arr2 = coll.GetValues(loop1)
            For loop2 = 0 To arr2.GetUpperBound(0)
                'Two loops to ignore the passed DB & Report variables
                If Not Server.HtmlEncode(arr1(loop1)) = "crDB" Then
                    If Not Server.HtmlEncode(arr1(loop1)) = "crRPT" Then
                        oReport.SetParameterValue(Server.HtmlEncode(arr1(loop1)), _
						Server.HtmlEncode(arr2(loop2)))
                    End If
                End If
            Next loop2
        Next loop1
    End Sub

    Public Sub DoCRLogin(ByVal crSRV As Object, ByVal crDB As Object, ByVal crID As Object, _
	ByVal crPW As Object, ByRef oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim _applyLogin As New ApplyCRLogin

        PassQStoCR()    'Read QS and pass vars to CR

        'Use ApplyLogin object to apply login info to all tables in CR object
        _applyLogin._dbName = crDB
        _applyLogin._passWord = crPW
        _applyLogin._serverName = crSRV
        _applyLogin._userID = crID
        _applyLogin.ApplyInfo(oRpt)

        'Clean up
        _applyLogin = Nothing
    End Sub

    Public Class ApplyCRLogin
        Public _dbName As String
        Public _serverName As String
        Public _userID As String
        Public _passWord As String

        Public Sub ApplyInfo(ByRef _oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
            Dim oCRDb As CrystalDecisions.CrystalReports.Engine.Database = _oRpt.Database
            Dim oCRTables As CrystalDecisions.CrystalReports.Engine.Tables = oCRDb.Tables
            Dim oCRTable As CrystalDecisions.CrystalReports.Engine.Table
            Dim oCRTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
            Dim oCRConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
            oCRConnectionInfo.DatabaseName = _dbName
            oCRConnectionInfo.ServerName = _serverName
            oCRConnectionInfo.UserID = _userID
            oCRConnectionInfo.Password = _passWord
            For Each oCRTable In oCRTables
                oCRTableLogonInfo = oCRTable.LogOnInfo
                oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo
                oCRTable.ApplyLogOnInfo(oCRTableLogonInfo)
            Next
        End Sub

    End Class

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        oReport.Dispose()
    End Sub
End Class

There you have it. Enjoy!
Portions of the above code where adapted from code found on the web.
I supply this code in the same spirit as you supplied yours.