Click here to Skip to main content
Click here to Skip to main content

How To Prevent "Enter Parameter Value" Dialog Popping Up While Using Crystal Report

, 27 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article contains code to prevent popping up "Enter Parameter Value" dialog for each SQL stored procedure parameter with dynamic database

Introduction

This article may be useful when one is using Crystal report with SQL stored procedure parameters but does not want to pop up "Enter Parameter Value" dialog for each stored procedure parameter for dynamic database and wants to pass them dynamically.

Background

Crystal report with SQL stored procedure always pops up a parameter value dialog on load as well as on refreshing report using the Refresh button click or using RefreshReport() method of the CrystalReportViewer class. However, when one wants to pass a value of parameters dynamically and wants to avoid popup, then there is no way because even parameters are passed on load. On refresh of report, all parameters are unbinded and again, there is a pop up for each parameter. So it seems very ridiculous that on refresh, all parameters are unbinded as well as there is no other way to bind them on refresh.

So, here I've tried my best to solve the problem that I had also faced.

Using the Code

First set the dynamic database connection information:

//Connection info object
private ConnectionInfo objConnectionInfo;
objConnectionInfo = new ConnectionInfo();
            
objConnectionInfo.ServerName    = "ServerName";
objConnectionInfo.DatabaseName  = "DBName";
objConnectionInfo.UserID        = "UserId";
objConnectionInfo.Password      = "Password";

Now, set the database information for each table of the report:

//Set dynamic database connection information
Tables tables = ((ReportClass)crystalReportViewer1.ReportSource).Database.Tables;
foreach (Table tbl in tables)
{
TableLogOnInfo objTableLogOnInfo = new TableLogOnInfo();
objTableLogOnInfo.ConnectionInfo = objConnectionInfo;
tbl.ApplyLogOnInfo(objTableLogOnInfo);
}

Bind the report source to the Crystal report viewer:

//Reset report source
crystalReportViewer1.ReportSource = objMyReport;

Set SQL stored procedure parameters:

ParameterField objParameterField = crystalReportViewer1.ParameterFieldInfo[0];
ParameterDiscreteValue objParameterDiscreteValue;

//Set value for first parameter
objParameterDiscreteValue = new ParameterDiscreteValue();
objParameterDiscreteValue.Description = "First Parameter";
objParameterDiscreteValue.Value = "First Value";

objParameterField.CurrentValues.Add(objParameterDiscreteValue);
//Reset params
crystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

objParameterField = crystalReportViewer1.ParameterFieldInfo[1];

//Set value for second parameter            
objParameterDiscreteValue = new ParameterDiscreteValue();
objParameterDiscreteValue.Description = "Second Parameter";
objParameterDiscreteValue.Value = "Second Value";

objParameterField.CurrentValues.Add(objParameterDiscreteValue);

//Reset params
crystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

//So on.....

Now on refresh report button click event, don't refresh report and refresh report manually. Using the following code, one can prevent popping up parameter value dialog on load as well as on refreshing report:

private void crystalReportViewer1_ReportRefresh
	(object source, CrystalDecisions.Windows.Forms.ViewerEventArgs e)
{
//Don't  handle refresh event on refresh button click
e.Handled = true;

//Manually refresh 
((ReportClass)crystalReportViewer1.ReportSource).Refresh();   
          
//AReset dynamic database connection information
//...
           
//Reset report source
//...
           
//Reset SQL parameters
//...
}

Find the given sample C# code as:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.Shared;

namespace MyCrystalReport
{
    public partial class MyCrystalReport : Form
    {
        public MyCrystalReport()
        {
            InitializeComponent();
        }

        //Crystal report object
        private MyReport objMyReport;
        //Connection info object
        private ConnectionInfo objConnectionInfo;

        private void Form1_Load(object sender, EventArgs e)
        {
            //Initialize report 
            objMyReport = new MyReport();
           
            //Set database settings for connection
            setDatabaseSettings();

            //Loads report first time
            refreshReport();
        }       

        /// <summary>
        /// Sets Database Settings
        /// </summary>
        private void setDatabaseSettings()
        {
            objConnectionInfo = new ConnectionInfo();
            
            objConnectionInfo.ServerName    = "ServerName";
            objConnectionInfo.DatabaseName  = "DBName";
            objConnectionInfo.UserID        = "UserId";
            objConnectionInfo.Password      = "Password";
        }

        private void crystalReportViewer1_ReportRefresh
	(object source, CrystalDecisions.Windows.Forms.ViewerEventArgs e)
        {
            //Don't  handle refresh event on refresh button click
            e.Handled = true;

            //Manually refresh 
            ((ReportClass)crystalReportViewer1.ReportSource).Refresh();   
        }

        /// <summary>
        /// Refreshes report
        /// </summary>
        private void refreshReport()        
        {        
            //Set dynamic database connection information
            Tables tables = 
		((ReportClass)crystalReportViewer1.ReportSource).Database.Tables;
            foreach (Table tbl in tables)
            {
                TableLogOnInfo objTableLogOnInfo = new TableLogOnInfo();
                objTableLogOnInfo.ConnectionInfo = objConnectionInfo;
                tbl.ApplyLogOnInfo(objTableLogOnInfo);
            }

            //Reset report source
            crystalReportViewer1.ReportSource = objMyReport;

            //Reset SQL parameters
            setSQLParams();
        }

        /// <summary>
        /// Sets SQL stored procedure parameters
        /// </summary>
        private void setSQLParams()
        {
            ParameterField objParameterField = 
		crystalReportViewer1.ParameterFieldInfo[0];            
            ParameterDiscreteValue objParameterDiscreteValue;

            //Set value for first parameter
            objParameterDiscreteValue = new ParameterDiscreteValue();
            objParameterDiscreteValue.Description = "First Parameter";
            objParameterDiscreteValue.Value = "First Value";

            objParameterField.CurrentValues.Add(objParameterDiscreteValue);
            //Reset params
            crystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

            objParameterField = crystalReportViewer1.ParameterFieldInfo[1];

            //Set value for second parameter            
            objParameterDiscreteValue = new ParameterDiscreteValue();
            objParameterDiscreteValue.Description = "Second Parameter";
            objParameterDiscreteValue.Value = "Second Value";

            objParameterField.CurrentValues.Add(objParameterDiscreteValue);

            //Reset params
            crystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

            //So on.....            
        }
    }
}

Wish

Hope this will be helpful for everyone who required the above. Have a nice time. Cheers!

History

  • 27th September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sneha B Patel
Team Leader Financial Technologies India Ltd.
India India
No Biography provided

Comments and Discussions

 
QuestionThanks a million Pinmemberelham593-Dec-14 20:40 
GeneralMy vote of 3 PinmemberPabloMarianoC14-Jun-14 16:49 
Generalone more article Pinmembermrsht11-Aug-11 9:41 
GeneralWish I would have had this a month ago Pinmembersides_dale30-Sep-08 17:40 
GeneralSee another article too PinmemberGigavisto30-Sep-08 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 27 Sep 2008
Article Copyright 2008 by Sneha B Patel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid