ASP.NET 报告 - 设置参数

本文关键字:设置 参数 报告 NET ASP | 更新日期: 2023-09-27 17:55:39

我有一个名为"Graph"的存储过程,它应该获取参数@Material的值,我在 ASP.NET 中创建了一个报表,该报表应使用存储过程中的数据显示图表。

但是,当我尝试加载报告时,我得到:

"数据不可用"

意思是,参数设置不正确(默认值有效...

代码隐藏:

public partial class StatisticsPage : Page
{
    string Connectionstring = "server=(local)''SQLEXPRESS;database=PhilipsMaterials;Integrated Security=SSPI";
    protected void btndisplay_Click(object sender, EventArgs e)
    {
        BindReport();
    }
    private void BindReport()
    {
        SSRSReport report = new SSRSReport();
        SqlParameter[] sqlParams = new SqlParameter[] {
         new SqlParameter("@Material","453567068441") };
        string ReportDataSource = "DataSet1";
        bool bind = report.CreateReport(Connectionstring, "graph", sqlParams, ref ReportViewer1, ReportDataSource);
        if (bind)
        {
            ReportViewer1.Visible = true;
        }
    }
}
public class SSRSReport
{     
    public bool CreateReport(String Connectionstring, string StoreProcedureName , SqlParameter[] parameters, ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer, string ReportDataSource)
    {
        bool reportbind = false;
        using (SqlConnection con = new SqlConnection(Connectionstring))
        {
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = StoreProcedureName;
            com.Parameters.AddRange(parameters);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(com);
            da.Fill(ds);
            ReportDataSource datasource = new ReportDataSource(ReportDataSource, ds.Tables[0]);
            if ( ds.Tables[0].Rows.Count > 0)
            {
                ReportViewer.LocalReport.DataSources.Clear();
                ReportViewer.LocalReport.DataSources.Add(datasource);
                ReportParameter myParam = new ReportParameter("Material", "453567068441");
                ReportViewer.LocalReport.SetParameters(myParam);
                ReportViewer.ServerReport.Refresh();
                reportbind = true;
            }                       
        }
       return reportbind;
    }
}

标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StatisticsPage.aspx.cs" Inherits="StatisticsPage" %>
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test SSRS</title>
</head>
<body>
      <form id="form1" runat="server">
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="610px" Width="1179px" ShowParameterPrompts="true">
            <LocalReport ReportPath="Report.rdlc" >
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" >
            <SelectParameters>
                <asp:Parameter Name="Material"/>
          </SelectParameters>
          </asp:ObjectDataSource>
          <asp:ScriptManager ID="ScriptManager1" runat="server">
          </asp:ScriptManager>
    </form>
</body>
</html>

ASP.NET 报告 - 设置参数

如果在没有默认参数的情况下发生错误,则问题是<asp:parameter>不会提供值,除非您提供值。 可以从标记或代码设置 DefaultValue 属性,此值将直接传递到后端。 您还可以切换到ControlParameter,SessionParameter等,从其他地方提取值。