报告未显示在报告查看器上[英] Report not showing up on report viewer

本文是小编为大家收集整理的关于报告未显示在报告查看器上的处理方法,想解了报告未显示在报告查看器上的问题怎么解决?报告未显示在报告查看器上问题的解决办法?报告未显示在报告查看器上问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我正在开发c#需要生成报告的应用程序.我是一个使用来自A 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();
            }
        }

推荐答案

为什么您要浏览报告中的所有经理?可能是您正在寻找包含所有经理数据的报告.

您正在为每个经理循环吗?

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();

其他推荐答案

在您的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.但是,如果您不想传递参数,则只需将命令类型更改为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

本文地址:https://www.itbaoku.cn/post/269261.html

问题描述

I am developing a c# application where I need to generate a report. I am a using a dataset which is filled with the data coming from a stored procedure which takes one parameter from the C# code. I am creating a parameter in report1.rdlc and populating it with the data from a text box. When I run the application I can’t see anything on report viewer.

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();
            }
        }

推荐答案

Why are you looping through all the Managers in the Report?? May be you are looking for a report which contains all the Managers Data.

You are looping the Same Report for each Manager???

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();

其他推荐答案

To use the report viewer in your C# WinForm project add the following code to a button or inthe 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();

Assuming that you have a stored procedure that accepts @ProjectID parameter. You have to set the cmd.CommandType = CommandType.Text if you pass these parameters along with the sql command. However, if you don't want to pass parameters just change the commandType to cmd.CommandType = CommandType.StoredProcedure.

Below is the stored procedure used in this example:

CREATE PROC [dbo].[sp_GetProject]
    @ProjectID      nvarchar(50)=NULL

AS
BEGIN

   SELECT ProjectID, ProjectName FROM Projects
    WHERE 
    (@ProjectID IS NULL) OR (ProjectID = @ProjectID)

END
查看更多