You are on page 1of 8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject


Email Password Sig i J i Remember me? Lost password?

8,305,908 members and growing! (39,103 online)

Home

Articles
C#

Quick Answers
Reporting

Discussions

Learning Zones

Features

Help!

The Lounge
C POL 28 Aug 2008 81,951 1,917 58 times

Sea ch

Languages

Cr stal Report with DataSet and DataTable using C#


By Rehan Ahmad Abbasi | 8 Sep 2008 Crystal Report with Dataset and DataTable using C#
Article Browse Code Stats Revisions

Licence First Posted Views Downloads Bookmarked

Part of The SQL Zone sponsored by

See Also
More like this More by this author

.NET1.0 .NET1.1 .NET2.0 Win2K WinXP Win2003 Vista .NET3.0 .NET3.5 C # , +

3.78 (30 votes)

12

Sponsored Links
MX-Frame Business Application Framework www.mxframe.net Search for ASP.NET Add search engine functionality to your ASP.NET web site. Features... keyoti.com

Download source - 86.83 KB

Introduction
In my previous article, I described how to create Crystal Report with Oracle Views. In this article, I am going to demonstrate how to create a Crystal Report with ADO.NET D aa T a b l e . You will find this article very interesting, simple and easy to understand. This article needs basic knowledge of .NET. Note: Please vote for this article.

Background
No background knowledge is needed for this article. This article is very simple and just requires very basic knowledge of .NET. But I recommend reading my previous article as well, though it has no direct relation with this article.

Using the Code


Now enough of theory, we will move to our point. So here we go... I have created 2 sample tables for this project. Scripts of tables are as follows, with some sample insert query to have sample data.
C ollapse | C opy C ode

See Also...
C reating animations with Dundas C hart for ASP.NET Smarter Data Labels with Dundas C hart SmartLabels Understanding C hart Areas with Dundas C hart for .NET Making Sense of Geographic Data with Dundas Map and AJAX DestroyWindow in VBScript SmartLink C reate data-driven applications

c r e ae a b l e b l _ p r o j e c ( P R O J E C T _ I DN U M B E R ( 4 ) , P R O J E C T _ N A M E V A R C H A R 2 ( 1 5 0 ) , G R O U P _ C O D E N U M B E R ( 2 ) ) c r e ae a b l e b l _ p r o j e c_ g r op

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

1/8

05/12/11
( G R O U P _ C O D E N U M B E R ( 2 ) , G R O U P _ N A M E V A R C H A R 2 ( 1 0 0 ) ) ; i ne i ne i ne i ne i no b l _ po j e c i no b l _ po j e c

Cr stal Report

ith DataSet and DataTable using C# - CodeProject


C reate data-driven applications with the Hera Application Framework Towards the self-documenting database: extended properties

