Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to connect crystal report from VB.net 2005. Have a look my code:

VB
Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
rptdocument.Load(strReportPath)
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
rptdocument.DataSourceConnections.Item(0).SetConnection("server", "db", "uname", "pwd")
rptViewer.ReportSource = rptDocument

However it is always demanding me login details even though I supplied correct details.

*Note: server, db, uname, pwd for illustrative purpose only.
any assistance it would be greatly appreciated.

Cheers,
Asan
Posted
Updated 18-Apr-10 19:45pm
v2

1 solution

VB
Private Sub setConn(ByRef rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim crtableLogoninfos As New TableLogOnInfos()
        Dim crtableLogoninfo As New TableLogOnInfo()
        Dim crConnectionInfo As New ConnectionInfo()
        Dim CrTables As Tables
        Dim CrTable As Table

       

        Dim crReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
        crReportDocument = rpt



        'Set the ConnectionInfo properties for logging on to
        'the Database

        'If you are using ODBC, this should be the
        'DSN name NOT the physical server name. If
        'you are NOT using ODBC, this should be the
        'physical server name

        With crConnectionInfo
            .ServerName = "servername"

            'If you are connecting to Oracle there is no
            'DatabaseName. Use an empty string.
            'For example, .DatabaseName = ""

            .DatabaseName = "dbaname"
            .UserID = "username"
            .Password = "password"
        End With

        'This code works for both user tables and stored
        'procedures. Set the CrTables to the Tables collection
        'of the report

        CrTables = rpt.Database.Tables


        'Loop through each table in the report and apply the
        'LogonInfo information

        For Each CrTable In CrTables

            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)

            If LCase(CrTable.Location) = "command" Then
                'CrTable.Location = CrTable.Location
                Exit For
            End If
            'If your DatabaseName is changing at runtime, specify
            'the table location.
            'For example, when you are reporting off of a
            'Northwind database on SQL server you
            'should have the following line of code:

            CrTable.Location = "databasename.dbo." & CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)
        Next

    End Sub



hope this will help.

UPDATE SM: Added pre tags. Though Answer looks fine but would had been complete if replier adds some explanation to what code has been posted/what was the enquirer missing. :thumbsup:



From HS:

from where u r calling report there u need to declare 1 variable like this

ByVal rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument

after that follow all the steps u were doing but before dat call my cade which i have given to u like this

setConn(rpt)

it will make all the connection to ur report with table.

Just try it.

I am damn sure it will work. coz for ma all the projects its working.

best luck
 
Share this answer
 
v3
Comments
sentoso 31-Oct-12 21:25pm    
Hi Asan,

In the example code, you write using VB.Net 2005, since I Used ASP C#.net 2010 how to translate that code ?

In ASP C#.Net 2010, the line Dim CrTable As Table
if we translate into ASP C#.Net 2010

Table CrTable;

It rises an error like this
'Table' is an ambigous reference between 'System.Web.UI.WebControls.Table' and 'CrystalDecisions.CrystalReports.Engine.Table'

Regards,

Sentoso

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900