Report Viewer Control - 将参数发送到DataSet中的存储过程

时间:2013-09-13 20:04:09

标签: asp.net stored-procedures parameters reportviewer

我在ASP.NET Web表单应用程序中创建了一个本地.rdlc报告。

我想使用ReportViewer Control加载报告。我使用数据库中的存储过程获取Report DataSet的数据。存储过程需要一个参数来从表中返回单个数据行。我希望能够以编程方式将此参数传递给存储过程,以便报告将加载数据。

我如何做到这一点?

2 个答案:

答案 0 :(得分:2)

ASPX上的reportViewer

<rsweb:ReportViewer ID="ReportViewer1" runat="server" SizeToReportContent="True">
</rsweb:ReportViewer>

获取数据集的方法

private DataTable GetSPResult()
{
    DataTable ResultsTable = new DataTable();

    SqlConnection conn = new SqlConnection(ConfigurationManager
      .ConnectionStrings["connectionString"].ConnectionString);

    try
    {
        SqlCommand cmd = new SqlCommand("yourStorePorcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", 1);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ResultsTable);
    }

    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }

    return ResultsTable;
}

将结果绑定到reportviewer

DataTable dt = GetSPResult();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));

答案 1 :(得分:0)

 DataTable dtval = new DataTable();

//添加参数

 SqlParameter[] param = new SqlParameter[5];
 if (!String.IsNullOrEmpty(ddlcust.SelectedValue))
 { param[0] = new SqlParameter("@custnam", ddlcust.SelectedValue); }
 else { param[0] = new SqlParameter("@custnam", DBNull.Value); }
 if (!string.IsNullOrEmpty(ddlproj.SelectedValue))
 { param[1] = new SqlParameter("@projid", Convert.ToInt32(ddlproj.SelectedValue)); }
 else { param[1] = new SqlParameter("@projid", DBNull.Value); }
 if (!string.IsNullOrEmpty(ddlvisittype.SelectedItem.Text))
 { param[2] = new SqlParameter("@vtype", ddlvisittype.SelectedItem.Text); }
 else { param[2] = new SqlParameter("@vtype", DBNull.Value); }
 param[3] = new SqlParameter("@frmdt", txtfrmdate.Text);
 param[4] = new SqlParameter("@todt", txttodate.Text);
 //Call storedprocedure with parameter
 dtval = objDal.Getvalue("[Usp_CRMRpt]", param);
 if (dtval != null && dtval.Rows.Count > 0)
 {

//加载到reportviewer

 RptCrm.Visible = true;
 RptCrm.LocalReport.DataSources.Clear();
 ReportDataSource rds = new ReportDataSource("dsCrmrpt", dtval);
 RptCrm.LocalReport.ReportPath = "Report1.rdlc";
 RptCrm.LocalReport.DataSources.Add(rds);
 RptCrm.DataBind();
 RptCrm.LocalReport.Refresh();
 }
 else
 { RptCrm.Visible = false; }