a le( 1 , ' C

a l R e p o W ih Oa c l e ' , 1 ) ;

Digital Signatures and PDF Documents WMP Power Hour APP Merge Landscape and Portrait PDFs using ASP.NET Using Barcodes in Documents Best Practices

a le( 2 , ' A j a A p p l i c ai o n ' , 2 ) ; a p p l i c ai o n ' ) ;

i no b l _ po j e c_ gop a le( 1 , 'i n d o

i no b l _ po j e c_ gop a le( 2 , ' W e ba p p l i c ai o n ' ) ;

First of all, create a project in Microsoft Visual Studio 2005 and name it C

a l R e p o W ih Oa c l e .

How to Retrieve EMC C entera C luster/Pool C apabilities Using multiple keyboards with different layouts on the same machine "Hey! Is That My C ar? How to Sharpen a QuickBird Satellite Image Using DotImage" Integrate your SharePoint environment into the open standards-based WebSphere Portal platform using the Visual Studio IDE

Then create a form as f m M a i n and add a Crystal report viewer control to this form. Add a Reference to S e m . D aa . Oa c l e C l i e n.

Step 1: Adding A DataSet and DataTable


Add a D aa S e to your Project and name it as m D aa S e , as follows:

The Dail Insider


30 free programming books Daily News: Signup now.

Now add a D aa T a b l e to m D aa S e:

Now add columns to your D aa T a b l e as given below in the image. Your column name and d aa p e should be the same as that in your database.

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

2/8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject

Change he d a t a tp e of he D a t a T a b l e col mn a

ho n belo :

No

a e hi D a t a T a b l ea m_ d t .

No

e ha e c ea ed o

D a t a S e t and D a t a T a b l e . The ne

ep i

o c ea e a C rs t a l R e p o r t .

Step 2: Adding A Cr stal Report


Add a C al epo o he p ojec and name i a m _ r p t .

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

3/8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject

Choose Using the report wi ard.

Choose the data source from project data

hich is m _ d a t a s e t in our case.

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

4/8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject

Choose the columns to be displa ed on the report.

Add a group b column, if

ou want.

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

5/8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject

Now our designing part is complete.

S ep 3: Binding O r Repor

o o r Da aSo rce

Below is the C# code to bind our report to the d a t a s o u r c e .


C ollapse C opy C ode

u s i n gS y s t e m ; u s i n gS y s t e m . C o l l e c t i o n s . G e n e r i c ; u s i n gS y s t e m . C o m p o n e n t M o d e l ; u s i n gS y s t e m . D a t a ; u s i n gS y s t e m . D r a w i n g ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . W i n d o w s . F o r m s ; u s i n gS y s t e m . D a t a . O r a c l e C l i e n t ; u s i n gS y s t e m . I O ; n a m e s p a c eC r y s t a l R e p o r t W i t h O r a c l e { p u b l i cp a r t i a lc l a s sf r m M a i n:F o r m { p u b l i cf r m M a i n ( ) { I n i t i a l i z e C o m p o n e n t ( ) ;

p r i v a t ev o i df r m M a i n _ L o a d ( o b j e c ts e n d e r ,E v e n t A r g se ) { m y _ r p to b j R p t ; / /C r e a t i n go b j e c to fo u rr e p o r t . o b j R p t=n e wm y _ r p t ( ) ; S t r i n gC o n n S t r=" S E R V E R = m y d b ; U S E RI D = u s e r 1 ; P W D = u s e r 1 " ; O r a c l e C o n n e c t i o nm y C o n n e c t i o n=n e wO r a c l e C o n n e c t i o n ( C o n n S t r ) ; S t r i n gQ u e r y 1=" s e l e c ta . P R O J E C T _ I D , a . P R O J E C T _ N A M E , b . G R O U P _ N A M Ef r o m t b l _ p r o j e c ta , t b l _ p r o j e c t _ g r o u pbw h e r ea . g r o u p _ c o d e =b . g r o u p _ c o d e " ; O r a c l e D a t a A d a p t e ra d a p t e r=n e wO r a c l e D a t a A d a p t e r ( Q u e r y 1 ,C o n n S t r ) ; D a t a S e tD s=n e wD a t a S e t ( ) ; / /h e r em_ d ti st h en a m eo ft h eD a t a T a b l ew h i c hw e / /c r e a t e di nt h ed e s i g n e rv i e w . a d a p t e r . F i l l ( D s ," m y _ d t " ) ; i f( D s . T a b l e s [ 0 ] . R o w s . C o u n t= =0 ) { M e s s a g e B o x . S h o w ( " N od a t aF o u n d " ," C r y s t a l R e p o r t W i t h O r a c l e " ) ; r e t u r n ;

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

6/8

05/12/11

Cr stal Report

ith DataSet and DataTable using C# - CodeProject

/ /S e t t i n gd a t as o u r c eo fo u rr e p o r to b j e c t o b j R p t . S e t D a t a S o u r c e ( D s ) ; C r y s t a l D e c i s i o n s . C r y s t a l R e p o r t s . E n g i n e . T e x t O b j e c tr o o t ; r o o t=( C r y s t a l D e c i s i o n s . C r y s t a l R e p o r t s . E n g i n e . T e x t O b j e c t ) o b j R p t . R e p o r t D e f i n i t i o n . R e p o r t O b j e c t s [ " t x t _ h e a d e r " ] ; r o o t . T e x t=" S a m p l eR e p o r tB yU s i n gD a t aT a b l e ! ! " ; / /B i n d i n gt h ec rs t a l R e p o r t V i e w e rw i t ho u rr e p o r to b j e c t . c r y s t a l R e p o r t V i e w e r 1 . R e p o r t S o u r c e=o b j R p t ;

Poin
M U C A C

of In e e
: C R ) O ( C# ) Q (P SQL MDI MDI

HTTPS SSL ,

Licen e
T L , (CPOL) , T C P O

Abo

he A

ho
S E 4

Rehan Ahmad Abba i I +

. P A . , C#. , V . , A . , VB 6.0, J2ME, A , X , X , S D (P PC 2003), O . E ,


S (S A -J R U M D

, I S ( VB 6.0.

, , O .NET

C KSA A

I O F
E

, C .)

Poor

E cellent Vo e

Commen
Yo m FAQ

and Di c
e hi me

ion
age boa d. ( - ) Search

Sign In o

P R M o e of 5

Medi m

Normal

25

Upda e F P N

amine al epo knai bala enka a i a amk ma hebinbacka E e nal Da a e E adding col mn ing da a e D o da a able and da a able ami .k ma .13 S hi M _5862

6:04 1 J l '11 22:04 18 Ma '11

da a no di pla ed in c M M o e of 5 o e of 3 i ho

20:52 7 Dec '10 8:00 5 A g '10 21:02 11 No 17:51 22 F '09 '11

Repo R :R

D namicall C Ve al Repo hank

3:38 10 J l '09 0:57 22 Ap '09 18:32 25 Ma '09

ni in.mail20 a k iala

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

7/8

05/12/11

Ver

thanks

Cr stal Report

ith DataSet and DataTable using C# - CodeProject


tarksiala Roopangee Tandon Rehan Ahmad Abbasi vinod.chakote

18:32 25 Mar '09 21:04 22 Oct '08 11:45 3 Sep '08 1:52 6 Sep '10 1

Cr stal Report with DataSet and DataTable using C# responce Please Vote This Article Re: Please Vote This Article Last Visit: 7:49 1 Dec '11 General News Last Update: 6:54 5 Dec '11 Suggestion Question Bug

Answer

Joke

Rant

Admin

Use C trl+Left/Right to switch messages, C trl+Up/Down to switch threads, C trl+Shift+Left/Right to switch pages.
P ermalink | A dvertis e | P rivac y | M obile Web0 4 | 2 .5 .1 1 1 2 0 3 .1 | L as t U pdated 8 Sep 2 0 0 8 L ayout: fixed | fluid A rtic le C opyright 2 0 0 8 by Rehan A hmad A bbas i E verything els e C opyright C odeP rojec t, 1 9 9 9 - 2 0 1 1 T erms of U s e

.codeproject.com/KB/cs/Cr stalReportNDataTable.asp

8/8

You might also like