0

我有一个在reportviewer 中显示reportserver RDL 报表的现有代码。我提示用户输入,然后将这些作为参数传递给 Sql Server 中的存储过程。

该报告正确生成为带有硬编码文本框数量的概念证明,但显然并非所有报告都具有相同数量的参数。所以这是我的工作代码和屏幕截图:

工作报告

ASP.NET

<%@ Page Language="C#" Trace="true" AutoEventWireup="true" CodeBehind="Reports.aspx.cs" Inherits="FormsAuthAd.Reports" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" class="reports-body">
<head runat="server">
    <title>Reports</title>
    <link href="../css/Style.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div class="params-container container">
        <asp:TextBox ID="txtStart" runat="server" placeholder="From Date" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtEnd" runat="server" placeholder="To Date" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtBrand" runat="server" placeholder="Brand" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtSeason" runat="server" placeholder="Season" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtComp" runat="server" placeholder="Company" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtLoc" runat="server" placeholder="Location" class="input-fields"></asp:TextBox>
        <asp:TextBox ID="txtLine" runat="server" placeholder="Line" class="input-fields"></asp:TextBox>
        <asp:Button ID="btnShow" runat="server" OnClick="Button1_Click" Text="Generate"  class="input-fields"/>
    </div>
    <div class="rpt-container container">
        <rsweb:ReportViewer ID="rptVwr" runat="server" CssClass="rpt-viewer"></rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>

代码背后

private void showReport()
{
    rptVwr.Reset();
        DataTable dt = GetData(txtStart.Text, txtEnd.Text, txtBrand.Text, txtSeason.Text, txtComp.Text, txtLoc.Text);
        rptVwr.Visible = true;
        ReportDataSource rds = new ReportDataSource("DataSet2", dt);
        rptVwr.LocalReport.DataSources.Add(rds);
        rptVwr.LocalReport.ReportPath = "../ReportServer/StockExport.rdl";

        ReportParameter[] rptParams = new ReportParameter[]
        {
        new ReportParameter("StartDate",txtStart.Text),
                new ReportParameter("EndDate",txtEnd.Text),
                new ReportParameter("Brand",txtBrand.Text),
                new ReportParameter("Season",txtSeason.Text),
                new ReportParameter("Company",txtComp.Text),
                new ReportParameter("Store",txtLoc.Text),
        };
        rptVwr.LocalReport.SetParameters(rptParams);
        rptVwr.LocalReport.Refresh();
}

private DataTable GetData(string fromDate, string toDate, string brandCode, string seasonCode, string compCode, string locCode)
{
    DataTable dt = new DataTable();
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings ["ReportServerConnStr"].ConnectionString;
        using (SqlConnection cn = new SqlConnection(connStr))
        {
            SqlCommand cmd = new SqlCommand("sp_valueEntryStock", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = fromDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = toDate;
                cmd.Parameters.Add("@Season", SqlDbType.NVarChar).Value = seasonCode;
                cmd.Parameters.Add("@Brand", SqlDbType.NVarChar).Value = brandCode;
                cmd.Parameters.Add("@Company", SqlDbType.NVarChar).Value = compCode;
                cmd.Parameters.Add("@Store", SqlDbType.NVarChar).Value = locCode;
                cmd.CommandTimeout = 1000;
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(dt);
    }
        return dt;

}

protected void Button1_Click(object sender, EventArgs e)
{
    showReport();
}

显然,我不想要硬编码的文本框数量,因此我调整了代码以根据 Sql Server 中的表在页面加载时动态添加文本框。这是我现有的代码和屏幕截图:

提交参数后的页面

代码背后

protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT [FieldNum],[FieldName],[AspId],[SqlParameter] FROM [MIS_FieldsMaster]", con))
                {
                    cmd.CommandType = CommandType.Text;
                        SqlDataAdapter adp = new SqlDataAdapter(cmd);
                        adp.Fill(dtable);
                        int i = 0;
                        foreach (DataRow dr in dtable.Rows)
                        {
                            TextBox tb = new TextBox();
                            tb.ID = dr["AspId"].ToString().Trim();
                            stringArray[i] = dr["SqlParameter"].ToString().Trim();
                            tb.Text = stringArray[i];
                            ph1.Controls.Add(tb);
                            i++;
                        }
                }
    }

}

private void showReport()
{
    rptVwr.Reset();
        DataTable dt = GetData();
        rptVwr.Visible = true;
        ReportDataSource rds = new ReportDataSource("DataSet2", dt);
    rptVwr.LocalReport.DataSources.Add(rds);
    rptVwr.LocalReport.ReportPath = "../ReportServer/StockExport.rdl";
        rptVwr.LocalReport.Refresh();
}

private DataTable GetData()
{
    DataTable dt = new DataTable();
        int i = 0;

        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ReportServerConnStr"].ConnectionString;
        using (SqlConnection cn = new SqlConnection(connStr))
        {
        SqlCommand cmd = new SqlCommand("sp_valueEntryStock", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                foreach (TextBox textBox in ph1.Controls.OfType<TextBox>())
                {
                    cmd.Connection = cn;
            cmd.Parameters.AddWithValue("@" + stringArray[i], textBox.Text);
                    i++;
                }
                cmd.CommandTimeout = 1000;
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(dt);
    }
            return dt;
}

protected void Button1_Click(object sender, EventArgs e)
{
    showReport();
}

所以它甚至没有向我显示任何错误消息,所以我真的不知道在哪里看。我调试了新代码,我相信它正在检索存储过程中的数据,因为局部变量显示了在数据表 dt 下检索到的行。唯一的问题是报告没有显示在reportViewer 中。它甚至不显示报告工具栏。

从数据表 dt 检索到的行

我将不胜感激任何帮助。谢谢。

4

1 回答 1

0

我找出了报告未在报告查看器中显示的原因。

看来我仍然需要添加参数并将其设置为reportviewer,就像在我的第一个工作代码中一样:

ReportParameter[] rptParams = new ReportParameter[]
{
new ReportParameter("StartDate",txtStart.Text),
        new ReportParameter("EndDate",txtEnd.Text),
        new ReportParameter("Brand",txtBrand.Text),
        new ReportParameter("Season",txtSeason.Text),
        new ReportParameter("Company",txtComp.Text),
        new ReportParameter("Store",txtLoc.Text),
};
rptVwr.LocalReport.SetParameters(rptParams);

显然=,它仍然需要是动态的,所以这是我使用的代码:

List<ReportParameter> paramList = new List<ReportParameter>();
int i = 0;

foreach (TextBox textBox in ph1.Controls.OfType<TextBox>())
{
    paramList.Add(new ReportParameter(stringArray[i], textBox.Text));
    i++;
}
rptVwr.LocalReport.SetParameters(paramList);

现在,我能够生成动态文本框,同时将这些文本框中的值传递给我的 sql server 存储过程,最后生成并显示基于相同文本框的报告。

我认为这已经解决了,但我脑子里只有一个琐碎的问题:

为什么我的代码与我采用的 youtube 示例中的代码完全相同(至少在逻辑上,如果不是语法上),为什么不能工作:在存储过程中传递参数并使用 C# 在报表查看器中显示输出的两种方法

于 2018-02-25T09:16:55.483 回答