Hi Everyone,
I am trying to display a crystal report in my ASP MVC project. here is my method in SalesController class,
I tried using joining four tables together and resulting anonymous type query, which I wanted to set to a Custom Dataset. The same dataset I want to assign as report datasourse. PDF file is downloaded successfully with other field values excepts data from database.I guess there is some mistake with datasourse. So please someone help me in this case.
public ActionResult DetailsReport(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
DataSetSales ds = new DataSetSales();
var SalesDetails =
from s in db.Sales
join sd in db.SaleDescs on s.Id equals sd.SaleId
join p in db.Products on sd.ProductId equals p.Id
join c in db.Categories on p.CategoryId equals c.Id
join x in db.People on s.CustomerId equals x.Id
where s.Id == 41
select new
{
id = s.Id,
AccntInvoiceNo = s.AccntInvoiceNo,
SaleDate = s.SaleDate,
NetTotal = s.SubTotal,
Discount = s.Discount,
NetAmount = s.NetAmount,
CustomerName = x.FName + " " + x.LName,
SaleId = sd.Id,
PName = p.PName,
Quantity = sd.Quantity,
UnitPrice = sd.UnitPrice,
SubTotal = sd.SubTotal,
SaleType = c.CatName
};
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(System.Int32));
dt.Columns.Add("AccntInvoiceNo", typeof(System.String));
dt.Columns.Add("SaleDate", typeof(System.DateTime));
dt.Columns.Add("NetTotal", typeof(System.Decimal));
dt.Columns.Add("Discount", typeof(System.Decimal));
dt.Columns.Add("NetAmount", typeof(System.Decimal));
dt.Columns.Add("CustomerName", typeof(System.String));
dt.Columns.Add("SaleId", typeof(System.Int32));
dt.Columns.Add("PName", typeof(System.String));
dt.Columns.Add("Quantity", typeof(System.Int32));
dt.Columns.Add("UnitPrice", typeof(System.Decimal));
dt.Columns.Add("SubTotal", typeof(System.Decimal));
dt.Columns.Add("SaleType", typeof(System.String));
List<DataRow> list = new List<DataRow>();
foreach (var t in SalesDetails.ToList())
{
var row = dt.NewRow();
row.SetField<int>("Id", t.id);
row.SetField<string>("AccntInvoiceNo", t.AccntInvoiceNo);
row.SetField<DateTime>("SaleDate", t.SaleDate);
row.SetField<decimal?>("NetTotal", t.NetTotal);
row.SetField<decimal?>("Discount", t.Discount);
row.SetField<Decimal>("NetAmount", t.NetAmount);
row.SetField<string>("CustomerName", t.CustomerName);
row.SetField<int>("SaleId", t.SaleId);
row.SetField<string>("PName", t.PName);
row.SetField<string>("Quantity", t.Quantity);
row.SetField<decimal>("UnitPrice", t.UnitPrice);
row.SetField<decimal>("SubTotal", t.SubTotal);
row.SetField<string>("SaleType", t.SaleType);
list.Add(row);
}
dt = list.CopyToDataTable();
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Reports"), "CRSingleSale.rpt"));
rd.SetDataSource(dt);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "CRSingleSale.pdf");
}
catch (Exception ex)
{
throw;
}
}
this is a part of view page code;
@Html.ActionLink("Export Report", "DetailsReport", new { id = Model.Id })
What I have tried:
I tried using joining four tables together and resulting anonymous type query, which I wanted to set to a Custom Dataset. The same dataset I want to assign as report datasourse. PDF file is downloaded successfully with other field values excepts data from database.I guess there is some mistake with datasourse. So please someone help me in this case.