Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have one ASP.NET (C#) Project and i am using Crystal Report for generating reports..
and for report i am using MS Access Database

Now my question is that when my project runs on client pc at that time my report will use the database which is held on client pc and when it will in my pc means at development that time report use my database...

Both database have same schema and data as well..


Means i want the database connection dynamically...

at client pc it will use client database and in my pc it will use my database...

I want to set database information at runtime....
Posted
Updated 2-Nov-11 20:21pm
v2

Here i find this solution for My Question mentioned as above...

Here i have declare one Function that will set my database information at run time....

here setDbInfo is the function for seting db info this function will called after loding report to report document and before assining to report viewer like shown in DisplayReport function...

C#
protected void setDbInfo(ReportDocument rpt, string ServerName, string DatabaseName, string UserName, string Password)
        {
            TableLogOnInfo logoninfo = new TableLogOnInfo();
            foreach (CrystalDecisions.CrystalReports.Engine.Table t in rpt.Database.Tables)
            {
                logoninfo = t.LogOnInfo;
                logoninfo.ReportName = rpt.Name;
                logoninfo.ConnectionInfo.ServerName = ServerName;
                logoninfo.ConnectionInfo.DatabaseName = DatabaseName;
                logoninfo.ConnectionInfo.UserID = UserName;
                logoninfo.ConnectionInfo.Password = Password;
                logoninfo.TableName = t.Name;
                t.ApplyLogOnInfo(logoninfo);
                t.Location = t.Name;
            }
        }

        protected void DisplayReport()
        {
            try
            {
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Server.MapPath("~/CRList.rpt")); 
                string ServerName = ConfigurationManager.AppSettings["Server"].ToString();
                string DatabaseName = ConfigurationManager.AppSettings["DBName"].ToString();
                string UserName = ConfigurationManager.AppSettings["UserName"].ToString();
                string Password = ConfigurationManager.AppSettings["Password"].ToString();
                setDbInfo(rpt,ServerName,DatabaseName,UserName,Password);
                CRPTViewer.ReportSource = rpt;
                CRPTViewer.RefreshReport();
            }
            catch (Exception exc)
            {   
                throw exc;
            }
        }
 
Share this answer
 
Comments
Dalek Dave 3-Nov-11 6:21am    
Good Answer
1.Create an object of ReportDocument say object name : objRpt.
objReport = new yourreportname();

2. Write following code :

C#
foreach (Table crTable in objRpt.Database.Tables)
            {
                TableLogOnInfo logOnInfo = new TableLogOnInfo();
                logOnInfo = objRpt.Database.Tables[crTable.Name].LogOnInfo;

                // Set the connection information for the table in the report.
                logOnInfo.ConnectionInfo.ServerName = server.Trim();
                logOnInfo.ConnectionInfo.DatabaseName = database.Trim();
                logOnInfo.ConnectionInfo.UserID = username;
                logOnInfo.ConnectionInfo.Password = password;
                objRpt.Database.Tables[crTable.Name].ApplyLogOnInfo(logOnInfo);

}
I hope you can get connection details from config

3. Create an object of CrystalReportViewer class say object name : objCrystalReportViewer
.

4. objCrystalReportViewer.ReportSource = objRpt;
5. objCrystalReportViewer.Show()
 
Share this answer
 
v3
Dear friend

this is not possible as U r Saying, to achive this
Don't connect ur Crystal report directly with database
Add a dataset in ur project and add datatable in that dataset, now design ur fields in that
datatable and connect ur crystal report with that datatable, now dynamicly fill that datatable
by using this method U can make ur report dyanamic
 
Share this answer
 
Hi,

This is not possible in web application.
You have to set your database in hosting server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900