Quantcast
Channel: SCN : Discussion List - SAP Crystal Reports, version for Visual Studio
Viewing all articles
Browse latest Browse all 3636

Changing a Crystal Report datasource (access) at runtime in C#

$
0
0

Hi,

 

I'm getting the error 'Log on failed' when trying to change my datasource at runtime. I've trawled the SO community forums and from that put together the code below, but I'm still getting the error. My access db is 2013 and isn't password protected and I'm using the default "Admin" user. The report has an OLE DB connection. My code loops through all tables, including subreports and changes the login, and I also change the database login. Any help much appreciated:


public void RunTestReport()   {   DataSet testDataSet = new DL.NonConformance().GetNonConformances(1, "NonConformance");   var rpt = new ReportDocument();  rpt.Load(@"C:\Reports\rptTest.rpt");   SetCrystalLogin(rpt);  rpt.SetDataSource(testDataSet);  crystalReportViewer1.ReportSource = rpt;  crystalReportViewer1.Refresh();   }   public void SetCrystalLogin(ReportDocument oRpt)   {   ConnectionInfo oConnectInfo = new ConnectionInfo();   string dbpath = string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"\db\dbacc.db");  oConnectInfo.ServerName = dbpath;  oConnectInfo.DatabaseName = string.Empty;  oConnectInfo.UserID = "Admin";  oConnectInfo.Password = string.Empty;   // Set the logon credentials for all tables   SetCrystalTablesLogin(oConnectInfo, oRpt.Database.Tables);   // Check for subreports   foreach (CrystalDecisions.CrystalReports.Engine.Section oSection in oRpt.ReportDefinition.Sections)   {   foreach (CrystalDecisions.CrystalReports.Engine.ReportObject oRptObj in oSection.ReportObjects)   {   if (oRptObj.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)   {   // This is a subreport so set the logon credentials for this report's tables   CrystalDecisions.CrystalReports.Engine.SubreportObject oSubRptObj = oRptObj as CrystalDecisions.CrystalReports.Engine.SubreportObject;   // Open the subreport   CrystalDecisions.CrystalReports.Engine.ReportDocument oSubRpt = oSubRptObj.OpenSubreport(oSubRptObj.SubreportName);   SetCrystalTablesLogin(oConnectInfo, oSubRpt.Database.Tables);   }   }   }  oRpt.Refresh();  oRpt.SetDatabaseLogon("Admin", string.Empty, dbpath, string.Empty, true);  oRpt.VerifyDatabase();  oRpt.Refresh();   }   private void SetCrystalTablesLogin(CrystalDecisions.Shared.ConnectionInfo oConnectInfo, Tables oTables)   {   foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in oTables)   {   CrystalDecisions.Shared.TableLogOnInfo oLogonInfo = oTable.LogOnInfo;  oLogonInfo.ConnectionInfo = oConnectInfo;  oTable.ApplyLogOnInfo(oLogonInfo);   }   }

Viewing all articles
Browse latest Browse all 3636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>