Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / Windows Forms
Article

Loading Crystal Report reports which use Stored Proc in C#

Rate me:
Please Sign up or sign in to vote.
3.89/5 (26 votes)
19 Apr 20052 min read 286.2K   4.9K   54   56
An article on how to load Crystal Report reports using C#.

Introduction

This article focuses on loading Crystal Report reports in C#. The Crystal Report report created here uses a SQL Server stored procedure which takes two parameters and it uses DSN with SQL Server authentication.

Background

When I started working on Crystal Reports, mainly I was wondering about these problems:

  1. How to set parameters of stored procedure from C# code using Crystal's APIs.
  2. How to avoid popup window which comes when we use DSN with SQL Server authentication.
  3. How to avoid these errors:
    • Missing prompting unit
    • The parameter is incorrect

This article gives a solution to all of the above issues and also gives a few notes to avoid unpredictable results.

Using the code

The attached code here loads the "SalseReport.rpt" file. The steps to run the attached application are:

  1. Create database say "testDB" in SQL Server and execute the script "SalseData_Table_SP.sql" in the SQL Server Query Analyser which will create a stored procedure "Sel_SalesData" and a table "SalesData" for you.
  2. Import the sample data to the table "salseData" from file "SalesData_Data.txt" (data is comma separated).
  3. Create a DSN named "TestDB_DSN" with SQL Server authentication. Give valid user name and password.
  4. Open "frmSalseData.cs" file and update the below line with your logon information, in the function "btnPreview_Click".
    C#
    //The parameters are in the order 
    //- UserName, Password, DSN Name, DatabaseName, Case Sensitive
    reportDocument.SetDatabaseLogon("pchitriv", "Windows2000", 
                               "TestDB_DSN", "testDB", false);
  5. In case you have created a DSN with some other name than "TestDB_DSN", then open the "SalseReport.rpt" file from the Reports directory and set the DataSource location to point to the correct DSN and "Sel_SalseData" stored procedure again.
  6. The code to load the report looks like this:
    C#
    private void btnPreview_Click(object sender, System.EventArgs e) 
    {
        //Instantiate variables
        ReportDocument reportDocument = new ReportDocument();
        ParameterField paramField = new ParameterField();
        ParameterFields paramFields = new ParameterFields();
        ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
    
        //Set instances for input parameter 1 -  @vDepartment
        paramField.Name = "@vDepartment";
        //Below variable can be set to any data 
        //present in SalseData table, Department column
        paramDiscreteValue.Value = "South";
        paramField.CurrentValues.Add(paramDiscreteValue);
        //Add the paramField to paramFields
        paramFields.Add(paramField); 
    
        //Set instances for input parameter 2 -  @iSalseYear
        //*Remember to reconstruct the paramDiscreteValue and paramField objects
        paramField = new ParameterField();
        paramField.Name = "@iSalesYear";
        paramDiscreteValue = new ParameterDiscreteValue();
        paramDiscreteValue.Value = "2004";
        paramField.CurrentValues.Add(paramDiscreteValue);
    
        //Add the paramField to paramFields
        paramFields.Add(paramField); 
    
        crystalReportViewer1.ParameterFieldInfo = paramFields;
    
        reportDocument.Load(@"..\..\..\Reports\SalseReport.rpt");
    
        //set the database loggon information. 
        //**Note that the third parameter is the DSN name 
        //  and not the Database or System name
        reportDocument.SetDatabaseLogon("pchitriv", "Windows2000", 
                                   "TestDB_DSN", "testDB", false);
      
        //Load the report by setting the report source
        crystalReportViewer1.ReportSource = reportDocument;
    }

Points of Interest

  1. Error - Missing prompting unit:

    I got this error when I was not creating a new “ParameterField" object for each of the input parameters.

  2. Error - The parameter is incorrect:

    I got this error when I used paramField.Name = "vDepartment"; instead of paramField.Name = "@vDepartment";. This is because in the report, the parameter field name in the Field Explorer is @vDepartment. So remember to use the exact name in the C# code as that is used in the report (for parameter/input fields).

  3. Note:

    Remember to uncheck the option “Save data with Report” under Crystal Reports File menu. Checking this option gives unpredictable results when you call a report in a C# form.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
He likes traveling a lot, especially long drives. He occasionally plays guitar, fascinated to watch WWE and likes spending time with his family and friends.

About the work, Pankaj is playing with the MS technologies since last couple of years. He designed and architected quiet a few applications and products which specially includes load balancing, DB clustering and also known to various architectural patterns and application blocks.



