search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Crystal Report And Parameter Passing Using Stored Procedure


By Jakir Hossain
Printer Friendly Version
View My Articles
176 Views
    

Here's sample application for how to create Crystal Report using stored procedure and passing parameter to that report. I have experienced this sample project in Visual Studio2005 and SQL Server.


Here, I will create a simple application for how to create Crystal Report using stored procedure and passing parameter to that report. I have experienced this sample project in Visual Studio2005 and SQL Server.

 

First, let’s assume that we have a database in SQL server –

 

Step -1:

 

There I will create a sample table for this project purpose –

 

drop table [dbo].[USERS]

GO

 

CREATE TABLE [dbo].[USERS] (

            [OID] [nvarchar] (24) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

            [Name] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [Phone] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [Department] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [Designation] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [LoginName] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [PassWord] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [LogQues] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [QuesAns] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [AccessLevel] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [Status] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [Email] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [GroupName] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

            [GroupEmail] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

) ON [PRIMARY]

GO

 

Step -2: Then I will create a simple stored procedure (UserInfo) for using into my report.

 

CREATE PROCEDURE UserInfo  @Dept VARCHAR(20) AS SELECT * FROM USERS WHERE USERS.Department = @Dept

GO

 

 

Step -3:

 

Now I will design my crystal report. For that I will have to add a crystal report in my project.

a.      Double click on the report to go to the design mode.

b.      In field explorer pan of crystal report. Right click ‘Database Fields’. Then choose ‘Database Expert’

c.       Then from left pane – click on ‘Create New Connection’ node

d.      Choose OLEDB (ADO)

e.      Right Click on ‘Make New Connection’  and select  Make New Connection’  

f.        Then from the list of OLEDB providers select ‘Microsoft OLEDB Provider for SQL Server’ then Next

g.      Provide required information for Database server ex. Server Name (IP Address), User ID, password, database name. Then click next and finished.

h.      Then add stored procedure ‘UserInfo’ from left pane to right pane.

 

 

Step -4: Now drag and drop the fields you want to display in report. Just drag the fields and drop into ‘Details section’  of crystal report’s design view.

 

Step -5: Now I will edit my Web.Config file for database information –

 

<configSections>

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

<section name="databaseinformation" type="System.Configuration.SingleTagSectionHandler"/>

</configSections>

<databaseinformation name="data" username="sa" password="pims" servername="192.168.201.69" pvdr="SQLOLEDB.1" databasename="TROUBLETICKET"/>

 

Step -6: Now I will create a page for report loading-

 

ReportLoader.aspx

 

<%@ Page Language="C#" MasterPageFile="~/Pages/MasterPage.master" AutoEventWireup="true" CodeFile="ReportLoader.aspx.cs" Inherits="Pages_ReportLoader" Title="Untitled Page" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"

    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

 

<asp:Label ID="Label1" runat="server" Text="Insert Department:"></asp:Label>

<asp:TextBox ID="txtParamDept" runat="server"></asp:TextBox>

<asp:Button ID="btnShowReport" runat="server" Text="Show Report" OnClick="btnShowReport_Click" />

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

</asp:Content>

 

ReportLoader.aspx.cs

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

 

public partial class Pages_ReportLoader : System.Web.UI.Page

{

    ReportDocument reportDocument = new ReportDocument();

    ParameterField paramField = new ParameterField();

    ParameterFields paramFields = new ParameterFields();

    ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

 

    protected void Page_Load(object sender, EventArgs e)

    {

        //Instantiate variables

 

        if (!IsPostBack)

        {

            //Set instances for input parameter 1 -  @Dept

            paramField.Name = "@Dept";

            //*Remember to reconstruct the paramDiscreteValue and paramField objects

            paramDiscreteValue.Value = "CS";

            paramField.CurrentValues.Add(paramDiscreteValue);

            //Add the paramField to paramFields

            paramFields.Add(paramField);

 

 

            CrystalReportViewer1.ParameterFieldInfo = paramFields;

 

            reportDocument.Load(Server.MapPath("CrystalReportTest.rpt"));

 

 

            //Load the report by setting the report source

            CrystalReportViewer1.ReportSource = reportDocument;

 

            //set the database loggon information.

            reportDocument.SetDatabaseLogon("sa", "pims", "192.168.201.69", "TROUBLETICKET");

        }

    }

    protected void btnShowReport_Click(object sender, EventArgs e)

    {

        paramField.Name = "@Dept";

        paramDiscreteValue.Value = txtParamDept.Text.ToString();

        paramField.CurrentValues.Add(paramDiscreteValue);

        paramFields.Add(paramField);

        CrystalReportViewer1.ParameterFieldInfo = paramFields;

 

        reportDocument.Load(Server.MapPath("CrystalReportTest.rpt"));

        CrystalReportViewer1.ReportSource = reportDocument;

        reportDocument.SetDatabaseLogon("sa", "pims", "192.168.201.69", "TROUBLETICKET");

    }

}

 

**Alternative way of Report Loading:

 

ReportLoader2.aspx

 

<%@ Page Language="C#" MasterPageFile="~/Pages/MasterPage.master" AutoEventWireup="true" CodeFile="ReportLoader2.aspx.cs" Inherits="Pages_ReportLoader" Title="Untitled Page" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"

    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:Label ID="Label1" runat="server" Text="Insert Department:"></asp:Label>

<asp:TextBox ID="txtParamDept" runat="server"></asp:TextBox>

<asp:Button ID="btnShowReport" runat="server" Text="Show Report" OnClick="btnShowReport_Click" />

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">

            <Report FileName="CrystalReportTest.rpt">

                <Parameters>

                    <CR:ControlParameter ControlID="txtParamDept" ConvertEmptyStringToNull="False" DefaultValue="IT"

                        Name="@Dept" PropertyName="Text" ReportName="" />

                </Parameters>           

            </Report>

        </CR:CrystalReportSource>

</asp:Content>

 

ReportLoader2.aspx.cs

 

public partial class Pages_ReportLoader2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        CrystalReportViewer1.Visible = true;

        CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "pims", "192.168.201.69", "TROUBLETICKET");

        CrystalReportViewer1.ReportSourceID = "CrystalReportSource1";

    }

    protected void btnShowReport_Click(object sender, EventArgs e)

    {

        CrystalReportViewer1.Visible = true;

        CrystalReportViewer1.ReportSourceID = "CrystalReportSource1";

 

    }

}

 

 

Sample Project code here


button
Article Discussion: Parameter Passing Crystal Report using Stored Procedure
Jakir Hossain posted at Monday, August 04, 2008 12:55 AM
Original Article
 

crystal report
mayank jain replied to Jakir Hossain at Wednesday, June 17, 2009 8:00 AM

that soulton was great

but i m facing problem if there are more than parameter ,there is error encountered

my code is

ReportDocument reportDocument = new ReportDocument();

ParameterField paramField = new ParameterField();

ParameterFields paramFields = new ParameterFields();

ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

paramField.Name = "@Dept";

paramDiscreteValue.Value = TextBox1.Text.ToString();

paramField.CurrentValues.Add(paramDiscreteValue);

paramFields.Add(paramField);

paramField.Name = "@Name";

paramDiscreteValue.Value = TextBox2.Text.ToString();

paramField.CurrentValues.Add(paramDiscreteValue);

paramFields.Add(paramField);

CrystalReportViewer1.ParameterFieldInfo = paramFields;

reportDocument.Load(Server.MapPath("CrystalReport.rpt"));

CrystalReportViewer1.ReportSource = reportDocument;

reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");

this code is wrkong fr one parameter but not fr two plz tel m the soution