I want Crystal Report connected to my App but the problem i face arises when i use EF Code First Approach with Repository Pattern. I dont understand how can I connect my Crystal Report Connection with My DBContext, in Crystal Report Connection Viewer i am unable to see my DbContext. Most of the help that i saw points me to create dataSets i.e .XSD file but all of DataSets are already available with DbContext. Is this possible or not , if it is then can any 1 guide me over this following is my Error
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
string reportPath = Path.Combine(reportFolder, rpt);
ReportDocument.Load(reportPath);
ReportDocument.Database.Tables[0].SetDataSource(companies); //Line 29 this where i am getting error
Line 30: // ReportDocument.SetDataSource(companies);
await Task.Delay(10);
Now the error is obvious i dont have connection created, as i am using DBcontext and i assume this is where it will get all connection related detail from.As per my understanding My DbContext should be available for whole application. If i dont go for Dbcontext i have to recreate the connection which kinda negates the whole Layering concept.
This is how i am using crystal reports with my repos,
namespace RPS.Reporting
{
public sealed class ReportGenerator
{
private ReportDocument ReportDocument { get; set; }
public ReportGenerator()
{
ReportDocument = new ReportDocument();
}
public async Task<Stream> GetCompaniesReport(IEnumerable<Company> companies)
{
string rpt = "VoucherSummary.rpt";
// string reportFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string reportFolder = AppDomain.CurrentDomain.BaseDirectory.Replace("WebApp", "Reporting")+ "Accounting";
string reportPath = Path.Combine(reportFolder, rpt);
ReportDocument.Load(reportPath);
ReportDocument.Database.Tables[0].SetDataSource(companies);
// ReportDocument.SetDataSource(companies);
await Task.Delay(10);
Stream stream = ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
}
}
And this is my controller.
public class HomeController : Controller
{
private readonly ICompanyService _companyService;
public HomeController(CompanyService companyService)
{
_companyService = companyService;
}
.........
public async Task<ActionResult> ExportCompanies()
{
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
ReportGenerator reportGenerator = new ReportGenerator();
// string rptPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~"));
Stream stream = await reportGenerator.GetCompaniesReport(_companyService.GetCompanies());
return File(stream, "application/pdf", "Companies.pdf");
}
}
User contributions licensed under CC BY-SA 3.0