Asp MVC load report failed - c#

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);

Related

Set Parameters RDLC Report C#

In my rdlc report I create parameter test. In form I write below code to set parameter:
//Set Parameters
ReportParameter[] p = new ReportParameter[1];
p[0] = new ReportParameter("Test", "Testing");
currentBilling_rv.LocalReport.SetParameters(p);
// Provide datasource to report Current_Total_Billing.rdlc
ReportDataSource rds = new ReportDataSource("CurrentTotalBilling", dataset.Tables[2]);
currentTotalBilling_rv.LocalReport.DataSources.Clear();
currentTotalBilling_rv.LocalReport.DisplayName = "Current Total Billing";
currentTotalBilling_rv.LocalReport.ReportPath = ".\\Current_Total_Billing.rdlc";
currentTotalBilling_rv.LocalReport.DataSources.Add(rds);
currentTotalBilling_rv.RefreshReport();
When I run the program I have exception
An unhandled exception of type
'Microsoft.Reporting.WinForms.MissingReportSourceException' occurred
in Microsoft.ReportViewer.WinForms.dll
Additional information: The source of the report definition has not
been specified
Please help.
In your code you are setting a parameter for a ReportViewer named currentBilling_rv not for currentTotalBilling_rv which is used in the next lines of code.
If currentBilling_rv name is correct verify that currentBilling_rv.LocalReport.ReportPath is compiled.

add TextObject to ReportDocument in C#

I use Visual Studio 2010 SAP Crystal Reports.
I setup a Form with a Report Viewer and Load a custom designed rpt file to the Viewer's source.
It works fine.
I can even change my custom designed fields of the rpt file from code behind like this:
ReportDocument reportDocument = new ReportDocument();
string filePath = AppDomain.CurrentDomain.BaseDirectory;
filePath = filePath.Replace("bin\\Debug\\", "MyReportClass.rpt");
reportDocument.Load(filePath);
CrystalDecisions.CrystalReports.Engine.TextObject MyText1= ((CrystalDecisions.CrystalReports.Engine.TextObject)reportDocument.ReportDefinition.Sections[3].ReportObjects["MyText1"]);
MyText1.Text = "Test Work";
MyReportViewer.ReportSource = reportDocument;
Question: How do I add a new TextObject to the ReportDocument in code behind?
I have tried:
reportDocument.Sections(2).AddTextObject(..
but that method does not exist
The Crystal Reports TextObject isn't that simple. You have to first create a ParagraphTextElement and place that in a ParagraphElements object, in a Paragraph object in a Paragraphs object and assign that object to the TextObject.Paragraphs property.
Then you need to find the section of the report in the ReportDefinition to add to and then add that object to the section via the ReportDefinition.
Enough explaining though, here's my using statements:
#region Using
using System;
using System.Windows;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.ReportDefModel;
#endregion
And here's the code that I got it working with:
var reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
var filePath = AppDomain.CurrentDomain.BaseDirectory;
filePath = filePath.Replace("bin\\Debug\\", "MyReport.rpt");
reportDocument.Load(filePath);
ReportDefController2 reportDefinitionController = reportDocument.ReportClientDocument.ReportDefController;
ISCRParagraphTextElement paraTextElementClass = new ParagraphTextElement { Text = "Text Work", Kind = CrParagraphElementKindEnum.crParagraphElementKindText };
ParagraphElements paragraphElements = new ParagraphElements();
paragraphElements.Add(paraTextElementClass);
Paragraph paragraph = new Paragraph();
paragraph.ParagraphElements = paragraphElements;
Paragraphs paragraphs = new Paragraphs();
paragraphs.Add(paragraph);
TextObject newTextObject = new TextObject();
newTextObject.Paragraphs = paragraphs;
var detailSection = reportDefinitionController.ReportDefinition.DetailArea.Sections[0];
reportDefinitionController.ReportObjectController.Add(newTextObject, detailSection, 0);
// MyReportViewer.ReportSource = reportDocument;
this.crystalReportsViewer.ViewerCore.ReportSource = reportDocument;
The last line is different because I'm using a WPF app to do my testing in. I hope it helps!

Clear Crystal Report Parameter

Using Visual Studio 2010 and Crystal Reports 13.0.
For the report, there is a prompt for the user to input a value. Then the report is generated with no problems.
If the user leaves the report.aspx page and comes back to run another report, the prompt does not show and the last report run is still there with the original value from the user.
Searching around for a solution, the only two found did not work:
//After the report loads
CrystalReportSource1.ReportDocument.ParameterFields.Clear();
Error:
You cannot add, remove or modify parameter fields using this method. Please modify the report directly.
Modify the report directly:
Right click the body of your Crystal Report
then goto:
Design -> Default Settings.. ->Reporting
Check the checkbox
Discard Saved Data When Loading Reports.
This did not work. The previous report still populates.
So, I now ask for a little insight on how to fix this problem.
As always, any suggestions are welcome.
Thanks
EDIT:
Here is the code behind for the report page. Many reports use this page....
CrystalReportSource1.Report.FileName = "reports\\" + fileName + ".rpt";
//CrystalReportSource1.Report.Parameters.Clear();
//CrystalReportSource1.Report = null;
//CrystalReportSource1.Report.Refresh();
if (!string.IsNullOrEmpty(Request.QueryString["item"]))
{
String Item = Request.QueryString["item"];
CrystalDecisions.Web.Parameter temp = new CrystalDecisions.Web.Parameter();
temp.Name = "Item";
temp.DefaultValue = Item;
CrystalReportSource1.Report.Parameters.Add(temp);
}
SqlConnectionStringBuilder settings = new SqlConnectionStringBuilder(MyConnectionString);
_crReportDocument = CrystalReportSource1.ReportDocument;
_crConnectionInfo.ServerName = settings.DataSource;
_crConnectionInfo.DatabaseName = settings.InitialCatalog;
_crConnectionInfo.UserID = settings.UserID;
_crConnectionInfo.Password = settings.Password;
//Get the table information from the report
_crDatabase = _crReportDocument.Database;
_crTables = _crDatabase.Tables;
//Loop through all tables in the report and apply the
//connection information for each table.
for (int i = 0; i < _crTables.Count; i++)
{
_crTable = _crTables[i];
_crTableLogOnInfo = _crTable.LogOnInfo;
_crTableLogOnInfo.ConnectionInfo = _crConnectionInfo;
_crTable.ApplyLogOnInfo(_crTableLogOnInfo);
}
You can try using this in your Page_Unload event.
Report.Close();
Report.Dispose();
That should get rid of the report when the page is unloaded and you will start fresh when the user comes back to the page.
There is a good example in this post here.
I found the solution!!!! -------- NOT
Add this line before all the code in my question above:
CrystalReportViewer1.ParameterFieldInfo.Clear();
Then load the file name and so forth.......

How to Bind rdlc report using business object?

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

Unable to display report in ASP.Net page

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.

Categories