I am using Stored Procedures. My parameters are set in the report. I pass the values from my code to the parameters. I have two codes, the first is the primary code that creates the CR object and calls the CR code. Below is a sample of my code DisplayProfile.cs:
private void Display_Click(object sender, System.EventArgs e)
{
try{
display_profile.CrystalReportViewer1 crViewer1 = new CrystalReportViewer1();
crViewer1.Show();
}
catch (Exception ex) {
statusBar1.Text = "Error: " + ex.Message.ToString();
}
}
The second is what I called CrystalReportViewer1.cs. This code call set my parameter values in the report to be displayed.
using System;
using System.Drawing;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.ComponentModel;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Windows.Forms;
using ConfigurationXml;
using common;
namespace display_profile
{
public class CrystalReportViewer1 : System.Windows.Forms.Form {
private SqlTools sqlTools;
private bool isLoggedOn = false;
private ReportDocument crDocument = new ReportDocument();
private Database crDatabase;
private Tables crTables;
private Table crTable;
private TableLogOnInfo crTableLogOnInfo;
private ConnectionInfo crConnectionInfo;
private ParameterFieldDefinitions crPDefinitions;
private ParameterFieldDefinition crPDefinition;
private ParameterValues crPValues;
protected SqlCommand cmdGateProfile;
protected ParameterField crPField = new ParameterField();
protected ParameterFields crPFields = new ParameterFields();
protected ParameterDiscreteValue crPDValue = new ParameterDiscreteValue();
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer2;
private display_profile.OA_Profiles oA_Profiles1;
private System.ComponentModel.Container components = null;
public CrystalReportViewer1(SqlTools sqlTools_in) {
InitializeComponent();
sqlTools = sqlTools_in;
}
protected override void Dispose( bool disposing ) {
if( disposing ){
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}
private void crystalReportViewer2_Load(object sender, System.EventArgs e)
{
if (!isLoggedOn)
SetReportParameters();
}
protected void SetReportParameters()
{
int idx;
string str = null;
DateTime dt = new DateTime();
try {
crDocument.Load(crystalReportViewer2.ReportSource.ToString()); // load report...
crConnectionInfo = sqlTools.GetCrConnectionInfo();
crConnectionInfo.ServerName = "SATLRCCDLDB15";
crConnectionInfo.DatabaseName = "oadb";
crConnectionInfo.UserID = "oauser";
crConnectionInfo.Password = "elmwood";
crDatabase = crDocument.Database;
crTables = crDatabase.Tables;
// loop through all the tables and apply the connection into to each table...
for (int i = 0; i < crTables.Count; i++)
{
crTable = crTables;
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
}
isLoggedOn = true;
str = DisplayProfile.myDoW.Remove(0, 2);
idx = Convert.ToInt32(DisplayProfile.myDoW.Remove(1, DisplayProfile.myDoW.Length - 1));
crPDefinitions = crDocument.DataDefinition.ParameterFields;
crPDefinition = crPDefinitions["@start_date"];
crPValues = crPDefinition.CurrentValues;
crPDValue.Value = DisplayProfile.startDate.ToString("MM-dd-yyyy");
crPValues.Add(crPDValue);
crPDefinition.ApplyCurrentValues(crPValues);
crDocument.DataDefinition.ParameterFields.MoveNext();
crPDefinition = crPDefinitions["@end_date"];
crPValues = crPDefinition.CurrentValues;
crPValues.Clear();
crPDValue.Value = DisplayProfile.endDate.ToString("MM-dd-yyyy");
crPValues.Add(crPDValue);
crPDefinition.ApplyCurrentValues(crPValues);
crDocument.DataDefinition.ParameterFields.MoveNext();
crDocument.SetDatabaseLogon(crConnectionInfo.UserID, crConnectionInfo.Password, crConnectionInfo.ServerName, crConnectionInfo.DatabaseName);
crystalReportViewer2.ReportSource = crDocument;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sqlTools.DisposeSqlConnection();
}
}
Whenever my code gets to the 'crViewer1.Show()' method (in my DiaplayProfile code) and call my CrystalReportViewr1 code, I get the error mentioned above. Thank you. Your help is very appreciated.