Receiving Parameters in Crystal Report using ASP.Net 2.0 - c#

I am making application in ASP.Net 2.0 in VS 2010, i have created crystal report with drag and drop, and its configured from database expert, now my problem is i have two times, start time and end time, i have to fetch the record in between, in short, i have to pass parameters to report and receive, one thing important, i am not using any data set or datatable, everything configured with drag and drop, but on click of button, i dont know what to write to receive parameters in report. Please help!

See my question and its answers here Easiest way to pass parameters to Crystal Report from C#?.

Here there is a simple and fast How-To ...
Crystal Report Working with Parameter
This is a working example of how to pass parameter at runtime :
1) Create new Crystal Report Solution
2) Set-up Report with Wizard facility
3) in the Field Exploror add a Field Parameter
4) Right click on the report form and select
5) Select first the field on the database, next the comparision under operators and finally the field parameter.
6) Go to the form and place a textbox with a button.
7) on the button_click paste this code :
ReportDocument myRpt = new ReportDocument();
myRpt.Load("**complete path ** \\CrystalReport1.rpt");
ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = textBox1.Text;
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["codicefiscale"];
crParameterValues = crParameterFieldDefinition.CurrentValues;
crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
crystalReportViewer1.ReportSource = myRpt;
crystalReportViewer1.Refresh();
8) Go back to the form and click on the ReportViewer Object, go to the properties and remove the ReportSource Properties value. (this is setting at runtime)
and the work is done...
Sorry for the indent and the code, but i've made it during the coffee break... have no more time.
For completition i've made this by reading this tutorial

Related

Why crystal report required parameter again when print/export

Dear all I am working on an asp .net application (Webform) in which I am using crystal report for showing reports (with store procedure ).
In my application everything works fine including the reports. The real problem comes when I start clicking on the crystal report toolbar. If I click on any buttons on the crystal report tool bar like (Export, next page ...etc.) it is asking parameters again. Is there any way so that that the crystal report viewer does not ask the parameter that I have already given ?
ReportDocument r = new ReportDocument();
r.Load(Server.MapPath("~/Consumers/Reports/JobOrder.rpt"));
SqlParameter[] para= new SqlParameter[1];
para[0]= new SqlParameter("#OrderId",JobOrder);
dt=da.ExecuteQuery("sp_rpt_JobOrder",para);
if(dt.Rows.Count>0)
{
r.SetParameterValue("#OrderId", JobOrder);
r.SetDataSource(dt);
CrystalReportViewer1.ReportSource = r;
}
can some one help me? Highly appreciated.
its causing due to post back. so i removed
if (!Page.IsPostBack)
now its working fine for me.
Thank you.

Printing w/o displaying Print Option Dialog Box Crystal Report Viewer in my C#

I have a C# application that has a Crystal Report Viewer in a form. I call that form and pass to it a value that I use to update a parameter field associated with the Crystal Report so only a specific record gets displayed.
That all works fine, and I can call the Viewers PrintReport method to print the report without operator intervention.
CrystalForm fs = new CrystalForm();
fs.SetCrystalOrderNumParameter(ItemID);
public partial class CrystalForm : Form
{
public CrystalForm()
{
InitializeComponent();
}
public void SetCrystalOrderNumParameter(string ItemID)
{
ParameterFields paramFields = new ParameterFields();
ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "ItemID";
paramDiscreteValue.Value = ItemID;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
crystalReportViewer1.ParameterFieldInfo = paramFields;
crystalReportViewer1.PrintReport();
}
}
The problem I am having is that I want to be able to pass a value to the Crystal Report so that it uses this # to determine how many copies of the report should be printed.
Is there a way to do this using a Crystal Report Viewer ?
Thank you in advance for your assistance.
The crystal report viewer itself does not provide this functionality.
To control the number of pages without a dialog popup, you will have to use the CrystalDecisions.CrystalReports.Engine.ReportDocument class. This class is what the CrystalReports API uses to represent an actual Crystal Report, and it is usually assigned to the ReportSource property of the viewer to tell the viewer what report to display. You may already be using this object, but I cannot see where you are assigning the report source from the code you have shared.
The ReportDocument class has a PrintToPrinter method, and the second overload looks like this: void PrintToPrinter(int nCopies, bool collated, int startPageN, int endPageN)
The nCopies parameter allows you to specify how many copies of the report to print. The print settings for the report will default to whatever the report's printer settings are, though they may be changed via the PrintOptions property of the ReportDocument instance.
Here is a simple code example, where rptPath is the path to your crystal report:
var rpt = new ReportDocument();
rpt.Load(rptPath);
rpt.PrintOptions.PrinterName = "MyPrinterName";
//This will print 2 copies of the crystal report.
//You can use the nCopies (first) parameter to specify whatever #
//of copies you wish.
rpt.PrintToPrinter(2, false, 0, 0);
Additionally, when a ReportDocument is used to load a Crystal Report via the Load() method, it automatically populates its ParameterFields collection with all of the parameters that the report expects. You can then set a parameter's value like reds displayed:
rpt.SetParameterValue("ParameterName", value);
Finally, if you want to show this report with the viewer, all you have to do is the following:
viewer.ReportSource = rpt;
Where rpt is the ReportDocument object representing the report, and viewer is the CrystalDecisions.Windows.Forms.CrystalReportViewer you wish to use to display the report.
By passing a variable from code behind to cr report parameter:
It could be be like this:
CRPT.SetParameterValue("syear", Servercls.year);
CRPT.SetParameterValue("smonth", Servercls.month);
CRPT.SetParameterValue("sday", Servercls.day);
See this link for more info

