该报告为空.代码有什么问题? [英] The report is empty. What's wrong with the code?
问题描述
这是我的加载方法:
这里的数据源是一个存储过程,我正在尝试从查询中获取值并将其作为参数发送给存储过程,这是我做错了吗?
This is my load method:
The datasource here is a stored procedure, I am trying to get values from the query and send it as a parameter to the stored procedure, What I am doing wrong?
protected void Page_Load(object sender, EventArgs e)
{
try
{
System.Data.SqlClient.SqlConnection myConnection =
new System.Data.SqlClient.SqlConnection( "user id=;" +
"password=;server=localhost;" +
"Trusted_Connection=yes;" +
"database=;" + "db;" +
"connection timeout=30");
myConnection.Open();
System.Data.SqlClient.SqlCommand myCommand_FirstName =
new System.Data.SqlClient.SqlCommand( "SELECT [Patient-ID] " +
"FROM [Patient-Info] " +
"WHERE [First Name]='" + Request.QueryString["param3"] +
"' AND [Last Name]='" + Request.QueryString["param4"] + "';"
, myConnection);
String patient_ID = "";
if (myCommand_FirstName.ExecuteScalar() != null)
{
patient_ID = myCommand_FirstName.ExecuteScalar().ToString();
}
CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
ParameterField paramField = new ParameterField();
ParameterFields paramFields = new ParameterFields();
paramField.Name = "@ID";
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramDiscreteValue.Value = patient_ID;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField = new ParameterField();
paramField.Name = "@Med";
paramDiscreteValue = new ParameterDiscreteValue();
paramDiscreteValue.Value = Request.QueryString["param1"];
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField = new ParameterField();
paramField.Name = "@Year";
paramDiscreteValue = new ParameterDiscreteValue();
paramDiscreteValue.Value = Request.QueryString["param2"];
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
reportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
CrystalReportViewer1.ParameterFieldInfo = paramFields;
CrystalReportViewer1.ReportSource = reportDocument;
myConnection.Close();
}
}
我尝试过的事情:
这是我定义CrystalReportViewer的地方:
What I have tried:
And this is where I have defined the CrystalReportViewer:
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" width="1000px" height="700px" runat="server" AutoDataBind="True" ToolPanelView="None" OnInit="CrystalReportViewer1_Init" DisplayToolbar="False" EnableDatabaseLogonPrompt="False" GroupTreeImagesFolderUrl="" ReportSourceID="CrystalReportSource1" ToolbarImagesFolderUrl="" ToolPanelWidth="200px" EnableParameterPrompt="False" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport1.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
推荐答案
第一件事,请确认您在Patient_ID中确实有一个值.如果它是一个空字符串,那么您的报告将为空白-除非您的数据具有空白记录ID,否则您将获得一个空白报告.
第二件事,请确保您具有报告文档.
Server.MapPath应该基于当前页面路径或网站的根目录引用报表的完整路径.
假设您具有以下结构;
www.mysite.com/mywebpage.aspx
www.mywebsite.com/Reports/CrystalReport1.rpt
要从mywebpage运行报告,您需要使用以下内容进行访问;
First thing, confirm you actually have a value in patient_ID. If it is an empty string then your Report will be blank which - unless you have data with a blank record Id - then you will get a blank report.
Second thing, ensure you have a Report document.
Server.MapPath should reference the full path to your report based on the current page path or the root of the web site.
Assume you have the following structure;
www.mysite.com/mywebpage.aspx
www.mywebsite.com/Reports/CrystalReport1.rpt
To run the report from mywebpage you would need access it using the following;
Server.MapPath("~/Reports/CrystalReport1.rpt");
始终使用波浪号(〜)字符从网站的根进行引用.
第三,您没有将参数或值传递给报告.您正在创建离散参数,但是将它们添加到新的ParameterFields集合中,而不是将其添加到Reports ParameterFields集合中.
如果您在Crystal Report&它会提示您输入参数,然后您需要执行以下操作;
Always reference from the root of your website by using the tilde (~) character.
Third, you are not passing the parameters or values to the report. You are creating discrete parameters but you are adding them to a new ParameterFields collection, not the Reports ParameterFields collection.
If you run the Report in Crystal Report & it prompts for the parameters then you need to do as follows;
foreach(CrystalDecsions.CrystalReports.Engine.ParameterFieldDefinition objParam in reportDocument.DataDefinition.ParameterFields)
{
// confirm you are accessing a main report parameter and not a sub report parameter
if(objParam.ReportName == string.empty)
{
string strParamName = objParam.ParameterFieldName;
reportDocument.SetParameterValue(strParamName, "myValue");
}
}
我个人不向报表添加谨慎的参数,因为报表随后可能会加载所有数据,然后隐藏"不符合所提供值的数据-对于子报表尤其如此.这确实不利于大型数据集的性能.允许使用通配符,通配符可以允许用户获取您不希望使用的通配符.
由于固有的性能和安全性优势,我的报表"通常从存储过程中检索数据.
亲切的问候
I personally do not add discreet parameters to a Report because the Report may then load all of the data and then "hide" the data that does not meet the provided values - this is especially true with sub-reports. This is really bad for performance on big datasets & allows for the use of wildcards, which can allow the user to get data that you did not intend them to have.
My Reports typically retrieve data from a stored procedure due to the inherent performance and security benefits.
Kind Regards
这篇关于该报告为空.代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!