Skip to Content
0
Jun 30, 2020 at 07:49 AM

Report from Visual studio code

109 Views Last edit Jul 06, 2020 at 03:03 PM 3 rev

Hi,

In my visual studio C# code I have created an API that creates a ReportDocument object. I am setting all the parameters, login credentials I am trying to login with a user who has access to 2 schemas. Both schemas have tables with same names. The report uses tables from schema2. When the code tries to generate memory stream of report I get "Field name is not known" error. This seems to be happening because the table the report is trying to use is from the schema1 which does not have all the fields the report is using. How can I make the report use particular schema. Is there a way I can set in my the .net code?

Here is my code snippet

ReportDocument rd = new ReportDocument();

var mappedPath = HttpContext.Current.Server.MapPath("~/Test");

rd.Load(Path.Combine(mappedPath ?? throw new InvalidOperationException(), "Test.rpt"));

ConnectionInfo connectioninfo = new ConnectionInfo();

connectioninfo.DatabaseName = "database1";

connectioninfo.UserID = "userabc";

connectioninfo.Password = "test123";

rd.DataSourceConnections[0].IntegratedSecurity = true;

Tables tables = rd.Database.Tables;

foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)

{

TableLogOnInfo tableLogonInfo = table.LogOnInfo;

tableLogonInfo.ConnectionInfo = connectioninfo;

table.ApplyLogOnInfo(tableLogonInfo);

}

rd.SetDatabaseLogon(connectioninfo.UserID, connectioninfo.Password, connectioninfo.DatabaseName, connectioninfo.DatabaseName, true);

rd.VerifyDatabase();

byte[] getBytes;

MemoryStream ms = (MemoryStream)rd.ExportToStream(ExportFormatType.PortableDocFormat);