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

Loading Crystal Report reports which use Stored Proc in C#

By , 19 Apr 2005
 

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".
    //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";
        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

About the Author

Pankaj A. Chitriv
Web Developer
India India
Member
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.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanks you so muchmembervpvk9031 Aug '12 - 18:40 
GeneralMy vote of 5memberSimon Gurner1 Mar '12 - 20:41 
Questionasking for DB login while generating reportmemberVirani1 Feb '12 - 22:37 
GeneralThe parameter is incorrectmemberLyzbeth25 Feb '10 - 11:44 
GeneralRe: The parameter is incorrectmemberLyzbeth25 Feb '10 - 12:05 
GeneralRe: The parameter is incorrectmemberLyzbeth25 Feb '10 - 12:05 
QuestionCan not display the value of parameter in report....memberThe_Collector7 Sep '09 - 19:40 
GeneralCode Works fine.memberManikantan20098 Jun '09 - 5:22 
GeneralWant to Accespt Parameters with Comma Seperated values.memberDevIndia8 May '09 - 23:37 
GeneralRe: Want to Accespt Parameters with Comma Seperated values.memberRajdeepanmca23 May '12 - 2:03 
GeneralThe parameter is incorrectmemberNiloofarNoroozi3 Oct '08 - 1:29 
GeneralRe: The parameter is incorrectmemberBabu.R.K3 Feb '09 - 19:32 
Questionthis article whitout DSNmembersepideh0612 Sep '08 - 19:40 
GeneralThanks a lotmemberi_razi12 Sep '08 - 10:14 
GeneralRe: Thanks a lotmemberPankaj A. Chitriv18 Jan '10 - 0:01 
GeneralProblem while printingmemberMember 409156626 Mar '08 - 21:17 
QuestionCall Crystal report in button asp.netmemberMember 29695697 Feb '08 - 6:32 
QuestionDymanic ParametersmemberMikeO@Indy11 Sep '07 - 16:40 
GeneralCrystal RepormemberPramod ji20 Jun '07 - 19:34 
GeneralRe: Crystal RepormemberPradeep K V2 Jul '07 - 23:08 
GeneralPassing sql query parameter to Crystal Reportmembermehmetned6 Apr '07 - 4:18 
AnswerRe: Passing sql query parameter to Crystal ReportmemberShahan Ayyub17 Sep '11 - 23:43 
GeneralOracle [modified]memberViswanatha Rao12 Oct '06 - 11:52 
GeneralWorks like a charmmemberslice2711 Apr '06 - 13:22 
Questionunsuccess,why?membericeboat200516 Dec '05 - 2:39 
Generalpopup windowmemberdinoniko5 Dec '05 - 11:50 
GeneralCrystal Report not working with Stored ProceduresmemberGireesh Viswanathan18 Oct '05 - 2:52 
GeneralRe: Crystal Report not working with Stored Proceduresmemberbarryw425 Aug '06 - 3:54 
GeneralRe: Crystal Report not working with Stored Proceduresmemberic3b3rg37 Sep '07 - 2:21 
QuestionImpossible to connect to my database with CR10 & C#sussAnonymous10 Oct '05 - 6:15 
GeneralRelated... Good ArticlememberJeff Emery26 Sep '05 - 15:20 
GeneralCrystal Report ProblemmemberMuhammad Waqas Butt22 Sep '05 - 23:10 
QuestionMake Crystal Prompt For Parameter ValuessussAnonymous6 Sep '05 - 9:37 
QuestionHow to Successfully Pass Values thru Crystal to Stored Procsmemberwgbarnum5 Sep '05 - 5:14 
AnswerRe: How to Successfully Pass Values thru Crystal to Stored Procsmemberwgbarnum5 Sep '05 - 12:40 
GeneralCrystal Report Driver ErrormemberAnup Singh J31 Aug '05 - 5:10 
GeneralSTORED PROCEDURE IN CRYSTAL REPORTmemberK.selvambikai8 Aug '05 - 19:50 
GeneralReportDocument's load method taking too much time.membererganesh17 Jul '05 - 20:25 
GeneralCrystal Report for Web Formsmembertudela14 Jul '05 - 4:32 
GeneralCrystal that comes with Visual Studiomemberthejonz30 Jun '05 - 4:47 
GeneralRe: Crystal that comes with Visual StudiomemberPankaj A. Chitriv30 Jun '05 - 17:09 
GeneralRe: Crystal that comes with Visual StudiomemberAubyone23 Nov '05 - 18:50 
GeneralDeploy Crystal Reportsmemberunitecsoft10 Jun '05 - 13:02 
GeneralRe: Deploy Crystal ReportsmemberRowland Shaw17 Jan '06 - 5:40 
Generalhorrible examplememberSuicide2013 May '05 - 15:26 
GeneralRe: horrible examplememberPankaj A. Chitriv14 May '05 - 12:22 
GeneralRe: horrible examplememberKris_B22 Aug '05 - 10:20 
Generalsub reportsmemberXodiak13 May '05 - 10:37 
GeneralRe: sub reportsmemberPankaj A. Chitriv14 May '05 - 12:26 
Generalparametermemberboruu10 May '05 - 0:30 

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

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