[英]Export Crystal Reports with multiple tables in ASP.NET MVC
I am trying to print an orders report from my web application. 我正在尝试从Web应用程序打印订单报告。 I have to connect 3 tables to get the data that I want. 我必须连接3个表才能获取所需的数据。
For instance, I have an Orders
, Contract
and Products
table. 例如,我有一个Orders
, Contract
和Products
表。 I also have another table ContractProducts
which is the bridge table for contract and products. 我还有另一个表ContractProducts
,这是合同和产品的过渡表。 The orders table has a customer id as a foreign key and the contract table has a foreign key as well for the customer id. 订单表具有作为外键的客户ID,合同表也具有针对客户ID的外键。
I actually have a Customers
table as well but I decided to just use the customer id from the orders table because I have no use for the Customers table. 我实际上也有一个Customers
表,但是我决定只使用订单表中的客户ID,因为我没有使用客户表。 Long story short, I need to display the Order ID, Contract ID and the products associated with the order. 长话短说,我需要显示订单ID,合同ID和与订单关联的产品。 I have the following code and it is giving me an error saying that there is an error in retrieving the database. 我有以下代码,它给我一个错误,指出检索数据库时出错。 I used the join method however, it is still not working. 我使用了join方法,但是它仍然无法正常工作。 Please help me. 请帮我。 I've spent hours on this :( 我已经花了几个小时了:(
//This function is for Export order report //此功能用于导出订单报告
public ActionResult CreateOrderReport()
{
try
{
var orders = (from o in db.Orders
join con in db.Contracts on o.CustomerID equals con.CustomerID
join prod in db.ContractProducts on con.ContractID equals prod.ContractID
select new
{
OrderID = o.OrderID.ToString(),
CustomerID = o.Customer.CustomerID.ToString(),
CustomerName = o.Customer.CustomerName.ToString(),
Phone = o.Customer.Phone.ToString(),
ProductID = prod.ProductID.ToString(),
CategoryID = prod.Product.CategoryID.ToString(),
Brand = prod.Product.Brand.ToString(),
Volume = prod.Product.Volume.ToString(),
PackSize = prod.Product.PackSize.ToString(),
Quantity = prod.Quantity.ToString()
}).ToList();
ReportDocument Rep = new ReportDocument();
Rep.Load(Path.Combine(Server.MapPath("~/Reports/OrderReport.rpt")));
Rep.SetDataSource(orders);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Stream stream = Rep.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "Orders Report.pdf");
}
catch (DataException ex)
{
throw;
}
}
***Replace this part***
********************************************************************
var orders = (from o in db.Orders
join con in db.Contracts on o.CustomerID equals con.CustomerID
join prod in db.ContractProducts on con.ContractID equals prod.ContractID
select new
{
OrderID = o.OrderID.ToString(),
CustomerID = o.Customer.CustomerID.ToString(),
CustomerName = o.Customer.CustomerName.ToString(),
Phone = o.Customer.Phone.ToString(),
ProductID = prod.ProductID.ToString(),
CategoryID = prod.Product.CategoryID.ToString(),
Brand = prod.Product.Brand.ToString(),
Volume = prod.Product.Volume.ToString(),
PackSize = prod.Product.PackSize.ToString(),
Quantity = prod.Quantity.ToString()
}).ToList();
********************************************************************
***with this***
********************************************************************
db.Orders.Select(p => new
{
OrderID = o.OrderID.ToString(),
CustomerID = o.Customer.CustomerID.ToString(),
CustomerName = o.Customer.CustomerName.ToString(),
Phone = o.Customer.Phone.ToString(),
ProductID = prod.ProductID.ToString(),
CategoryID = prod.Product.CategoryID.ToString(),
Brand = prod.Product.Brand.ToString(),
Volume = prod.Product.Volume.ToString(),
PackSize = prod.Product.PackSize.ToString(),
Quantity = prod.Quantity.ToString()
}).ToList());
********************************************************************
***and create Dataset with DataTable name is **
> Orderexport
** have this column :***
OrderID
CustomerID
CustomerName
Phone
ProductID
CategoryID
Brand
Volume
PackSize
Quantity
***And change the database source of your Crystal Reports "OrderReport.rpt"***
***
> It works well
***
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.