Crystal Reports keeps prompting for Parameter

I am having a terrible problem with crystal report 2010 for .net 4.0 (I am using the fixed 13.0.1 version but 13.0.4 is released). No matter which way I try, I am always getting a prompting dialogue box to input my one parameter value the first time.
CrystalReportViewer1.ReportSource = CustomerReport1;
CustomerReport1.Database.Tables[0].SetDatasource ( this.dataset);
CustomerReport1.SetParameterValue("PathLocation", Location.Text);
CustomerReport1.Parameter_PathLocation.CurrentValues.Add(Location.Text) // to be safe using CS 2010 for .net 4
CrystalReportViewer1.ReuseReportParametersOnRefresh = true; // to prevent from showing again and again.
I also tried this:
CustomerReport1.Database.Tables[0].SetDatasource ( this.dataset);
CustomerReport1.SetParameterValue("PathLocation", Location.Text);
CrystalReportViewer1.ReportSource = CustomerReport1;
And this:
CustomerReport1.Database.Tables[0].SetDatasource ( this.dataset);
CustomerReport1.Parameter_PathLocation.CurrentValues.Add(Location.Text)
CrystalReportViewer1.ReportSource = CustomerReport1; // the parameter in the report has Optional Parameter = false, Static , Multiple Value = false .
Can anyone please help? I am getting frustrated with this. It worked in previous versions, but now I am getting this prompt box.
Thank you.
Finally found the solution. It doesn't prompt if we set the DataSource after the ParameterValue.
So anyone of those will work if we put them in this order:
// First, call SetParameterValue. Then, call SetDatasource.
CustomerReport1.SetParameterValue("PathLocation", Location.Text);
CustomerReport1.Database.Tables[0].SetDatasource(this.dataset);
CrystalReportViewer1.ReportSource = CustomerReport1;
Thank you all.
create the parameter but do not assign it a formula using selection formula -> record. Apply this parameter from vb or c#.net IDE by creating a text box,a label and a button. Put the selection formula on click button procedure.

Application runs slow when DB logging info is applied to each Crystal Reports Sections

