Data Connectivity
 Crystal Reports Forum : Crystal Reports for Visual Studio 2005 and Newer : Data Connectivity
Message Icon Topic: ExportToStream / Logon Failed Post Reply Post New Topic
Author Message
ckapilla
Newbie
Newbie


Joined: 29 Aug 2007
Location: United States
Online Status: Offline
Posts: 10
Quote ckapilla Replybullet Topic: ExportToStream / Logon Failed
    Posted: 29 Aug 2007 at 4:58pm
Hi, in ASP.Net page I opened a form from a SQL database ok. I click on an export button that tries  to call the ExportToStream function, but it throws an OLE DB exception saying Logon Failed. I don't understand why I'm getting that error at this point of the process. My ConnectionInfo credentials were set up ok to open the form in the first place.
 
I just ordered the book, but am hoping someone can help while I'm waiting for it to arrive.
 
thanks
 
Chris K
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 04 Sep 2007 at 4:28pm
Try refreshing the report - I'm willing to bet you'll get the same error.  Then check the report to see if it has "Save Data with Report" turned on.  If it does then what you're seeing is the data that was saved with the report, not data from the database, and the report doesn't have to connect to the database to display it.
 
-Dell
 
 
IP IP Logged
ckapilla
Newbie
Newbie


Joined: 29 Aug 2007
Location: United States
Online Status: Offline
Posts: 10
Quote ckapilla Replybullet Posted: 04 Sep 2007 at 5:35pm
Well I wish you would have won your bet, but alas no.
 
Here is the relevant code: (sorry for the excess line breaks)
 

private void ConfigureCrystalReports()

{

agentMultiReport = new ReportDocument();

agentMultiReport.Load(Server.MapPath("agentMultiReport.rpt"));

crystalReportViewer1.ReportSource = agentMultiReport;

ConnectionInfo connectionInfo = new ConnectionInfo();

connectionInfo.ServerName = "Server";

connectionInfo.DatabaseName = "DB";

connectionInfo.UserID = "user";

connectionInfo.Password = "pwd";

TableLogOnInfos tableLogOnInfos = crystalReportViewer1.LogOnInfo;

foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)

  {

    tableLogOnInfo.ConnectionInfo = connectionInfo;

  }

}

Then

protected void Button1_Click(object sender, System.EventArgs e)

{

  try

  {

  MemoryStream oStream; // using System.IO

  oStream = (MemoryStream)agentMultiReport.ExportToStream(

  ExportFormatType.PortableDocFormat);

 

The initial logon works (and works after a refresh and fails if I put in a bad password) the second logon fails (and I'm not sure why there is even a logon taking place at that point).

thanks
 
Chris
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 05 Sep 2007 at 7:23am

I think I see what the problem is!

You're setting the logins for the report in the viewer, NOT the report object that you're exporting.  When you assign the report object to the ReportSource property of the viewer, the viewer has a copy of the report and loses its links to the report object.  So, if the user has set any parameter values for the report in the viewer, those parameters have not been set for the report object.
 
Here's code for setting the logins at the report object level before you assign it to the ReportSource:
CrystalDecisions.Shared.ConnectionInfo connectionInfo =
    new CrystalDecisions.Shared.ConnectionInfo();
connectionInfo.ServerName = "server";
connectionInfo.DatabaseName = "DB"
connectionInfo.UserID = "user";
connectionInfo.Password = "PW";
// set report connection for main report
SetDBLogonForReport(connectionInfo, crReport, qServer);
// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport, qServer);
private void SetDBLogonForReport
  (CrystalDecisions.Shared.ConnectionInfo connectionInfo,
    ReportDocument reportDocument, string qServer)
  {
    Tables tables = reportDocument.Database.Tables;
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
      TableLogOnInfo tableLogonInfo = table.LogOnInfo;
      tableLogonInfo.ConnectionInfo = connectionInfo;
      table.ApplyLogOnInfo(tableLogonInfo);
    }
  }
private void SetDBLogonForSubreports
  (CrystalDecisions.Shared.ConnectionInfo connectionInfo,
   ReportDocument reportDocument, string qServer)
  {
    Sections sections = reportDocument.ReportDefinition.Sections;
    foreach (Section section in sections)
    {
      ReportObjects reportObjects = section.ReportObjects;
      foreach (ReportObject reportObject in reportObjects)
      {
        if (reportObject.Kind == ReportObjectKind.SubreportObject)
        {
          SubreportObject subreportObject = (SubreportObject)reportObject;
          ReportDocument subReportDocument = 
            subreportObject.OpenSubreport(subreportObject.SubreportName);
          SetDBLogonForReport(connectionInfo, subReportDocument, qServer);
        }
      }
    }
 }

Also, you could try changing the button click code to do something like this:

MemoryStream oStream; // using System.IO
oStream = (MemoryStream) ((ReportDocument) 
  crystalReportViewer1.ReportSource.ExportToStream(
   
ExportFormatType.PortableDocFormat));
 
Good luck!
 
-Dell
IP IP Logged
ckapilla
Newbie
Newbie


Joined: 29 Aug 2007
Location: United States
Online Status: Offline
Posts: 10
Quote ckapilla Replybullet Posted: 05 Sep 2007 at 8:19am
Ah, you are truly a gentelman and a scholar! The first bit of code worked like a charm. Thanks so much for your help, and the explanation of what was going on in my code.
 
Chris
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 05 Sep 2007 at 8:31am
Actually, it would be "lady and a scholar".  Tongue
 
I'm glad that it worked for you!
 
-Dell
IP IP Logged
ckapilla
Newbie
Newbie


Joined: 29 Aug 2007
Location: United States
Online Status: Offline
Posts: 10
Quote ckapilla Replybullet Posted: 05 Sep 2007 at 8:38am
well thanks for straightening that out for me!
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.030 seconds.