cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Table Location Error Oracle and SqlServer

Former Member
0 Likes
1,864

<p>

I would like to use the <b>same report for several data base providers</b> (ODBC, OLEDB SqlServer and OLEDB Oracle).<br />

My original report is designed with ODBC by another department. When I try to change the data base provider to Oracle or SqlServer I have an error at line:<br />

<br /></p>

<code>

&nbsp;//this line throw an exception if the report was not designed with the

same database provider.<br />

switch (provider)<br />

{<br />

&nbsp;&nbsp;&nbsp; case Provider.SqlServer:<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; crTable.Location =

String.Format(&quot;{0}.dbo.&quot;, crConnectionInfo.DatabaseName, crTable.Name); //database.dbo.tablename<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br /> &nbsp;&nbsp;&nbsp; case Provider.Oracle:<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; crTable.Location = String.Format(&quot;.&quot;, crConnectionInfo.DatabaseName, crTable.Name);

//schema.tablename<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />

&nbsp;&nbsp;&nbsp; default:<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />

}</code>

<p>Even setting the same location, like this crTable.Location = crTable.Location; the code throws an exception.

I have to change the report schema location at runtime because we have several databases (SqlServer) or schemas (Oracle) and we have to use the same report.

I use Crystal Reports Version=12.0.2000.0</p>

<br />

Here is my complete code:<br />

<code>

private CrystalDecisions.Shared.ConnectionInfo FixDatabaseSqlServer()<br />

{<br />

CrystalDecisions.Shared.DbConnectionAttributes dbAttributes = new

DbConnectionAttributes();<br />

dbAttributes.Collection.Set(&quot;Server&quot;, this.ServerName.Text);<br />

dbAttributes.Collection.Set(&quot;Trusted_Connection&quot;, &quot;false&quot;);<br />

dbAttributes.Collection.Set(&quot;Data Source&quot;, this.ServerName.Text);<br />

dbAttributes.Collection.Set(&quot;Initial Catalog&quot;, this.DatabaseName.Text);<br />

dbAttributes.Collection.Set(&quot;Integrated Security&quot;, &quot;false&quot;);<br />

dbAttributes.Collection.Set(&quot;Provider&quot;, &quot;SQLOLEDB&quot;);<br />

<br />

//setup the connection <br />

CrystalDecisions.Shared.ConnectionInfo crConnectionInfo = new ConnectionInfo();<br />

crConnectionInfo.DatabaseName = this.DatabaseName.Text;<br />

crConnectionInfo.ServerName = this.ServerName.Text;<br />

crConnectionInfo.UserID = this.UserID.Text;<br />

crConnectionInfo.Password = this.Password.Text;<br />

crConnectionInfo.IntegratedSecurity = false;<br />

crConnectionInfo.Attributes.Collection.Set(&quot;Database DLL&quot;, &quot;crdb_ado.dll&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_DatabaseName&quot;,

this.DatabaseName.Text);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_DatabaseType&quot;, &quot;OLE DB (ADO)&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_LogonProperties&quot;, dbAttributes);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_ServerDescription&quot;,

this.ServerName.Text);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_SQLDB&quot;, &quot;True&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;SSO Enabled&quot;, &quot;False&quot;);<br />

crConnectionInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.CRQE;<br />

crConnectionInfo.LogonProperties = dbAttributes.Collection;<br />

<br />

return (crConnectionInfo);<br />

}<br />

<br />

private CrystalDecisions.Shared.ConnectionInfo FixDatabaseOracle()<br />

