简体   繁体   English

如何在Visual Studio 2013中查看Crystal报表?

[英]How to View Crystal report in Visual Studio 2013?

I'm working on ASP.Net with C#, and installed CrystalReport 13 for Visual Studio 2013. I wrote code to fill a Datasource and pass it into my `CrystalReport file, but my website doesn't show anything. 我正在ASP.Net与C#,并安装CrystalReport 13适用于Visual Studio 2013年我写代码来填充一个Datasource ,并把它传递到我的`CrystalReport文件,但我的网站不显示任何东西。 How can I execute my report? 如何执行报告?

Here is my PageLoad: 这是我的PageLoad:

SqlConnection cnn = new SqlConnection(connectionstring);
cnn.Open();
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
command.Connection = cnn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "STP_T00050030";
command.Parameters.Add("@F_CNTRowID", SqlDbType.Int).Value = Convert.ToInt32("20");
command.Parameters.Add("@F_CNTid", SqlDbType.Int).Value = Convert.ToInt32("45");
adapter = new SqlDataAdapter(command);
adapter.Fill(ds);

ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/Report1.rpt"));
crystalReport.SetDataSource(ds);
CrystalReportViewer1.ReportSource = crystalReport;
  1. In your solution, right mouse click on each report (.rpt file) and go properties --> Build Action=Content, Copy To Output Directory=Copy Always 在您的解决方案中,右键单击每个报告(.rpt文件)并转到属性-> Build Action = Content,复制到Output Directory = Copy Always

  2. Here's a complete block of code for setting data source. 这是用于设置数据源的完整代码块。 Notice how data source must also be set for each subreport. 请注意,还必须如何为每个子报表设置数据源。

     public static CrystalReportSource Crystal_SetDataSource(string reportName, object par1, object par2, object par3) { CrystalReportSource CrystalReportSource2 = new CrystalReportSource(); CrystalReportSource2.CacheDuration = 10; CrystalReportSource2.Report.FileName = @"~\\App_Pages\\Secure\\Reports\\" + reportName; CrystalDecisions.CrystalReports.Engine.Database crDatabase = CrystalReportSource2.ReportDocument.Database; ConnectionInfo crConnectionInfo = new ConnectionInfo(); crConnectionInfo.ServerName = server; crConnectionInfo.DatabaseName = db; crConnectionInfo.UserID = crystalUser; crConnectionInfo.Password = pwd; crConnectionInfo.IntegratedSecurity = false; // First we assign the connection to all tables in the main report TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo(); foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crDatabase.Tables) { crTableLogOnInfo = crTable.LogOnInfo; crTableLogOnInfo.ConnectionInfo = crConnectionInfo; crTable.ApplyLogOnInfo(crTableLogOnInfo); } //SET DATASOURCE FOR EACH SUBREPORT IN REPORT foreach (CrystalDecisions.CrystalReports.Engine.Section section in CrystalReportSource2.ReportDocument.ReportDefinition.Sections) { // In each section we need to loop through all the reporting objects foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in section.ReportObjects) { if (reportObject.Kind == ReportObjectKind.SubreportObject) { SubreportObject subReport = (SubreportObject)reportObject; ReportDocument subDocument = subReport.OpenSubreport(subReport.SubreportName); foreach (CrystalDecisions.CrystalReports.Engine.Table table in subDocument.Database.Tables) { // Cache the logon info block TableLogOnInfo logOnInfo = table.LogOnInfo; // Set the connection logOnInfo.ConnectionInfo = crConnectionInfo; // Apply the connection to the table! table.ApplyLogOnInfo(logOnInfo); } } } } if (CrystalReportSource2.ReportDocument.DataSourceConnections.Count > 0) CrystalReportSource2.ReportDocument.DataSourceConnections[0].SetConnection(server, db, crystalUser, pwd); CrystalReportSource2.ReportDocument.SetDatabaseLogon(crystalUser, pwd, server, db); if (par1 != null) CrystalReportSource2.ReportDocument.SetParameterValue(0, par1); if (par2 != null) CrystalReportSource2.ReportDocument.SetParameterValue(1, par2); if (par3 != null) CrystalReportSource2.ReportDocument.SetParameterValue(2, par3); return CrystalReportSource2; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM