VB.NET Crystal Reports Load Dynamically

Before proceeding with this tutorial, it is important to note that all programming samples showcased herein are based on the "crystaldb" database. Kindly take a moment to familiarize yourself with the database structure by referring to the following link: Database Structure .

In this particular section, our focus centers around the dynamic passing of User ID, Password, Server Name, and Database Name to Crystal Reports from a VB.NET application. This dynamic approach allows for greater flexibility and adaptability in configuring the Crystal Reports environment.


The ability to pass User ID, Password, Server Name, and Database Name dynamically to Crystal Reports from a VB.NET application proves incredibly valuable in various scenarios. For instance, when developing a database project on a test server that requires migration to another environment, dynamically providing these parameters to Crystal Reports streamlines the transition process.

In the field of VB.NET Crystal Report development, encountering the error message "Failed to open the connection" is not uncommon. This issue often arises when embedding Crystal Reports in a VB.NET application. However, by implementing dynamic logon parameter passing, this problem can be effectively resolved.

In this program, we use our previous work and dynamically pass the required values to Crystal Reports. Before diving into this section, I recommend reviewing the step by step Crystal Report in VB.NET. This guide walks you through the creation of a report that selects all data from the Product table. As this report is static, the ability to change the server dynamically at runtime is not feasible. However, in the present scenario, we overcome this limitation by dynamically passing the Server Name, UserID, and Password to Crystal Reports.

Here we use Crystal Reports ConnectionInfo .

Dim crConnectionInfo As New ConnectionInfo , and pass these arguments to ConnectionInfo

Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

  1. Imports CrystalDecisions.CrystalReports.Engine
  2. Imports CrystalDecisions.Shared

Put the following source code in the button click event

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim cryRpt 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 cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") With crConnectionInfo .ServerName = "YOUR SERVER NAME" .DatabaseName = "YOUR DATABASE NAME" .UserID = "YOUR DATABASE USERNAME" .Password = "YOUR DATABASE PASSWORD" End With CrTables = cryRpt.Database.Tables For Each CrTable In CrTables crtableLogoninfo = CrTable.LogOnInfo crtableLogoninfo.ConnectionInfo = crConnectionInfo CrTable.ApplyLogOnInfo(crtableLogoninfo) Next CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub End Class

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here.

You have to replace the source code values of "YOUR SERVER NAME" , "YOUR DATABASE NAME" , "YOUR DATABASE USERNAME" , "YOUR DATABASE PASSWORD" with your correct values .

When you run this program you will get the following screen.