Report not showing up on report viewer

夙愿已清 提交于 2019-12-11 19:36:45

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/17101524/report-not-showing-up-on-report-viewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!