I have created a dataset and using it in my crystal report. Issue is that at design time i am able to display the data but when I execute it using ASP.Net page its showing a blank page. Below is my code that i am using to display the report programatically:
/// <summary>
/// Displays batch report
/// </summary>
/// <param name="agreementId"></param>
private void ShowStatements(string agreementId)
{
var crvStatements = new CrystalReportViewer();
var reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("Statements.rpt"));
crvStatements.ReportSource = reportDocument;
var dt1 = new dsStatements.usp_GetBillDetailsByAgreementIdDataTable();
var dt2 = new dsStatements.usp_GetTransactionTypesByAgreementIdForBillDataTable();
var adapter1 = new usp_GetBillDetailsByAgreementIdTableAdapter();
var adapter2 = new usp_GetTransactionTypesByAgreementIdForBillTableAdapter();
adapter1.Fill(dt1, agreementId);
adapter2.Fill(dt2, agreementId);
//var statements = new dsStatements();
//statements.Tables.Add(dt1);
//statements.Tables.Add(dt2);
// reportDocument.SetDataSource(dsStatements);
crvStatements.RefreshReport();
}
Please note that i haven't used any control on the page. I am adding report viewer , report source and dataset programatically. Please help me out. I need it ASAP. I am using VS2010 with CR 2010
Can you provide the reason why you are creating the Crystal Report Viewer at runtime and not simply creating the control on the page? Some people do this so that they can export without displaying, but I don't see any code for that.
I haven't tested this, but I believe the below would work if you had a viewer control on your page.
private void ShowStatements(string agreementId)
{
//var crvStatements = new CrystalReportViewer(); //created on the page
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("Statements.rpt"));
crvStatements.ReportSource = reportDocument;
System.Data.DataTable dt1 = new dsStatements.usp_GetBillDetailsByAgreementIdDataTable();
System.Data.DataTable dt2 = new dsStatements.usp_GetTransactionTypesByAgreementIdForBillDataTable();
System.Data.DataSet statements = new System.Data.DataSet();
statements.Tables.Add(dt1);
statements.Tables.Add(dt2);
reportDocument.SetDataSource(dsStatements);
crvStatements.DataBind();
}
I'm assuming that the report is set up with these tables linked in the report, and you may have to give the datatables a name so that Crystal knows how to map each datatable to the corresponding table within the report. If possible you may try to limit the report to accepting a single table until you have the loading logic worked out.
If each of these datatables represent the data for a subreport then you'll have to iterate through those reports and set the datasources manually.
I hope this helps, but I answered this off the top of my head so there are probably things I've missed. If it doesn't work you just provide a comment and we can probably work through any issues.
The RefreshReport(); makes the Report appearing Empty. try it before loading the Datasets.
Related
I have a C# windows application in which there are many crystal reports.I call and show them using the following piece of code :
rptDDCollection rpt = new rptDDCollection();
rpt.SetDatabaseLogon(Connect.sDatabaseUserName, DLL.Connect.sDatabasePassword, "", Connect.sCurrentDatabase);
rpt.RecordSelectionFormula = "";
frmReports frm = new frmReports();
frm.crViewer1.DisplayGroupTree = false;
frm.crViewer1.ReportSource = rpt;
frm.crViewer1.SelectionFormula = rpt.RecordSelectionFormula;
frm.crViewer1.Show();
frm.Show();
Now, the issue is that I have 2 databases to work on.Things work fine with one database.I have to use the same report but very often I need to view data from the other database.The database engine is SQL server.While searching on the net and on stack overflow, I found suggestions to set the database name dynamically through code.What should be done to achieve what I want in this case?
I worked this around by setting database name for the tables in the report.I made one change in the actual code above.I created a method called ApplyLogonInfo and passed the report object to it.Inside the method I wrote the code for setting database name for the report tables dynamically.This is the modified code:
rptDDCollection rpt = new rptDDCollection();
rpt.SetDatabaseLogon(Connect.sDatabaseUserName, DLL.Connect.sDatabasePassword, "", Connect.sCurrentDatabase);
ApplyLogOnInfo(rpt);
rpt.RecordSelectionFormula = "";
frmReports frm = new frmReports();
frm.crViewer1.DisplayGroupTree = false;
frm.crViewer1.ReportSource = rpt;
frm.crViewer1.SelectionFormula = rpt.RecordSelectionFormula;
frm.crViewer1.Show();
frm.Show();
Below is the newly created method:
public static void ApplyLogOnInfo(ReportClass rpt)
{
TableLogOnInfo info = new TableLogOnInfo();
info.ConnectionInfo.DatabaseName = Connect.sCurrentDatabase;
for (int i = 0; i < rpt.Database.Tables.Count; i++)
{
rpt.Database.Tables[i].ApplyLogOnInfo(info);
}
}
sCurrentDatabase is the name of the database that has been currently selected for viewing.
This enabled me to set the database name dynamically, and now I can work with 2 (or in general multiple) databases and view the data from whichever database I wish to see.
I have created a report in my C# ASP.net web application, to generate a cover page of a proposal. I am attempting to pass the proposal ID to the report. My dataset is set up to accept the proposal ID as a query parameter, and I have the GetByProposalID and FillByProposalID methods defined.
The problem is that when the report runs, the report viewer does not contain data. Either the problem is in my code, or in my report / report viewer configuration.
Here's my method:
/// <summary>
/// Generate the cover page report as a PDF file from a given proposal
/// </summary>
/// <param name="ProposalID">Proposal ID from the database of the proposal</param>
public void GenerateCoverPage( int ProposalID )
{
ReportViewer viewer = new ReportViewer();
viewer.Reset();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "ReportCoverPage.rdlc"; //sets the report from the project RDLC file
//Connect the dataset to the report
ds_ReportCoverPage ds = new ds_ReportCoverPage();
ds_ReportCoverPage.dt_ReportCoverPageDataTable table = new ds_ReportCoverPage.dt_ReportCoverPageDataTable();
ds_ReportCoverPageTableAdapters.dt_ReportCoverPageTableAdapter ta = new ds_ReportCoverPageTableAdapters.dt_ReportCoverPageTableAdapter();
ta.FillByProposalID(table, ProposalID); //This SHOULD fill the adapter with the data from the selected proposal
ReportDataSource source = new ReportDataSource("ds_Report_ReportCoverPage", ds.Tables[0]); //Name must match the data source name within the report
viewer.LocalReport.DataSources.Add(source);
//Run-time exception that there is no report parameter "#Proposal"
//ReportParameter parm = new ReportParameter("#Proposal", ProposalID.ToString()); //Placeholder in report query
//viewer.LocalReport.SetParameters(parm);
viewer.LocalReport.Refresh();
string filepath = "C:\\Temp\\foo.pdf";
SavePDF(viewer, filepath);
}
As you can see, I tried to pass the parameter as a ReportParameter, but the parameter is in the query and not the report so it was rejected.
(I've gotten this far thanks to looking up other questions on SO.)
You will need a parameter in the report and then you can map that parameter to the parameter in the query in the data set.
I am trying to use crystal report with asp mvc first time but I have some problem to load report:
ReportClass rptH = new ReportClass();
rptH.FileName = Server.MapPath("Reports/TestReport.rpt");
rptH.Load();
This is value of rptH.FileName
"rassdk://D:\\ProjectDir\\Pro\\Pro.WebUI\\Reports\\TestReport.rpt"
But on Load() I get exception:
Load report failed.
Report doesn't have any datasource it's just blank report with some text.
Maybe it's important. I set project to run on local IIS (not in VS).
UPDATE
I have changed code a little bit and now I get some other error (I also copied project in inetpub/wwwroot). This is the error:
Value cannot be null. Parameter name: path2
And path now is:
"C:\\inetpub\\wwwroot\\MyProject\\Pro\\Pro.WebUI\\Reports\\TestReport.rpt"
New code is:
ReportClass rptH = new ReportClass();
var path = Server.MapPath("Reports/TestReport.rpt");
rptH.Load(path);
I've done a Crystal Report with ASP.NET before. Not sure if you're able to do this or not, but it worked successfully for me.
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(Server.MapPath("TestReport.rpt");
Response.Buffer = False
Response.ClearContent()
Response.ClearHeaders()
rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, "TestReport.rpt")
rptDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "MyReport")
Check the rpt file if there are parameters defined.
If there are and you do not need them then remove it.
If you need them then supply them values and description in code.
Exp:
rep = new ReportDocument();
string reportPath = Server.MapPath("Reports/TestReport.rpt");
rep.Load(reportPath);
rep.SetParameterValue(0, "SomeValue");
crViewer.ReportSource = rep;
crViewer.ParameterFieldInfo[0].CurrentValues[0].Description = "ParameterName";
crViewer.DataBind();
Try this
ReportDocument rd = new ReportDocument();
string FileName = Server.MapPath("~/Report") + "//YourReportName.rpt";
rd.Load(FileName);
rd.SetDataSource(dt);
i have business object called "TeamMaster",
in which i define three properties,Id,Name & Flg.
in my .rdlc report i apply TeamMaster object as a data source,
now i write the following code in page load event of form in which i add report viewer control and i define my report as a local report.
using (RDLC_DEMO_DBEntities objdatabase = new RDLC_DEMO_DBEntities())
{
lstTeamMstr = objdatabase.TeamMasters.ToList();
}
this.TeamMasterBindingSource.DataSource = lstTeamMstr;
this.reportViewer1.RefreshReport();
when i check this code using debugging i get 6 records in TeamBindingSource,
but in windows report only displays six blank rows,
what is the problem?
Follow this code : >>
string path = HttpContext.Current.Server.MapPath(Your Report path);
ReportViewer1.Reset(); //important
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
// Add sub report even handler if you need
***ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(MySubreportProcessingEventHandler);***
LocalReport objReport = ReportViewer1.LocalReport;
objReport.ReportPath = path;
// Add Parameter If you need
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("Name", Value));
ReportViewer1.LocalReport.SetParameters(parameters);
ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ShowPromptAreaButton = false;
ReportViewer1.LocalReport.Refresh();
//Add Datasourdce
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "Datasource Name Used due to report design";
reportDataSource.Value = DataSourceValue(Your object data-source);
objReport.DataSources.Add(reportDataSource);
objReport.Refresh();
Here Subreport Even handler code
private void MySubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
//You can get parameter from main report
int paramname = int.Parse(e.Parameters[0].Values[0].ToString());
//You can also add parameter in sub report if you need like main report
//Now add sub report data source
e.DataSources.Add(new ReportDataSource("DataSource Name",DataSourceValue)));
}
If you need to create Drillthrough report than follow this link Click here for Drillthrough report
I have created a winform for the users to view view the many reports I am creating for them. I have a drop down list with the report name which triggers the appropriate fields to display the parameters. Once those are filled, they press Submit and the report appears. This works the first time they hit the screen. They can change the parameters and the ReportViewer works fine. Change to a different report, and the I get the following ReportViewer error:
An error occurred during local report processing.
An error has occurred during the report processing.
A data source instance has not been supplied for the data source "CgTempData_BusMaintenance".
As far as the process I use:
I set reportName (string) the physical RDLC name.
I set the dataSource (string) as the DataSource Name
I fill a generic DataTable with the data for the report to run from.
Make the ReportViewer visible
Set the LocalReport.ReportPath = "Reports\\" = reportName;
Clear the LocalReport.DataSources.Clear()
Add the new LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
RefreshReport() on the ReportViewer.
Here is the portion of the code that setups up and displays the ReportViewer:
/// <summary>
/// Builds the report.
/// </summary>
private void BuildReport()
{
DataTable dt = null;
ReportingCG rcg = new ReportingCG();
if (reportName == "GasUsedReport.rdlc")
{
dataSource = "CgTempData_FuelLog";
CgTempData.FuelLogDataTable DtFuelLog = rcg.BuildFuelUsedTable(fromDate, toDate);
dt = DtFuelLog;
}
else if (reportName == "InventoryCost.rdlc")
{
CgTempData.InventoryUsedDataTable DtInventory;
dataSource = "CgTempData_InventoryUsed";
DtInventory = rcg.BuildInventoryUsedTable(fromDate, toDate);
dt = DtInventory;
}
else if (reportName == "VehicleMasterList.rdlc")
{
dataSource = "CgTempData_VehicleMaster";
CgTempData.VehicleMasterDataTable DtVehicleMaster = rcg.BuildVehicleMasterTable();
dt = DtVehicleMaster;
}
else if (reportName == "BusCosts.rdlc")
{
dataSource = "CgTempData_BusMaintenance";
dt = rcg.BuildBusCostsTable(fromDate, toDate);
}
// Setup the DataSource
this.reportViewer1.Visible = true;
this.reportViewer1.LocalReport.ReportPath = "Reports\\" + reportName;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
this.reportViewer1.RefreshReport();
}
Any ideas how to remove all of the old remaining data? Do I dispose the object and recreate it?
I figured it out. I needed to add: reportViewer1.Reset(); to the beginning of the method.