Currently I use the following method to assign connection info to all the report sections. But as I have many sections in the report, the report is displayed after almost 10 seconds. Which looks really slow. Is there some other method by which we can set logon information to each CR once and for all when it is installed at client side.
JFYI: All the CRs connect to same DB, with same login credentials. Thank you in advance.
readDiamondBillReport = new RealDiamondBill();
crConnectionInfo.ServerName = db.Connection.DataSource;
crConnectionInfo.DatabaseName = db.Connection.Database;
crConnectionInfo.UserID = "client";
crConnectionInfo.Password = "client";
crConnectionInfo.IntegratedSecurity = false;
CrTables = readDiamondBillReport.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
Sections crSections2 = readDiamondBillReport.ReportDefinition.Sections;
// loop through all the sections to find all the report objects
foreach (Section crSection in crSections2)
{
ReportObjects crReportObjects = crSection.ReportObjects;
//loop through all the report objects in there to find all subreports
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject crSubreportObject = (SubreportObject)crReportObject;
//open the subreport object and logon as for the general report
ReportDocument crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
Tables SubCrTables = crSubreportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table SubCrTable in SubCrTables)
{
crtableLogoninfo = SubCrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
SubCrTable.ApplyLogOnInfo(crtableLogoninfo);
}
}
}
}
readDiamondBillReport.Refresh();
I finally found that, neither applying logon info was the issue nor refreshing the report was. But it was my large picture object which I used for setting a watermark in crystal reports.
I had 10 reports which used this Image as watermark. I removed the watermarked image and now following problems are solved:
Project builds very very fast. Previously it took around 1 min to build, which has now reduced drastically to 8-10 secs.
Any changes to the project, especially to reports gets saved much faster.
I used to get "Not enough storage is available to complete this operation" after one or two builds. I had to restart VS and cross my fingers for each of the build.
Crystal Reports are displayed faster on CrystalReportViewer and also objrpt.PrintToPrinter works 500 times faster.
I hope these points will help fellow programmers.
Although you have answered your own question I will point out an alternative approach that can be used with Crystal Reports. Typically people use the "pull" approach in which the connection is set on the Crystal Report and the data set is "pulled" based upon queries embedded within the report.
However, it is possible to use a "push" approach as well. In this scenario you simply bind your Crystal Report data source to an XSD schema and set the data set on the Crystal Report. In .NET you can easily generate the XSD from your data set so this approach is straight forward. You can therefore bind any sub report to the specific table you desire from the passed data set.
The advantage here is that the data can come from any DBMS (database agnostic) and can be manipulated as necessary before being passed to the report (implement custom security, joins, etc).
The caveat being that you would not implement this approach for reports that have larges amounts of data as .NET data sets can be memory intensive.
However, since your performance issue was related to an image this approach would not have helped.
Every report has a subreports collection.
You can apply login info to the tables of each subreport instead of searching for the subreports in each section.
Here is some code
private void showrep(string repName)
{
rd = new ReportDocument();
rd.Load(pth+"\\"+repName);
LogInInfo();
crv.ReportSource = rd; // crv is the reportviewer
crv.Show();
}
private void LogInInfo()
{
MyApp.Properties.Settings s = new MyApp.Properties.Settings();
TableLogOnInfo linfo = new TableLogOnInfo();
linfo.ConnectionInfo.DatabaseName = s.dbname;
linfo.ConnectionInfo.UserID = s.usr;
linfo.ConnectionInfo.Password = s.pw;
linfo.ConnectionInfo.ServerName = s.svr;
foreach (Table t in rd.Database.Tables)
{
t.ApplyLogOnInfo(linfo);
}
foreach (ReportDocument sr in rd.Subreports)
{
foreach (Table t in sr.Database.Tables )
{
t.ApplyLogOnInfo(linfo);
}
}
}
Hope it helps.

Database Login required when generating Crytal Report. SQL Server 08, VS 08

When o load my Crystal Report onto a CrystalReportViewer i am prompted with Database Login with disabled (fixed) server name.
Now when i load the same report on my development machine, it works fine. but when i deploy the C# application on a different machine, i am always prompted for a DB Login. (where the server-name is the one used on my development machine and its static (i cant change it)
i have been trying to find a solution for weeks now and with no luck.
i am using a DataTable as a report source :
MyReport.SetDataSource(MyDataTable)
i have tried most of the online solutions that i searched for.
i have tried passing the DB Login information at run-time
i have installed the SQL Native Client
i have tried to pass a DataSet instead of a DataTable
All with no luck
it also came to my understanding that when i load a report with 1 database table inside i am not prompted for database login (i.e. my datable will be filled with one table form the database : "Select * From SomeTable" when i use an inner join query i am prompted for the login
Any thoughts anyone on how to handle this issue.
well i have finally solved it i was using
cr.SetDatabaseLogon(Username, Password, #Server, DBNAME);
i replaced that code and passed the login information for each table used in the report
T
ableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo CInfo = new ConnectionInfo();
//GET THE SERVER INFORMATION
DataTable DT = b.GetServerInfo();
CInfo.ServerName = DT.Rows[0][1].ToString();
CInfo.DatabaseName = DT.Rows[0][2].ToString();
CInfo.UserID = DT.Rows[0][3].ToString();
CInfo.Password = DT.Rows[0][4].ToString();
Tables CrTables = cr.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = CInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
frmReportViewer ff = new frmReportViewer();
ff.crViewer.LogOnInfo = crtableLogoninfos;
ff.crViewer.ReportSource = cr;
First of all, set the property 'ReportSource' of object CrystalReportViewer to Nothing:
CrystalReportViewer1.ReportSource = Nothing
to avoid previewing of any report set by default at design-time.

Categories