【问题标题】:Failed to pass data from the controller to the report viewer无法将数据从控制器传递到报表查看器
【发布时间】:2021-04-09 07:35:56
【问题描述】:

我在我的 .net 核心项目中创建了一个报表查看器。在这里,我创建了用于创建报告的 windows 窗体。我想要这样的东西:-首先,我从会话中收集我的数据,然后这些收集的数据通过我的报告查看器并希望在我的桌面上显示此报告。但是当我尝试这个时,我发现了很多不同的错误。

这是我的代码:-

   public async Task<IActionResult> Print()
        {
            string mimetype = "";
            int extension = 1;
            var path = $"{this._webHostEnvironment.WebRootPath}\\Reports\\Report1.rdlc";
            Dictionary<string, string> parameters = new Dictionary<string, string>();
          

            List<Shop> shop = HttpContext.Session.Get<List<Shop>>("shop");

            for(int i=0;i<shop.Count();i++)
            {
                parameters.Add("Name", shop[i].Name);
                parameters.Add("Quantity", shop[i].Quantity);
                parameters.Add("ProductId", shop[i].Id);
                parameters.Add("Price", shop[i].Price);


            }

            
            LocalReport localReport = new LocalReport(path);
          

            var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimetype);

            var r = File(result.MainStream, "application/pdf");
            return View(r);

        }

Report1.rdlc:-

1.Name(Text,允许空值,允许多个值) 2.ProductId(Intiger,允许多个值) 3.Price(Intiger,允许多个值) 4.(Intiger,Quantity (允许多个值)

我发现了一个意外错误:-

但我想在我的桌面上查看我的数据报告视图。有什么解决办法。我还是个初学者。请帮忙。

【问题讨论】:

  • 我想开始从参数参数中删除引号。Add("Name", "shop[i].Name");应该是 parameters.Add("Name", shop[i].Name);
  • 现在的错误是什么?
  • 我相信您应该使用DataSource 属性。您已将参数声明为Dictionary,但随后您尝试使用相同的键添加多个项目。这行不通。研究使用 DataSource 代替。

标签: c# asp.net-mvc asp.net-core asp.net-core-mvc rdlc


【解决方案1】:

编程学习过程的一部分是学习阅读错误。 看看它在哪里声明你不能添加相同的值名称 2x

此外,您正在执行异步任务,但您的代码中没有等待。只需从方法头中删除它

公共 IActionResult Print() { 字符串 mimetype = ""; 整数扩展 = 1; var path = $"{this._webHostEnvironment.WebRootPath}\Reports\Report1.rdlc"; Dictionary 参数 = new Dictionary();

List<Shop> shop = HttpContext.Session.Get<List<Shop>>("shop");

for(int i=0;i<shop.Count();i++)
{
    parameters.AddWithValue("Name", shop[i].Name);
    parameters.AddWithValue("Quantity", shop[i].Quantity);
    parameters.AddWithValue("ProductId", shop[i].Id);
    parameters.AddWithValue("Price", shop[i].Price);
}

        
LocalReport localReport = new LocalReport(path);
      

var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimetype);

var r = File(result.MainStream, "application/pdf");
return View(r);

}

【讨论】:

  • 不工作。我复制并粘贴了您的代码。但在我的“参数”下方发现错误。
  • 我应该如何为“参数”使用数据类型?
  • 我猜你的 SqlParameter 比你需要的 AddWithValue,docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 2019-07-10
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
相关资源
最近更新 更多