Here is the code Im using in a project to view crystal reports (its a web control)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Objects.Controls;
using System.Data.SqlClient;
using Objects.Database;
using System.IO;
using System.Configuration;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace App.WebControls
{
public partial class CrystalReportViewer : BaseWebControl
{
#region Members
private CrystalDecisions.Shared.ParameterFields m_ParameterFields = new CrystalDecisions.Shared.ParameterFields();
private CrystalDecisions.Web.CrystalReportSource m_CrystalReportSource = new CrystalDecisions.Web.CrystalReportSource();
#endregion Members
#region Properties
public CrystalDecisions.Web.CrystalReportSource ReportSource
{
get { return m_CrystalReportSource; }
set { m_CrystalReportSource = value; }
}
public string WAPDataSourceName
{
get
{
if ( ViewState[ "WAPDataSourceName" ] == null )
{
ViewState[ "WAPDataSourceName" ] = "WAP";
}
return ViewState[ "WAPDataSourceName" ] as string;
}
set
{
ViewState[ "WAPDataSourceName" ] = value;
}
}
public string SageDataSourceName
{
get
{
if ( ViewState[ "SageDataSourceName" ] == null )
{
ViewState[ "SageDataSourceName" ] = "WAP_Sage";
}
return ViewState[ "SageDataSourceName" ] as string;
}
set
{
ViewState[ "SageDataSourceName" ] = value;
}
}
public string ReportFileName
{
get
{
return ReportSource.Report.FileName;
}
set
{
ReportSource.Report.FileName = value;
}
}
public SageDatabase SageDatabase
{
get
{
if ( ViewState[ "SageDatabase" ] == null )
{
ViewState[ "SageDatabase" ] = new SageDatabase();
}
return ViewState[ "SageDatabase" ] as SageDatabase;
}
set
{
ViewState[ "SageDatabase" ] = value;
}
}
public CrystalDecisions.Shared.ParameterFields ParameterFields
{
get
{
return m_ParameterFields;
}
}
#endregion Properties
#region Events
protected void Page_Load( object sender, EventArgs e )
{
try
{
if ( !this.IsPostBack )
{
this.SetReportSource();
this.ConfigureReports();
this.SetParameters();
}
}
catch ( Exception )
{
throw;
}
}
#endregion Events
#region Methods
private ConnectionInfo GetConnectionInfo( SqlConnectionStringBuilder oSqlConnectionStringBuilder, string ServerName )
{
try
{
ConnectionInfo oConnectionInfo = new ConnectionInfo();
oConnectionInfo.ServerName = ServerName;
oConnectionInfo.DatabaseName = oSqlConnectionStringBuilder.InitialCatalog;
oConnectionInfo.UserID = oSqlConnectionStringBuilder.UserID;
oConnectionInfo.Password = oSqlConnectionStringBuilder.Password;
oConnectionInfo.IntegratedSecurity = oSqlConnectionStringBuilder.IntegratedSecurity;
oConnectionInfo.Type = ConnectionInfoType.SQL;
oConnectionInfo.AllowCustomConnection = true;
return oConnectionInfo;
}
catch
{
throw;
}
}
private void SetDBLogonForReport( ConnectionInfo oConnectionInfo )
{
try
{
TableLogOnInfos oTableLogOnInfos = ReportViewer.LogOnInfo;
foreach ( CrystalDecisions.CrystalReports.Engine.Table oTable in ReportSource.ReportDocument.Database.Tables )
{
if ( oTable.LogOnInfo.ConnectionInfo.ServerName == oConnectionInfo.ServerName )
{
TableLogOnInfo oTableLogOnInfo = oTable.LogOnInfo;
oTableLogOnInfo.ConnectionInfo = oConnectionInfo;
oTable.ApplyLogOnInfo( oTableLogOnInfo );
bool b = oTable.TestConnectivity();
if ( !b )
{
}
}
}
}
catch
{
throw;
}
}
private void ConfigureReports()
{
try
{
ConnectionInfo sageConnectionInfo = this.GetConnectionInfo( new SqlConnectionStringBuilder( this.SageDatabase.ConnectString ), this.SageDataSourceName );
ConnectionInfo wapConnectionInfo = this.GetConnectionInfo( new SqlConnectionStringBuilder( ConfigurationManager.ConnectionStrings[ "DatabaseConnectString" ].ConnectionString ), this.WAPDataSourceName );
this.SetDBLogonForReport( sageConnectionInfo );
this.SetDBLogonForReport( wapConnectionInfo );
}
catch
{
throw;
}
}
public void AddDiscreteValue( string ParameterName, object value )
{
try
{
CrystalDecisions.Shared.ParameterField oParameterField = new CrystalDecisions.Shared.ParameterField();
oParameterField.Name = ParameterName;
CrystalDecisions.Shared.ParameterDiscreteValue oParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
oParameterDiscreteValue.Value = value;
oParameterField.CurrentValues.Add( oParameterDiscreteValue );
this.ParameterFields.Add( oParameterField );
}
catch ( Exception )
{
throw;
}
}
private void SetReportSource()
{
try
{
this.ReportSource.ReportDocument.Load( Server.MapPath( this.ReportFileName ) );
}
catch ( Exception )
{
throw;
}
}
public void ExportToDisk( string FileName )
{
try
{
this.ReportSource.ReportDocument.ExportToDisk( CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, FileName );
}
catch ( Exception )
{
throw;
}
}
[Obsolete( "Bug in Crystal Reports objects that causes the attachment to be called untitled.txt", true )]
public void ExportToMAPI()
{
try
{
CrystalDecisions.Shared.ExportOptions oExportOptions = new CrystalDecisions.Shared.ExportOptions();
{
CrystalDecisions.Shared.MicrosoftMailDestinationOptions oMicrosoftMailDestinationOptions = new CrystalDecisions.Shared.MicrosoftMailDestinationOptions();
oMicrosoftMailDestinationOptions.MailToList = "nathanf@nfs.co.uk";
oMicrosoftMailDestinationOptions.MailSubject = "test";
oMicrosoftMailDestinationOptions.MailMessage = "Body text";
CrystalDecisions.Shared.PdfRtfWordFormatOptions oPdfRtfWordFormatOptions = new CrystalDecisions.Shared.PdfRtfWordFormatOptions();
oExportOptions.ExportDestinationOptions = oMicrosoftMailDestinationOptions;
oExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;
oExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
oExportOptions.ExportFormatOptions = oPdfRtfWordFormatOptions;
this.ReportSource.ReportDocument.Export( oExportOptions );
}
}
catch ( Exception )
{
throw;
}
}
public Stream ExportToStream()
{
try
{
return this.ReportSource.ReportDocument.ExportToStream( CrystalDecisions.Shared.ExportFormatType.PortableDocFormat );
}
catch ( Exception )
{
throw;
}
}
private void SetParameters()
{
try
{
if ( this.ParameterFields.Count > 0 )
{
this.ReportViewer.ParameterFieldInfo = this.ParameterFields;
}
this.ReportViewer.ReportSource = this.ReportSource;
}
catch ( Exception )
{
throw;
}
}
#endregion Methods
}
}
To use this in code:
protected void Page_Load( object sender, EventArgs e )
{
try
{
if ( !Page.IsPostBack )
{
this.CrystalReportViewer.ReportFileName = @"~/Reports/WAP Std Sage PurchaseOrder.rpt";
if ( base.CurrentOrder != null )
{
this.CrystalReportViewer.SageDatabase = base.CurrentOrder.SageDatabase;
this.CrystalReportViewer.AddDiscreteValue( "OrderID", base.CurrentOrder.OrderID );
}
}
}
catch ( Exception ex )
{
ErrorLogging.LogError( ex );
}
}
The main methods to look at are AddDiscreteValue and SetParameters in the User Control
EDIT:
Abstracted relevant methods:
private void SetParameters()
{
try
{
if ( this.ParameterFields.Count > 0 )
{
this.ReportViewer.ParameterFieldInfo = this.ParameterFields;
}
this.ReportViewer.ReportSource = this.ReportSource;
}
catch ( Exception )
{
throw;
}
}
public void AddDiscreteValue( string ParameterName, object value )
{
try
{
CrystalDecisions.Shared.ParameterField oParameterField = new CrystalDecisions.Shared.ParameterField();
oParameterField.Name = ParameterName;
CrystalDecisions.Shared.ParameterDiscreteValue oParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
oParameterDiscreteValue.Value = value;
oParameterField.CurrentValues.Add( oParameterDiscreteValue );
this.ParameterFields.Add( oParameterField );
}
catch ( Exception )
{
throw;
}
}