{<br />

CrystalDecisions.Shared.DbConnectionAttributes dbAttributes = new

DbConnectionAttributes();<br />

dbAttributes.Collection.Set(&quot;Server&quot;, this.ServerName.Text);<br />

dbAttributes.Collection.Set(&quot;Trusted_Connection&quot;, &quot;False&quot;);<br />

dbAttributes.Collection.Set(&quot;Data Source&quot;, this.ServerName.Text);<br />

dbAttributes.Collection.Set(&quot;Provider&quot;, &quot;MSDAORA&quot;);<br />

<br />

//setup the connection <br />

CrystalDecisions.Shared.ConnectionInfo crConnectionInfo = new ConnectionInfo();<br />

crConnectionInfo.DatabaseName = &quot;&quot;;<br />

crConnectionInfo.ServerName = this.ServerName.Text;<br />

crConnectionInfo.UserID = this.UserID.Text;<br />

crConnectionInfo.Password = this.Password.Text;<br />

crConnectionInfo.IntegratedSecurity = false;<br />

crConnectionInfo.Attributes.Collection.Set(&quot;Database DLL&quot;, &quot;crdb_oracle.dll&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_DatabaseName&quot;, &quot;&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_DatabaseType&quot;, &quot;OLE DB (ADO)&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_LogonProperties&quot;, dbAttributes);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_ServerDescription&quot;,

this.ServerName.Text);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;QE_SQLDB&quot;, &quot;True&quot;);<br />

crConnectionInfo.Attributes.Collection.Set(&quot;SSO Enabled&quot;, &quot;False&quot;);<br />

crConnectionInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.CRQE;<br />

crConnectionInfo.LogonProperties = dbAttributes.Collection;<br />

<br />

return (crConnectionInfo);<br />

}<br />

<br />

private void FixDatabase(ReportDocument report, Provider provider)<br />

{<br />

bool setLocationError = false;<br />

string tableLocationPattern = String.Empty;<br />

CrystalDecisions.Shared.ConnectionInfo crConnectionInfo = null;<br />

<br />

switch (provider)<br />

{<br />

case Provider.SqlServer:<br />

crConnectionInfo = FixDatabaseSqlServer();<br />

tableLocationPattern = String.Format(&quot;{0}..&quot;, crConnectionInfo.DatabaseName, &quot;&quot;);<br /> break;<br /> case Provider.Oracle:<br /> crConnectionInfo = FixDatabaseOracle();<br /> tableLocationPattern = String.Format(&quot;.&quot;, crConnectionInfo.UserID, &quot;&quot;);<br /> break;<br /> <br /> case Provider.Undefinded:<br /> default:<br /> break;<br /> }<br /> <br /> //set database login information for the entire report object<br /> report.SetDatabaseLogon(crConnectionInfo.UserID, crConnectionInfo.Password, crConnectionInfo.ServerName, crConnectionInfo.DatabaseName);<br /> <br /> <br /> CrystalDecisions.CrystalReports.Engine.Database crDatabase = report.Database; //Set the CrDatabase Object to the Report's Database <br /> CrystalDecisions.CrystalReports.Engine.Tables crTables = crDatabase.Tables; //Set the CrTables object to the Tables collection of the Report's dDtabase<br /> <br /> foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)<br /> {<br /> TableLogOnInfo crTableLogOnInfo = null;<br /> crTableLogOnInfo = crTable.LogOnInfo;<br /> if (crTableLogOnInfo != null)<br /> {<br /> crTableLogOnInfo.ConnectionInfo = crConnectionInfo;<br /> crTable.ApplyLogOnInfo(crTableLogOnInfo);<br /> <br /> //<br /> //Set location.<br /> //<br /> try<br /> {<br /> //this line throw an exception if the report was not designed with the same database provider.<br /> switch (provider)<br /> {<br /> case Provider.SqlServer:<br /> crTable.Location = String.Format(&quot;.dbo.&quot;, crConnectionInfo.DatabaseName, crTable.Name); //database.dbo.tablename<br /> break;<br /> case Provider.Oracle:<br /> crTable.Location = String.Format(&quot;.&quot;, crConnectionInfo.DatabaseName,

crTable.Name); //schema.tablename<br />

break;<br />

default:<br />

break;<br />

}<br />

crTable.Location = crTable.Location;<br />

<br />

}<br />

