如何修复设置报告参数时出现的rdlc本地报告处理错误

时间:2019-10-02 12:35:09

标签: c# sqlite rdlc

我想保存rdlc报表而不使用report-viewer,并且没有报表参数也成功了,因为当我尝试向报表中添加报表参数时出现“报表处理错误”。

我试图保存不带参数的报告,但是报告起作用了,但是一旦添加参数,它就会产生此错误。

try
{
                using (IDbConnection db = new SQLiteConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString))
                {
                    if (db.State == ConnectionState.Closed)
                    {
                        db.Open();
                    }
                    string getOrderItems = "Select * from OrderItems where OrderId = 20";
                    List<OrderItem> orderItems = db.Query<OrderItem>(getOrderItems, commandType: CommandType.Text).ToList();
                    OrderItemBindingSource.DataSource = orderItems;
                }
                LocalReport orderReceipt = new LocalReport();
                orderReceipt.ReportPath = @"D:\ADiTM\Products\ADiTmPoS\ADiTmPoS\Reports\OrderReceipt.rdlc";
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "orderItem";
                rds.Value = this.OrderItemBindingSource;
                orderReceipt.DataSources.Add(rds);
                ReportParameter[] parameters = new ReportParameter[]
                {
                    new ReportParameter("CustomerName",orderDetail.CustomerName),
                    new ReportParameter("CustomerContactNumber",orderDetail.CustomerContactNumber),
                    new ReportParameter("CustomerAddress",orderDetail.CustomerAddress),
                    new ReportParameter("OrderId",orderDetail.Id.ToString()),
                    new ReportParameter("Date",orderDetail.Date.ToString("MM/DD/yyyy")),
                    new ReportParameter("Status",orderDetail.Status),
                    new ReportParameter("StaffName",orderDetail.StaffName),
                    new ReportParameter("StaffMobileNumber",orderDetail.StaffMobileNumber),
                    new ReportParameter("UserName",orderDetail.UserName)

                };
                orderReceipt.SetParameters(parameters);

                Byte[] mybytes = orderReceipt.Render("PDF");
                using (var fbd = new FolderBrowserDialog())
                {
                    DialogResult result = fbd.ShowDialog();
                    if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                    {
                        using (FileStream fs = File.Create(fbd.SelectedPath + "OrderReceipt22.pdf"))
                        {
                            fs.Write(mybytes, 0, mybytes.Length);
                        }
                        MetroFramework.MetroMessageBox.Show(this, "Order Receipt Saved", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            catch (Exception ex)
            {
                MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

我使用断点,它在

上显示错误
orderReceipt.SetParameters(parameters);

1 个答案:

答案 0 :(得分:0)

这是一个令人讨厌的错误消息,但是有一个鲜为人知的改进,只要您的rdlc运行时版本为> 2015,就应该可以利用该改进:查看InnerException,并附加一个处理程序到ReportError event

orderReceipt.ReportError += new EventHandler(orderReceipt_ReportError);

private void orderReceipt_ReportError(object sender, ReportErrorEventArgs e)
{
    MetroFramework.MetroMessageBox.Show(this, e.Exception.ToString(), "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    e.Handled = true;
}

将详细说明哪个参数是罪魁祸首以及它出了什么问题。常见原因:拼写错误,类型不匹配,可空性不匹配。