Change the database path for a Crystal Report dynamically through C# code when the form with the report viewer loads

I have a simple application that connects to an Access database (.mdb). I connected the database file to the application using code only. The connection string is as follows,

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\CentreDB.mdb;Jet OLEDB:Database Password=pass"

For designing purposes I connected the database file to a crystal report by using the Connect Database File option in the report Wizard. But I need to use code to dynamically assign the database to the crystal report, so that I wont have a problem when I deploy it in a client machine. I tried to use this code below.

public void ViewReport(FileInfo crFileInfo) // path\filename.rpt
        {
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            crConnectionInfo.ServerName = "YOUR SERVER NAME";
            crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
            crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
            crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

            // CrystalReportViewer control
            crViewer.ReportSource = ReportSourceSetup(crFileInfo, crConnectionInfo);
            crViewer.Refresh();
        }

        ReportDocument ReportSourceSetup(FileInfo crFileInfo, ConnectionInfo crConnectionInfo)
        {
            ReportDocument crDoc = new ReportDocument();
            TableLogOnInfos crTableLogonInfos = new TableLogOnInfos();
            TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
            Tables crTables;

            crDoc.Load(crFileInfo.FullName);

            // Each table in report needs to have logoninfo setup:
            crTables = crDoc.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
            {
                crTableLogonInfo = crTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                crTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            return crDoc;
        } 

I found this sample on the net, but I can't get it to work. I need to know where I need to place this exactly & what I should pass for "YOUR SERVER NAME", "YOUR DATABASE NAME" since I'm using a database file with only a password. I think that the FileInfo object carries the path of the database. Please help me if possible. Any help would be greatly appreciated.

Answers


With access I believe you should specify a DSN as the servername, so setup a DSN on the machine and reference it this way...if you named the dsn access_dsn:

        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.ServerName = "access_dsn"; //dsn name
        crConnectionInfo.DatabaseName = ""; //leave blank, it knows from DSN
        crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
        crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

Need Your Help

Distance between elements in HTML page

javascript jquery html css

I am working on a planetarium in HTML/CSS/jQuery/JS, and there is something I don't really manage to do.

How to assign color scale to a variable in a 3D scatter plot?

r colors ggplot2 3d gradient

I'm a beginner in R and needs a bit of help for my scripting.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.