将参数存储在报表查看器的变量中

时间:2013-12-09 22:58:48

标签: asp.net sql c#-4.0

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {   ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            ReportViewer1.ServerReport.ReportServerUrl = new Uri(@"http://123.abc/Reportserver");
            ReportViewer1.ServerReport.ReportPath = "/Test/Development/Details";
            ReportViewer1.ShowParameterPrompts = true;
            ReportViewer1.ShowPrintButton = true;


            ReportParameter[] rptParams = new ReportParameter[1];
            SqlConnection MA = new SqlConnection(MedA);
            SqlCommand command = new SqlCommand("select distinct Users from PROD.Users Where Username ='" + this.Username + "'", MA);         
            SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            dataAadpter.Fill(ds);
            rptParams[0] = new ReportParameter("UserID", ds); //this.Username
            ReportViewer1.ServerReport.SetParameters(rptParams);
            ReportViewer1.ServerReport.Refresh();
            ReportViewer1.AsyncRendering = false;
            ReportViewer1.SizeToReportContent = true;
        }

我无法传递参数。我试图从登录用户获取用户密钥。任何人都可以帮助我

1 个答案:

答案 0 :(得分:1)

首先在登录屏幕上存储用户名,如下所示:

Session["LoggedInUser"] = txtUsername.Text.Trim();

然后将以下代码粘贴到您的报告页面中:

  protected String Username
    {
        get
        {
            return Convert.ToString(Session["LoggedInUser"]);
        }
    }

在页面加载中,为您报告以下代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {   ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                ReportViewer1.ServerReport.ReportServerUrl = new Uri(@"http://123.abc/Reportserver");
                ReportViewer1.ServerReport.ReportPath = "/Test/Development/Details";
                ReportViewer1.ShowParameterPrompts = true;
                ReportViewer1.ShowPrintButton = true;

        ReportParameter[] rptParams = new ReportParameter[1];
        SqlConnection MA = new SqlConnection(MedA);
        SqlCommand command = new SqlCommand("select distinct Users from PROD.Users Where Username ='" + this.Username + "'", MA);         
        SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        dataAadpter.Fill(ds);
        rptParams[0] = new ReportParameter("UserID", ds.Tables[0].Rows[0][0].ToString()); //this.Username
        ReportViewer1.ServerReport.SetParameters(rptParams);
        ReportViewer1.ServerReport.Refresh();
        ReportViewer1.AsyncRendering = false;
        ReportViewer1.SizeToReportContent = true;
    }