Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to use crystal report from with in VS2005

Status
Not open for further replies.

iworkonline

Programmer
Mar 8, 2010
1
US
Hello

I have plenty of reports that are created in Crystal Reports v12. Now I am designing a windows application so the user can select different reports to run it. I have been going through the links at the following site


but I cannot find a link that would answer my question.

So far I have added the crystal reprots viewer control on to my form and set the report source proprty to the physical path, and the reports fine, but I also want the user to change the database settings according to their environment. How will I do that. I am using VS2005. Thanks.
 
The way that I usually handle this is to load the .rpt file into a ReportDocument object, change the various properties - parameters, table logons, etc. - and then set the ReportSource of the viewer to the ReportDocument.

Here is sample C# code for doing this:
Code:
ReportDocument crReport = new ReportDocument();
crReport.Load(<filename>);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = <server>;
connectionInfo.DatabaseName = <db name>;
connectionInfo.UserID = <userID>;
connectionInfo.Password = <password>;

// set data connections for main report
SetDBLogonForReport(connectionInfo, crReport);

// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport);

// view reportcrystalReportViewer.ReportSource = crReport;

-------------
private void SetDBLogonForReport(    CrystalDecisions.Shared.ConnectionInfo connectionInfo,     ReportDocument reportDocument)
{  
  Tables tables = reportDocument.Database.Tables;
  foreach (Table table in tables)    
  {      
    TableLogOnInfo tableLogonInfo = table.LogOnInfo;
    tableLogonInfo.ConnectionInfo = connectionInfo;      
    table.ApplyLogOnInfo(tableLogonInfo);      
  }
}
-----------
private void SetDBLogonForSubreport(    CrystalDecisions.Shared.ConnectionInfo connectionInfo,    ReportDocument reportDocument)
{
  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);
      }
    }
  }
}

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top