title: Export Crystal Reports with multiple tables in ASP.NET MVC
body: <p>I am trying to print an orders report from my web application. I have to connect  3 tables to get the data that I want. </p>
  
  <p>For instance, I have an <code>Orders</code>, <code>Contract</code> and <code>Products</code> table. I also have another table <code>ContractProducts</code> which is the bridge table for contract and products. 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. </p>
  
  <p>I actually have a <code>Customers</code> 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. Long story short, I need to display the Order ID, Contract ID and the products associated with the order. 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. Please help me. I've spent hours on this :(</p>
  
  <p>//This function is for Export order report</p>
  
  <pre><code>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;
          }
  
      }
  </code></pre>