【问题标题】:Reportviewer shows blank report using dynamic datasetReportviewer 使用动态数据集显示空白报告
【发布时间】:2018-07-09 16:58:50
【问题描述】:

我关注了这个博客,并且能够在 ReportViewer 中成功地将动态数据集与 Tablix 绑定 - https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-dataset-dynamically-created-in-code-to-your-local-report-with-reportviewer/

代码:

private void button1_Click(object sender, EventArgs e)
        {
            //Check if both selection criteria are present
            if (comboBox1.Text == "" || comboBox2.Text == "")
            {
                MessageBox.Show("Please select employee", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            /* Generate payslip */

            //Get dept name and role
            DataSet dsDept = PanktiLa("select r.roledesc, d.depname from emprole r INNER JOIN deptmaster d on r.DEPSRNO = d.srno where r.empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));

            //Get PF no and ESI no
            //We assume 1 = PF & 2 = ESI in DocMaster (I would like to keep these two values permanent)
            string pf = "-";
            string esi = "-";
            DataSet dsDocs = PanktiLa("select * from empdocs where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
            if (dsDocs.Tables[0].Rows.Count > 0) //If there are records
                foreach (DataRow dr in dsDocs.Tables[0].Rows) //Cycle through dataset to find doc numbers
                {
                    if (dr["DOCSRNO"].ToString() == "1") //This is PF#
                        pf = dr["DOCDESC"].ToString();
                    if (dr["DOCSRNO"].ToString() == "2") //This is ESI#
                        esi = dr["DOCDESC"].ToString();
                }

            //Get bank ac no
            string khatano = "-";
            DataSet dsBank = PanktiLa("select ACNO from empbank where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
            if (dsBank.Tables[0].Rows.Count > 0) //If there are records
                khatano = dsBank.Tables[0].Rows[0]["ACNO"].ToString();

            /*===========================================================================*/
            //Populate earning & deduction datasets

            //To hold earnings
            DataTable dtE = new DataTable();
            dtE.Columns.Add("SALNAME");
            dtE.Columns.Add("AMT");

            //To hold deductions
            DataTable dtD = new DataTable();
            dtD.Columns.Add("SALNAME");
            dtD.Columns.Add("AMT");

            //Get all salary heads for the employee-month
            DataSet dsPYSalary = PanktiLa("select p.FKSRNO, p.SALCODE, p.AMT, s.SALNAME, s.SALTYPE from PYSalary p INNER JOIN SalaryMaster s ON s.SALCODE = p.SALCODE where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));

            //Segregate and label them based on earnings and deductions
            foreach (DataRow dr in dsPYSalary.Tables[0].Rows)
            {
                if (dr["SALTYPE"].ToString() == "Earning") //Head is of earning type
                {
                    //Populate earning dataset 'dsE'
                    DataRow drE = dtE.NewRow();
                    drE["SALNAME"] = dr["SALNAME"];
                    drE["AMT"] = dr["AMT"];
                    //dsE.Tables[0].Rows.Add(drE);
                    dtE.Rows.Add(drE);
                }
                else if (dr["SALTYPE"].ToString() == "Deduction") //Head is of deduction type
                {
                    //Populate deduction dataset 'dsD'
                    DataRow drD = dtD.NewRow();
                    drD["SALNAME"] = dr["SALNAME"];
                    drD["AMT"] = dr["AMT"];
                    //dsD.Tables[0].Rows.Add(drD);
                    dtD.Rows.Add(drD);
                }
            }

            //Populate OT
            //Get data
            DataSet dsPYOT = PanktiLa("select p.FKSRNO, p.OTSRNO, p.OQTY, p.OTAMT, m.OTID, m.OTNAME from PYOT p INNER JOIN OTMaster m ON p.OTSRNO = m.SRNO where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));

            foreach (DataRow dr in dsPYOT.Tables[0].Rows)
            {
                //Populate earning dataset 'dsE'
                DataRow drO = dtE.NewRow();
                drO["SALNAME"] = dr["OTNAME"];
                drO["AMT"] = dr["OTAMT"];
                dtE.Rows.Add(drO);
            }

            //Add table to dataset
            DataSet dsE = new DataSet();
            dsE.Tables.Add(dtE);
            DataSet dsD = new DataSet();
            dsD.Tables.Add(dtD);

            //Bind datasources
            dsE.Tables[0].TableName = "dtEarnings";
            this.dtEarningsBindingSource.DataSource = dsE;            
            dsD.Tables[0].TableName = "dtDeductions";
            this.dtDeductionsBindingSource.DataSource = dsD;
            /*===========================================================================*/

            //Define parameters
            ReportParameter _rpMonth = new ReportParameter("rpMonth", comboBox2.Text);
            ReportParameter _rpName = new ReportParameter("rpName", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPNAME"].ToString());
            ReportParameter _rpID = new ReportParameter("rpEmpID", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPID"].ToString());
            ReportParameter _rpDept = new ReportParameter("rpDept", dsDept.Tables[0].Rows[0]["DEPNAME"].ToString());
            ReportParameter _rpRole = new ReportParameter("rpRole", dsDept.Tables[0].Rows[0]["ROLEDESC"].ToString());
            ReportParameter _rpPF = new ReportParameter("rpPFNO", pf);
            ReportParameter _rpESI = new ReportParameter("rpESINO", esi);
            ReportParameter _rpBankAcNo = new ReportParameter("rpBankAcNo", khatano);

            //Set parameters
            reportViewer1.LocalReport.SetParameters(new ReportParameter[] 
            { _rpMonth, _rpName, _rpID, _rpDept, _rpRole, _rpPF, _rpESI, _rpBankAcNo });

            //Refresh report
            reportViewer1.RefreshReport();
        }

        private DataSet PanktiLa(string queryle)
        {
            string connString = System.IO.File.ReadAllText("D:\\Payroll\\Payroll.txt") + " User ID = sa; Password = DEMO";
            DataSet ds_local = new DataSet();

            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(queryle, conn);
                SqlDataAdapter adapter = new SqlDataAdapter();

                conn.Open();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds_local);

                return (ds_local);
            }
        }

tablix 甚至可以生成数据集中存在的行数。但它不会用数据集中的数据填充行。

我错过了什么? 注意: 我注意到虽然DataSet 绑定到dataTable 并依次绑定到ReportViewer,但@987654327 的DataSet Columns 之间没有一对一的绑定@ 和RDLC Report 的那个。那里有什么可以做的吗?

我在 C# 中使用 VS2015 和 SQL Server 2008 R2 Express 和 .Net 4.5.2 使用 WinForms 进行编码

【问题讨论】:

  • *Lot of code to populate the dataset* 我们是否应该假设您将数据放入DataAdapter 并绑定到DataSet?另外,this. 不应该在reportViewer1 上加上前缀吗?
  • @WEI_DBA 我编写了一个从数据库中获取记录并返回数据集的过程。该过程使用 DataAdapter。我不需要“这个”。因为还有其他参数在不使用“this”的情况下被填充。但是,在发表评论之前,我也尝试过使用“this”。但它仍然不起作用。另请注意,它会根据 DataSet 的“Rows.Count”创建 tablix 行数,但不会填充它们。所以这意味着它已正确绑定到 DataTable。唯一的问题是字段......它们不会被填充。
  • @WEI_DBA 我编写了一个从数据库中获取记录并返回数据集的过程。该过程使用 DataAdapter。我不需要this.,因为在不使用this. 的情况下会填充其他参数但是,在评论之前我也尝试过使用this.,但它仍然不起作用。另请注意,它会根据 DataSet 的 Rows.Count 创建 tablix 行数,但不会填充它们。所以这意味着它已正确绑定到 DataTable。唯一的问题是字段......它们不会被填充。

标签: c# sql-server rdlc reportviewer ssrs-tablix


【解决方案1】:

问题解决了。数据表的字段必须与代码中为数据集定义的列同名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多