INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a
Computer / IT professional?
Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site
(Download This Button Today!)
"...Brilliant! Your site is great...and saving me hours of time at work and making my boss think I am brilliant too! I also picked up on a thread that will potentially save us a lot of money in the future..."
Where in the world do Tek-Tips members come from?
How to pass Database logon info to a Crystal Report at runtime in VB .NET or in C#
Posted: 20 Feb 04
When we design reports in Crystal Report designer, it fixs the complete information about Database location with table or stored procedures and if we want to run same reports with different datasource then we have to change logon information and as well as the location of the database. The main purpose of this article is to explain how we change logon information of our reports to run them with different databases. I noticed that it is a most common problem which most of the developer face in development.
How do you pass database logon information to a Crystal Report at runtime in this VB .NET application?
To pass logon information to a Crystal Report at
runtime, use the following code sample:
Dim crtableLogoninfos As New TableLogOnInfos()
Dim crtableLogoninfo As New TableLogOnInfo()
Dim crConnectionInfo As New ConnectionInfo()
Dim CrTables As Tables
Dim CrTable As Table
'If you are using a Strongly Typed report (Imported in
'your project) named CrystalReport1.rpt use the
Dim crReportDocument As New CrystalReport1()
'If you are using a Non-Typed report, and
'loading a report outside of the project, use the
Dim crReportDocument As New ReportDocument()
'Set the ConnectionInfo properties for logging on to
'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
.ServerName = "DSN or Server Name"
'If you are connecting to Oracle there is no
'DatabaseName. Use an empty string.
'For example, .DatabaseName = ""
.DatabaseName = "DatabaseName"
.UserID = "Your User ID"
.Password = "Your Password"
'This code works for both user tables and stored
'procedures. Set the CrTables to the Tables collection
'of the report
CrTables = crReportDocument.Database.Tables
'Loop through each table in the report and apply the
For Each CrTable in CrTables
CrTableLogonInfo = CrTable.LogonInfo
'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 = crConnectionInfo.DatabaseName & ".dbo." &
".") + 1)
'Set the viewer to the report object to be previewed.
CrystalReportViewer1.ReportSource = crReportDocument
private CrystalReport1 crReportDocument = new
private Database crDatabase;
private Tables crTables;
private Table crTable;
private TableLogOnInfo crTableLogOnInfo;
private ConnectionInfo crConnectionInfo = new
//Setup the connection information structure
//to log on to the data source for the report.
// If using ODBC, this should be the DSN. If using
// OLEDB, etc, this should be the physical server name
crConnectionInfo.ServerName = "DSN or
// If you are connecting to Oracle there is no
// DatabaseName. Use an empty
// string i.e. crConnectionInfo.DatabaseName = "";
crConnectionInfo.DatabaseName = "DatabaseName";
crConnectionInfo.UserID = "Your UserID";
crConnectionInfo.Password = "Your Password";
// This code works for both user tables and stored
//Get the table information from the report
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
//Loop through all tables in the report and apply the
//connection information for each table.
for (int i = 0; i < crTables.Count; i++)
crTable = crTables ;
crTableLogOnInfo = crTable.LogOnInfo;
//If your DatabaseName is changing at runtime, specify
//the table location. For example, when you are
//off of a Northwind database on SQL server
//you should have the following line of code:
crTable.Location = crConnectionInfo.DatabaseName + ".dbo." +
(".") + 1)
//Set the viewer to the report object to
· 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 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 = "databaseName.dbo.tablename"
If you are reporting off of an Access Database, then
you only need to specify either the ServerName or
DatabaseName to the ConnectionInfo Object depending on
how you are connecting to Access.
For example, if you are connecting to Access through ODBC,
then you just need to set the DatabaseName for the
ConnectionInfo object as the following:
.DatabaseName = "C:\mydatabase\mydata.mdb"
If you are connecting to Access via OLEDB, then you
just need to set the ServerName:
.ServerName = "C:\mydatabase\mydata.mdb"
CR for Visual Studio .Net
9.1.9360.0 Crystal Reports for Visual Studio .Net
Muhammad Essa Mughal
Back to Business Objects: Crystal Reports 4 Other topics FAQ Index
Back to Business Objects: Crystal Reports 4 Other topics Forum
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
- Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close