catch (Exception ex)<br />

{<br />

setLocationError = true;<br />

//throw;<br />

}<br />

crTable.TestConnectivity();<br />

}<br />

}<br />

<br />

//call this method recursively for each subreport<br />

foreach (ReportObject reportObject in report.ReportDefinition.ReportObjects)<br />

{<br />

if (reportObject.Kind == ReportObjectKind.SubreportObject)<br />

{<br />

FixDatabase(report.OpenSubreport(((SubreportObject)reportObject).SubreportName),

provider);<br />

}<br />

}<br />

}

</code>

<p>

I tried everything I found at forums with no success.<br />

Please, Is there anyone with the same problem who had solved it?

Is it possible to have the same report and several database providers?

<br />

What have do I have to do?

Thank you very much.</p>

Edited by: jporcar on Feb 9, 2010 9:41 AM

View Entire Topic
Former Member
0 Likes

<p>

We have several unresolved difficultties with Oracle using the code below supplied by SAP.<br />

But I hope it helps.

</p>

<p>

<strong>Access</strong><br />

<code>

//Add these required Crystal Assemblies to your project<br />

//CrystalDecisions.ReportAppServer.DataDefModel<br />

//CrystalDecisions.ReportAppServer.ClientDoc<br />

//CrystalDecisions.ReportAppServer.Controllers<br />

//CrystalDecisions.CrystalReports.Engine<br />

//CrystalDecisions.Shared<br />

<br />

//Add these Crystal Assemblies to the top of your code page<br />

using CrystalDecisions.ReportAppServer.DataDefModel;<br />

using CrystalDecisions.CrystalReports.Engine;<br />

using CrystalDecisions.Shared;<br />

<br />

private ReportDocument ChangeConnectionInfo()<br />

{<br />

ReportDocument boReportDocument = new ReportDocument();<br />

//*EDIT* Change the path and report name to the report you want to change.<br />

boReportDocument.Load(@&quot;c:\reports\yourreport.rpt&quot;,

OpenReportMethod.OpenReportByTempCopy);<br />

<br />

//Create a new Database Table to replace the reports current table.<br />

CrystalDecisions.ReportAppServer.DataDefModel.Table boTable = <br />

new CrystalDecisions.ReportAppServer.DataDefModel.Table();<br />

<br />

//boMainPropertyBag: These hold the attributes of the tables ConnectionInfo

object<br />

PropertyBag boMainPropertyBag = new PropertyBag();<br />

//boInnerPropertyBag: These hold the attributes for the QE_LogonProperties<br />

//In the main property bag (boMainPropertyBag)<br />

PropertyBag boInnerPropertyBag = new PropertyBag();<br />

<br />

//Set the attributes for the boInnerPropertyBag<br />

boInnerPropertyBag.Add(&quot;Data Source&quot;,

@&quot;your_data_base.accdb&quot;);<br />

boInnerPropertyBag.Add(&quot;Locale Identifier&quot;, &quot;1033&quot;);<br />

boInnerPropertyBag.Add(&quot;Office Database Type&quot;, &quot;Access&quot;);<br />

boInnerPropertyBag.Add(&quot;OLE DB Services&quot;, &quot;-6&quot;);<br />

boInnerPropertyBag.Add(&quot;Provider&quot;, &quot;Microsoft.ACE.OLEDB.12.0&quot;);<br />

boInnerPropertyBag.Add(&quot;Use DSN Default Properties&quot;, &quot;False&quot;);<br />

<br />

//Set the attributes for the boMainPropertyBag<br />

boMainPropertyBag.Add(&quot;Database DLL&quot;, &quot;crdb_ado.dll&quot;);<br />

boMainPropertyBag.Add(&quot;QE_DatabaseName&quot;, &quot;&quot;);<br />

boMainPropertyBag.Add(&quot;QE_DatabaseType&quot;, &quot;OLE DB (ADO)&quot;);<br />

//Add the QE_LogonProperties we set in the boInnerPropertyBag Object<br />

boMainPropertyBag.Add(&quot;QE_LogonProperties&quot;, boInnerPropertyBag);<br />

boMainPropertyBag.Add(&quot;QE_ServerDescription&quot;,

@&quot;
Xaloc\epsilonnet\Dietario\Datos\DatosConfig.accdb&quot;);<br />

boMainPropertyBag.Add(&quot;QE_SQLDB&quot;, &quot;True&quot;);<br />

boMainPropertyBag.Add(&quot;SSO Enabled&quot;, &quot;False&quot;);<br />

<br />

//Create a new ConnectionInfo object<br />

CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo boConnectionInfo =

<br />

new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();<br />

//Pass the database properties to a connection info object<br />

boConnectionInfo.Attributes = boMainPropertyBag;<br />

//Set the connection kind<br />

boConnectionInfo.Kind = CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;<br />

//*EDIT* Set the User Name and Password if required.<br />

boConnectionInfo.UserName = &quot;UserName&quot;;<br />

boConnectionInfo.Password = &quot;Password&quot;;<br />

//Pass the connection information to the table<br />

boTable.ConnectionInfo = boConnectionInfo;<br />

<br />

//Get the Database Tables Collection for your report<br />

CrystalDecisions.ReportAppServer.DataDefModel.Tables boTables;<br />

boTables =

boReportDocument.ReportClientDocument.DatabaseController.Database.Tables;<br />

<br />

//For each table in the report:<br />

// - Set the Table Name properties.<br />

// - Set the table location in the report to use the new modified table<br />

boTable.Name = &quot;TABLE1&quot;;<br />

boTable.QualifiedName = &quot;TABLE1&quot;;<br />

boTable.Alias = &quot;TABLE1&quot;;<br />

<br />

boReportDocument.ReportClientDocument.DatabaseController.SetTableLocation(boTables[0],

boTable);<br />

<br />

//Verify the database after adding substituting the new table.<br />

//To ensure that the table updates properly when adding Command tables or Stored

Procedures.<br />

boReportDocument.VerifyDatabase();<br />

<br />

return boReportDocument;<br />

}</code></p>

