Click here to Skip to main content
13,358,079 members (59,064 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 20 Apr 2005

Loading Crystal Report reports which use Stored Proc in C#

, 20 Apr 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to load Crystal Report reports using C#.


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.


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".
    //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:
    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";
        //Add the paramField to paramFields
        //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";
        //Add the paramField to paramFields
        crystalReportViewer1.ParameterFieldInfo = paramFields;
        //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.


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


About the Author

Pankaj A. Chitriv
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.

You may also be interested in...


Comments and Discussions

QuestionAbout crystal report loading Pin
Member 1074968514-Apr-14 23:29
memberMember 1074968514-Apr-14 23:29 
Questiondata is not getting in report only headings are coming Pin
Member 102176786-Jan-14 20:30
memberMember 102176786-Jan-14 20:30 
QuestionThanks you so much Pin
vpvk9031-Aug-12 19:40
membervpvk9031-Aug-12 19:40 
GeneralMy vote of 5 Pin
Simon Gurner1-Mar-12 21:41
memberSimon Gurner1-Mar-12 21:41 
Questionasking for DB login while generating report Pin
Virani1-Feb-12 23:37
memberVirani1-Feb-12 23:37 
GeneralThe parameter is incorrect Pin
Lyzbeth25-Feb-10 12:44
memberLyzbeth25-Feb-10 12:44 
GeneralRe: The parameter is incorrect Pin
Lyzbeth25-Feb-10 13:05
memberLyzbeth25-Feb-10 13:05 
GeneralRe: The parameter is incorrect Pin
Lyzbeth25-Feb-10 13:05
memberLyzbeth25-Feb-10 13:05 
QuestionCan not display the value of parameter in report.... Pin
The_Collector7-Sep-09 20:40
memberThe_Collector7-Sep-09 20:40 
GeneralCode Works fine. Pin
Manikantan20098-Jun-09 6:22
memberManikantan20098-Jun-09 6:22 
GeneralWant to Accespt Parameters with Comma Seperated values. Pin
DevIndia9-May-09 0:37
memberDevIndia9-May-09 0:37 
GeneralRe: Want to Accespt Parameters with Comma Seperated values. Pin
Rajdeepanmca23-May-12 3:03
memberRajdeepanmca23-May-12 3:03 
GeneralThe parameter is incorrect Pin
NiloofarNoroozi3-Oct-08 2:29
memberNiloofarNoroozi3-Oct-08 2:29 
GeneralRe: The parameter is incorrect Pin
Babu.R.K3-Feb-09 20:32
memberBabu.R.K3-Feb-09 20:32 
Questionthis article whitout DSN Pin
sepideh0612-Sep-08 20:40
membersepideh0612-Sep-08 20:40 
GeneralThanks a lot Pin
i_razi12-Sep-08 11:14
memberi_razi12-Sep-08 11:14 
Thanks a lot for posting this (the crystal parameter one), saves a lot of time on my release date!!


GeneralRe: Thanks a lot Pin
Pankaj A. Chitriv18-Jan-10 1:01
memberPankaj A. Chitriv18-Jan-10 1:01 
GeneralProblem while printing Pin
Member 409156626-Mar-08 22:17
memberMember 409156626-Mar-08 22:17 
QuestionCall Crystal report in button Pin
Member 29695697-Feb-08 7:32
memberMember 29695697-Feb-08 7:32 
QuestionDymanic Parameters Pin
MikeO@Indy11-Sep-07 17:40
memberMikeO@Indy11-Sep-07 17:40 
GeneralCrystal Repor Pin
Pramod ji20-Jun-07 20:34
memberPramod ji20-Jun-07 20:34 
GeneralRe: Crystal Repor Pin
Pradeep K V3-Jul-07 0:08
memberPradeep K V3-Jul-07 0:08 
GeneralPassing sql query parameter to Crystal Report Pin
mehmetned6-Apr-07 5:18
membermehmetned6-Apr-07 5:18 
AnswerRe: Passing sql query parameter to Crystal Report Pin
Shahan Ayyub18-Sep-11 0:43
memberShahan Ayyub18-Sep-11 0:43 
GeneralOracle [modified] Pin
Viswanatha Rao12-Oct-06 12:52
memberViswanatha Rao12-Oct-06 12:52 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Apr 2005
Article Copyright 2005 by Pankaj A. Chitriv
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid