【问题标题】:Report not showing up on report viewer报告未显示在报告查看器中
【发布时间】:2013-06-14 05:15:27
【问题描述】:

我正在开发一个需要生成报告的c# 应用程序。我正在使用一个数据集,其中填充了来自 stored procedure 的数据,该数据集从 C# 代码中获取一个参数。我在report1.rdlc 中创建一个参数,并用文本框中的数据填充它。当我运行应用程序时,我在报表查看器中看不到任何内容。

public void GenerateBranchwiseReport()
        {
            conn.Open();
            SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
            SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
            while (BranchReportread.Read())
            {
                BranchManagerName.Add(BranchReportread.GetValue(0).ToString());
            }
            conn.Close();
            foreach (string managername in BranchManagerName)
            {
                conn.Open();
                SqlCommand GetReportDatacmd = new SqlCommand();
                GetReportDatacmd.CommandText = "USP_BranchwiseReport";
                GetReportDatacmd.CommandType = CommandType.StoredProcedure;
                GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", managername));
                GetReportDatacmd.Connection = conn;
                GetReportDatacmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                conn.Close();
                reportViewer1.Reset();
                this.reportViewer1.Visible = true;
                string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
                this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
                ReportParameter[] param = new ReportParameter[1];
                param[0] = new ReportParameter("ManagerName", managername);
                this.reportViewer1.LocalReport.SetParameters(param);
                ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
                this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
                //this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
                this.reportViewer1.LocalReport.Refresh();
                //SendEmail();
            }
        }

【问题讨论】:

  • 向我们展示您的代码。我们无法读懂您的想法。
  • 投反对票的任何理由!!!

标签: c# asp.net sql-server rdlc report-viewer2010


【解决方案1】:

您为什么要遍历报告中的所有经理?可能您正在寻找包含所有经理数据的报告。

您正在为每个经理循环相同的报告???

public void GenerateBranchwiseReport()
{
string ManagerNames="";
    conn.Open();
    SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
    SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
    while (BranchReportread.Read())
    {
     if (ManagerNames.length==0)
         ManagerNames="'"+BranchReportread.GetValue(0).ToString()+"'";
     else
         ManagerNames =ManagerNames + ",'" + BranchReportread.GetValue(0).ToString()+ "'";

    }
    conn.Close();


  //So now, You have Multiple Managers in ManagerNames string You can send it to SQL, which must used in Where clause with IN (@Managers) in you Stored Procedure.

        conn.Open();
        SqlCommand GetReportDatacmd = new SqlCommand();
        GetReportDatacmd.CommandText = "USP_BranchwiseReport";
        GetReportDatacmd.CommandType = CommandType.StoredProcedure;
        GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", ManagerNames));
        GetReportDatacmd.Connection = conn;
        GetReportDatacmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        reportViewer1.Reset();
        this.reportViewer1.Visible = true;
        string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
        this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
        ReportParameter[] param = new ReportParameter[1];
        param[0] = new ReportParameter("ManagerName", managername);
        this.reportViewer1.LocalReport.SetParameters(param);
        ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
        this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
        this.reportViewer1.LocalReport.DataSources.Clear();
        this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
        //this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
        this.reportViewer1.LocalReport.Refresh();
        //SendEmail();

【讨论】:

    【解决方案2】:

    要在 C# WinForm 项目中使用报表查看器,请将以下代码添加到按钮或 form_load 中:

        string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True";
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = new SqlConnection (strConnectionString);
        da.SelectCommand = cmd;
    
        da.Fill(ds,"DataSet1");
    
        reportViewer1.ProcessingMode = ProcessingMode.Local;
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));
        reportViewer1.LocalReport.Refresh();
        reportViewer1.RefreshReport();
    

    假设您有一个接受@ProjectID 参数的存储过程。 如果将这些参数与 sql 命令一起传递,则必须设置 cmd.CommandType = CommandType.Text。但是,如果您不想传递参数,只需将 commandType 更改为 cmd.CommandType = CommandType.StoredProcedure

    以下是本例中使用的存储过程:

    CREATE PROC [dbo].[sp_GetProject]
        @ProjectID      nvarchar(50)=NULL
    
    AS
    BEGIN
    
       SELECT ProjectID, ProjectName FROM Projects
        WHERE 
        (@ProjectID IS NULL) OR (ProjectID = @ProjectID)
    
    END
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      相关资源
      最近更新 更多