C# Crystal Reports Dynamic Logon parameters

The following section describes how to pass the logon information like Server Name , database Name , User Name and password dynamically to the Crystal Reports from C# applications.

When you are new to Crystal Report, you may frequently encounter the following error message: "Failed to open the connection." This is a prevalent issue when integrating Crystal Reports in C#. However, you can effectively resolve this problem by utilizing dynamic logon parameter passing.

When executing the prior programs in this C# tutorial, each time you run the Crystal Reports, it prompts for the Username and Password. This segment elucidates the method to circumvent the runtime dialogue box that requests the username and password for Crystal Reports login. Within this section, we employ dynamic techniques to dynamically pass the User ID, Password, Server Name, and Database Name from C# to Crystal Reports.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

In numerous scenarios, the dynamic passing of logon parameter fields proves highly advantageous. For instance, when undertaking a database project within a test server environment that may later require migration to another server, it is advisable to provide these vital pieces of information dynamically to Crystal Reports. By doing so, the process becomes more flexible and adaptable to different server environments.

In step by step tutorial for creating a Crystal Reports from C# - we created a report selecting all data from the Product table . There is no chance to change the server name or any other information on runtime in that case, because it is a static report and it will asks username and password every time you run the Crystal Reports . Here we are passing Server Name , UserID and Password dynamically to the Crystal Reports from our C# program

Here we use Crystal Reports ConnectionInfo class for passing connection information.

ConnectionInfo crConnectionInfo = new ConnectionInfo();

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project.

Full Source C#
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables ; cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); crConnectionInfo.ServerName = "YOUR SERVER NAME"; crConnectionInfo.DatabaseName = "YOUR DATABASE NAME"; crConnectionInfo.UserID = "YOUR DATABASE USERNAME"; crConnectionInfo.Password = "YOUR DATABASE PASSWORD"; CrTables = cryRpt.Database.Tables ; foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }

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

The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt

You have to replace the source code values of

crConnectionInfo.ServerName = "YOUR SERVER NAME";

crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";

crConnectionInfo.UserID = "YOUR DATABASE USERNAME";

crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

with your real-time values.

csharp-crystal-reports-final