Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set connection to database in CR9

Status
Not open for further replies.

elibb

Programmer
Oct 22, 2001
335
MX
hi, i have a vb6.0 application, and i use Crystal Reports 9 to make my reports.
usually i connect my report to the databas eusing an odbc driver, so i have no problem when i install the program in other machines, but this time im not using a driver, i want to connect directly to the path of my database, how can i do this by code?? so i get the database path from my app, and then send it to the report??
i know there is a way, but i havent found it..

can anybody help me?

Eli
 
Hi try this
Synopsis

A VB .NET application uses Crystal Reports for Visual Studio .NET as the reporting development tool.

How do you pass database logon information to a Crystal Report at runtime in this VB .NET application?


Solution

To pass logon information to a Crystal Report at runtime, use the following code sample:

'Import namespaces in general section
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

'Add the following code above the statement "#Region" to declare the required objects
Dim crReportDocument As New 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 TableCounter

'Add the following code after the statement "InitializeComponent()"
crReportDocument = New CrystalReport1()

'CrystalReport1 is the name given to the strongly typed report that has been added to the 'application. In this case, we are creating a new instance of that report. If you want to work with 'existing reports, you will need to use the 'Load' method of the ReportDocument() object.
'For Example: 'crReportDocument.Load("C:\myReports\myReport.rpt", 'OpenReportMethod.OpenReportByTempCopy)

'Set the ConnectionInfo properties for logging on.
'The ServerName member is either the DSN (if using ODBC) or the physical server name (if using a 'native connection)

With crConnectionInfo
.ServerName = "DSN or Server Name"
.DatabaseName = "DatabaseName"
.UserID = "UID"
.Password = "PWD"
End With

'Set the CrTables to the Tables collection of the report
CrTables = crReportDocument.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)
Next

'If changing database servers at runtime, you will need to specify the table location similar to the line below:
'CrTable.Location = "the TableName in the report"

CrystalReportViewer1.ReportSource = crReportDocument


=============
NOTE:

If you're using a web application make sure that, you do not specify or call the DataBind in your code as this will nullify the code above.

If you are changing database servers at runtime, it is important that you specify the table location after you apply logon information (this is a case sensitive property). You can either specify the tablename only or the fully qualified tablename such as:

crTable.location = "dbname.dbo.tablename"
=============

To pass database logon information to a Crystal Report at runtime using C#, go to and search for:

· csharp_win_dbengine.exe

cheers

pgtek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top