<p>

<strong>SqlServer</strong><br />

<code>

//Add these required Crystal Assemblies to your project<br />

//CrystalDecisions.ReportAppServer.DataDefModel<br />

//CrystalDecisions.ReportAppServer.ClientDoc<br />

//CrystalDecisions.ReportAppServer.Controllers<br />

//CrystalDecisions.CrystalReports.Engine<br />

//CrystalDecisions.Shared<br />

<br />

//Add these Crystal Assemblies to the top of your code page<br />

using CrystalDecisions.ReportAppServer.DataDefModel;<br />

using CrystalDecisions.CrystalReports.Engine;<br />

using CrystalDecisions.Shared;<br />

<br />

private ReportDocument ChangeConnectionInfo()<br />

{<br />

ReportDocument boReportDocument = new ReportDocument();<br />

//*EDIT* Change the path and report name to the report you want to change.<br />

boReportDocument.Load(@&quot;c:\reports\yourreport.rpt&quot;,

OpenReportMethod.OpenReportByTempCopy);<br />

<br />

//Create a new Database Table to replace the reports current table.<br />

CrystalDecisions.ReportAppServer.DataDefModel.Table boTable = <br />

new CrystalDecisions.ReportAppServer.DataDefModel.Table();<br />

<br />

//boMainPropertyBag: These hold the attributes of the tables ConnectionInfo

object<br />

PropertyBag boMainPropertyBag = new PropertyBag();<br />

//boInnerPropertyBag: These hold the attributes for the QE_LogonProperties<br />

//In the main property bag (boMainPropertyBag)<br />

PropertyBag boInnerPropertyBag = new PropertyBag();<br />

<br />

//Set the attributes for the boInnerPropertyBag<br />

boInnerPropertyBag.Add(&quot;Auto Translate&quot;, &quot;-1&quot;);<br />

boInnerPropertyBag.Add(&quot;Connect Timeout&quot;, &quot;15&quot;);<br />

boInnerPropertyBag.Add(&quot;Data Source&quot;, &quot;localhost&quot;);<br />

boInnerPropertyBag.Add(&quot;General Timeout&quot;, &quot;0&quot;);<br />

boInnerPropertyBag.Add(&quot;Initial Catalog&quot;, &quot;your_data_base&quot;);<br />

boInnerPropertyBag.Add(&quot;Integrated Security&quot;, &quot;True&quot;);<br />

boInnerPropertyBag.Add(&quot;Locale Identifier&quot;, &quot;3082&quot;);<br />

boInnerPropertyBag.Add(&quot;OLE DB Services&quot;, &quot;-5&quot;);<br />

boInnerPropertyBag.Add(&quot;Provider&quot;, &quot;SQLOLEDB&quot;);<br />

boInnerPropertyBag.Add(&quot;Tag with column collation when possible&quot;, &quot;0&quot;);<br />

boInnerPropertyBag.Add(&quot;Use DSN Default Properties&quot;, &quot;False&quot;);<br />

boInnerPropertyBag.Add(&quot;Use Encryption for Data&quot;, &quot;0&quot;);<br />

<br />

//Set the attributes for the boMainPropertyBag<br />

boMainPropertyBag.Add(&quot;Database DLL&quot;, &quot;crdb_ado.dll&quot;);<br />

boMainPropertyBag.Add(&quot;QE_DatabaseName&quot;, &quot;your_data_base&quot;);<br />

boMainPropertyBag.Add(&quot;QE_DatabaseType&quot;, &quot;OLE DB (ADO)&quot;);<br />

//Add the QE_LogonProperties we set in the boInnerPropertyBag Object<br />

boMainPropertyBag.Add(&quot;QE_LogonProperties&quot;, boInnerPropertyBag);<br />

boMainPropertyBag.Add(&quot;QE_ServerDescription&quot;, &quot;localhost&quot;);<br />

boMainPropertyBag.Add(&quot;QE_SQLDB&quot;, &quot;True&quot;);<br />

boMainPropertyBag.Add(&quot;SSO Enabled&quot;, &quot;False&quot;);<br />

<br />

//Create a new ConnectionInfo object<br />

CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo boConnectionInfo =

<br />

new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();<br />

//Pass the database properties to a connection info object<br />

boConnectionInfo.Attributes = boMainPropertyBag;<br />

//Set the connection kind<br />

boConnectionInfo.Kind = CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;<br />

//*EDIT* Set the User Name and Password if required.<br />

boConnectionInfo.UserName = &quot;UserName&quot;;<br />

boConnectionInfo.Password = &quot;Password&quot;;<br />

//Pass the connection information to the table<br />

boTable.ConnectionInfo = boConnectionInfo;<br />

<br />

//Get the Database Tables Collection for your report<br />

CrystalDecisions.ReportAppServer.DataDefModel.Tables boTables;<br />

boTables =

boReportDocument.ReportClientDocument.DatabaseController.Database.Tables;<br />

<br />

//For each table in the report:<br />

// - Set the Table Name properties.<br />

// - Set the table location in the report to use the new modified table<br />

boTable.Name = &quot;TABLE1&quot;;<br />

boTable.QualifiedName = &quot;your_data_base.dbo.TABLE1&quot;;<br />

boTable.Alias = &quot;TABLE1&quot;;<br />

<br />

boReportDocument.ReportClientDocument.DatabaseController.SetTableLocation(boTables[0],

boTable);<br />

<br />

//Verify the database after adding substituting the new table.<br />

//To ensure that the table updates properly when adding Command tables or Stored

Procedures.<br />

boReportDocument.VerifyDatabase();<br />

<br />

return boReportDocument;<br />

}<br />

</code>

</p>