Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters to crystal reports with ASP.net

Status
Not open for further replies.

alvintse

Programmer
Aug 1, 2003
66
US
Hi,
I have created a report with 2 parameters. After I place the crystalreportviewer to the web form, it automatically set the databinding=true. Every time I try to view the report, it prompts me for the paramter value.
Can anyone show me the code to pass the parameter values to the report (Using VB)? I have tried days searching from the web looking for sample codes, no luck at all!

Thanks.
 
What paramaters are you passing the report? Dataset name or table name etc.. Or is it custom parameters?
Klaz
 
Custom parameters that use to filter out those data that do not match the conditions (parameter values)

Thanks.
 
Do you mean this?

Dim rptObj As New REPORTNAME
rptObj.SetParameterValue(0, string2)

I use that to send parameter from aspx to crystal report.

Thx
Angela.
 
Hi Angela,
I think that is something that should work. Can you send me the whole soruce code to call the crystal reports? I am new with ASP.net.

Here is what I found the sample source code for win form written in C#. I don't know why to convert them to VB and don't know where to place the soruce code.

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
//CR Variables
CrystalReport1 crReportDocument;
ParameterFieldDefinitions crParameterFieldDefinitions;
ParameterFieldDefinition crParameterFieldDefinition;
ParameterValues crParameterValues;
ParameterDiscreteValue crParameterDiscreteValue;

private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.Button button1;
// private Form1 Frm; // = new Form1();
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form2(Form1 Frm)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
//Create an instance of the strongly-typed report object
crReportDocument = new CrystalReport1();

//Get the collection of parameters from the report
crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields;

//Access the specified parameter from the collection
crParameterFieldDefinition = crParameterFieldDefinitions["Country"];

//Get the current values from the parameter field. At this point
//there are zero values set.
crParameterValues = crParameterFieldDefinition.CurrentValues;

//Set the current values for the parameter field
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = Frm.OffID.Text; //"Canada"; //1st current value

//Add the first current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue);

//Since this parameter allows multiple values, the discrete value
//object needs to be reset. Destroy the previous instance and create
//a new instance.
//crParameterDiscreteValue = null;

//crParameterDiscreteValue = new ParameterDiscreteValue();
//crParameterDiscreteValue.Value = "USA"; //2nd current value

//Add the second current value for the parameter field
//crParameterValues.Add(crParameterDiscreteValue);

//All current parameter values must be applied for the parameter field.
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
///////////////////////////////////////////////////////////////////
//2nd Parameter
//Get the collection of parameters from the report
//crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields;

//Access the specified parameter from the collection
crParameterFieldDefinition = crParameterFieldDefinitions["Customer"];

//Get the current values from the parameter field. At this point
//there are zero values set.
crParameterValues = crParameterFieldDefinition.CurrentValues;

//Set the current values for the parameter field
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = Frm.startDate.Text; //"Canada"; //1st current value

crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
//crParameterFieldDefinition.ApplyCurrentValues(crParameterValues1);
//Set the viewer to the report object to be previewed.
crystalReportViewer1.ReportSource = crReportDocument;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.AutoScroll = ((bool)(configurationAppSettings.GetValue("crystalReportViewer1.AutoScroll", typeof(bool))));
this.crystalReportViewer1.Location = new System.Drawing.Point(8, 8);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = "D:\\Crystal\\crnet\\csharp_win_paramengine\\CRParams.rpt";
this.crystalReportViewer1.ShowCloseButton = ((bool)(configurationAppSettings.GetValue("crystalReportViewer1.ShowCloseButton", typeof(bool))));
this.crystalReportViewer1.Size = new System.Drawing.Size(704, 392);
this.crystalReportViewer1.TabIndex = 0;
this.crystalReportViewer1.Load += new System.EventHandler(this.crystalReportViewer1_Load);
//
// button1
//
this.button1.Location = new System.Drawing.Point(440, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Exit";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(712, 397);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.crystalReportViewer1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}

private void Form2_Load(object sender, System.EventArgs e)
{
//Frm = new Form1();

}

private void crystalReportViewer1_Load(object sender, System.EventArgs e)
{

}
}
}

Thanks.
 
Hi Alvintse..

I'm not really good at C#.
Here's the code I use from crystal report and convert it to pdf.

Dim rptObj As New RoutineClaim --> crystal report's name
Dim rpttxtObj As CrystalDecisions.CrystalReports.Engine.TextObject
Dim strFilename As String = "c:\exports\" & Session.SessionID.ToString & ".pdf"
Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions

rptObj.Database.Tables.Item("Table's name").SetDataSource(dataset)
rpttxtObj = CType(rptObj.Section2.ReportObjects.Item("txtTanggal"), CrystalDecisions.CrystalReports.Engine.TextObject)
strdate1 = Right(strdate1, 2) & "/" & Mid(strdate1, 6, 2) & "/" & Left(strdate1, 4)
strdate2 = Right(strdate2, 2) & "/" & Mid(strdate2, 6, 2) & "/" & Left(strdate2, 4)
rpttxtObj.Text = "Periode Approval : " & strdate1 & " s/d " & strdate2

CRVRoutineClaim.ReportSource = rptObj
rptObj.SetParameterValue(0, string2)
rptObj.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
rptObj.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
DiskOpts.DiskFileName = strFilename
rptObj.ExportOptions.DestinationOptions = DiskOpts
rptObj.Export()

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(strFilename)
Response.Flush()
Response.Close()

System.IO.File.Delete(strFilename)
ds.Dispose()

Thx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top