Comments and Discussions

 
GeneralCrystal Report not working with Stored Procedures Pin
Gireesh Viswanathan18-Oct-05 2:52
Gireesh Viswanathan18-Oct-05 2:52 
GeneralRe: Crystal Report not working with Stored Procedures Pin
barryw425-Aug-06 3:54
barryw425-Aug-06 3:54 
GeneralRe: Crystal Report not working with Stored Procedures Pin
ic3b3rg37-Sep-07 2:21
ic3b3rg37-Sep-07 2:21 
QuestionImpossible to connect to my database with CR10 & C# Pin
Anonymous10-Oct-05 6:15
Anonymous10-Oct-05 6:15 
GeneralRelated... Good Article Pin
Wai Friend26-Sep-05 15:20
Wai Friend26-Sep-05 15:20 
GeneralCrystal Report Problem Pin
| Muhammad Waqas Butt |22-Sep-05 23:10
professional| Muhammad Waqas Butt |22-Sep-05 23:10 
QuestionMake Crystal Prompt For Parameter Values Pin
Donh76446-Sep-05 9:37
Donh76446-Sep-05 9:37 
QuestionHow to Successfully Pass Values thru Crystal to Stored Procs Pin
wgbarnum5-Sep-05 5:14
wgbarnum5-Sep-05 5:14 
Hi everyone. I had the same problem with this article as you all seem to have: the Crystal Report in question was not created with the special OEM version of Crystal that is bundled with Visual Studio 2003, and it won't work.

However, all by my little self I managed to figger out how to use stored procs that have parameters with the Crystal Reports version that iSmile | :) s included with VS Studio 2003. It was agony, so I thought I'd share a bit so others don't have to go through the 2 days I did. I'm not going to include much code, but I don't need to.

Like most of you, probably, I asked myself, "OK, I see there are CR parameters, but where the f___ are the parms in the CR Report Expert or the Data Expert? How do I set the stored proc parm values like I do for an ADO.NET cmd in the CR object model? How do I pass through the values? Arrrghhhh!" Well, here's the big, dark secret: the CR parameters ARE the stored proc parameters; they're effectively the same thing!

Here's what happens: when you add a "table" to your CR report using the Report Export or whatever, and you choose a stored proc as the data source for that 'table', the CR designer *automatically* creates CR parameter fields that match any stored proc parameters. You can see them right after you add the table - in the Field Explorer when your report is an active window inside VS Studio 2003. Not only that, but you don't have to write any code to copy the values from the CR parameters to some stored proc parameter that's a property of the CR table object, or something, because there isn't any. CR just seems to do it for you. Is this explained ANYWHERE IN THE DOCUMENTATION OR ANYWHERE ON THE WEB? I couldn't find it, if there is. Somehow, we're all just supposed to know this? Anyway, I'm still a little bitter as you can see ... but my code now works! Smile | :)

To finish up, I'll explain that I have a C# web page that sets the values of 2 date fields. I simply pass those values to the CR parameter fields that got created in the CR designer in Visual STudio, and poof! it works. In my C# code-behind class, I used the SetParameterValue() method of the ReportDocument object like this:

_Report.SetParameterValue("@prmBeginDate", _ReportBeginDate);
_Report.SetParameterValue("@prmEndDate", _ReportEndDate);

However it happens, these values make it the stored proc in question; it's all very puzzling and not well documented. But don't ask questions, just do it. It works.


BillyB
AnswerRe: How to Successfully Pass Values thru Crystal to Stored Procs Pin
wgbarnum5-Sep-05 12:40
wgbarnum5-Sep-05 12:40 
GeneralCrystal Report Driver Error Pin
Anup Singh J31-Aug-05 5:10
Anup Singh J31-Aug-05 5:10 
GeneralSTORED PROCEDURE IN CRYSTAL REPORT Pin
Member 21779488-Aug-05 19:50
Member 21779488-Aug-05 19:50 
GeneralReportDocument's load method taking too much time. Pin
erganesh17-Jul-05 20:25
erganesh17-Jul-05 20:25 
GeneralCrystal Report for Web Forms Pin
tudela14-Jul-05 4:32
tudela14-Jul-05 4:32 
GeneralCrystal that comes with Visual Studio Pin
thejonz30-Jun-05 4:47
thejonz30-Jun-05 4:47 
GeneralRe: Crystal that comes with Visual Studio Pin
Pankaj A. Chitriv30-Jun-05 17:09
Pankaj A. Chitriv30-Jun-05 17:09 
GeneralRe: Crystal that comes with Visual Studio Pin
Aubyone23-Nov-05 18:50
Aubyone23-Nov-05 18:50 
GeneralDeploy Crystal Reports Pin
unitecsoft10-Jun-05 13:02
unitecsoft10-Jun-05 13:02 
GeneralRe: Deploy Crystal Reports Pin
Rowland Shaw17-Jan-06 5:40
Rowland Shaw17-Jan-06 5:40 
Generalhorrible example Pin
SalizarMarxx13-May-05 15:26
SalizarMarxx13-May-05 15:26 
GeneralRe: horrible example Pin
Pankaj A. Chitriv14-May-05 12:22
Pankaj A. Chitriv14-May-05 12:22 
GeneralRe: horrible example Pin
Kris_B22-Aug-05 10:20
Kris_B22-Aug-05 10:20 
Generalsub reports Pin
Xodiak13-May-05 10:37
Xodiak13-May-05 10:37 
GeneralRe: sub reports Pin
Pankaj A. Chitriv14-May-05 12:26
Pankaj A. Chitriv14-May-05 12:26 
Generalparameter Pin
boruu10-May-05 0:30
boruu10-May-05 0:30 
GeneralCan't running your code Pin
ntha822-May-05 6:47
ntha822-May-05 6:47 

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

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