Você está na página 1de 18

Practical .

NET Series SQL Server Reporting Services Report Viewer

Reporting with Reports Viewer in Visual Studio 2005


Platform Support: .NET 2.

Authors:

C# Corner Authors Team


Pu!lis"e# on: $arc" % & 2 '

(2

' $in#crac)er LL*. +ll rig"ts reserve#. ,nite# States an# -nternational cop.rig"t laws protect t"is propert.. $in#crac)er LL* or t"e aut"or of t"is material "as no affiliation wit" $icrosoft or an. ot"er companies t"at #evelops t"e software. Some )e.wor#s use# in t"is material are registere# tra#emar)s of t"eir respective owners. Repro#uction or printing multiple copies of t"is material is pro"i!ite#. -f .ou nee# multiple copies of t"is material& please contract authors@c-sharpcorner.com.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

ntroduction
T"is #ocument #iscusses t"e ReportViewer control an# "ow to generate reports using t"e ReportViewer control. T"is #ocument covers t"e following topics: ,n#erstan#ing t"e ReportViewer control an# report processing mo#es *reating a simple report /enerating reports from an o!0ect collection /enerating reports from a 1ataSet /enerating reports from an 2$L #ocument

!etting Started with ReportViewer Control


-f .ou "ave written an. reporting applications in .NET& .ou ma. !e familiar wit" *r.stal Reports an#3or SQL Server Reporting Services. T"e ReportViewer control is a new a##ition to Visual Stu#io 2 4& w"ic" is actuall. a SQL Server Reporting Services component. T"is control "as two versions 5in#ows 6orms version an# 5e! version. +s .ou ma. presume& 5in#ows 6orms version is use# to write reporting applications in 5in#ows 6orms applications an# 5e! version is use# to write +SP.NET 5e! applications.

Report Processing $o#es


T"e ReportViewer control supports two report processing mo#es local an# remote. -n local processing mo#e& t"e ReportViewer control runs wit"in t"e client application an# t"e report processing is performe# as a local process on t"e client mac"ine w"ere actual application is running. -n remote processing mo#e& t"e ReportViewer control runs on a SQL Server 2 4 Reporting Services report server. -n t"is mo#e& t"e ReportViewer control is use# to view t"e report t"at is alrea#. generate# on t"e server. +ll processing from #ata retrieval to report ren#ering is performe# on t"e report server. To use remote processing mo#e& .ou must "ave a license# cop. of SQL Server 2 4 Reporting Services. 7ot" 5in#ows an# 5e! controls can !e configure# to run in local processing mo#e or remote processing mo#e.

Namespace an# +ssem!l.


T"e ReportViewer control assem!lies for 5in#ows 6orms an# 5e! 6orms versions are Microsoft.ReportViewer.5in6orms an# $icrosoft.ReportViewer.5e!6orms respectivel..

*reate a Simple Report


Now let8s create a simple report using t"e ReportViewer control. -n t"is application& - will loa# #ata from a SQL Server #ata!ase to generate reports. 9. *reate a 5e! +pplication using Visual Stu#io 2 4 2. 1rag an# #rop a ReportViewer control from Tool!o: to t"e 5e! page %. Rig"t clic) on t"e Pro0ect in Solution E:plorer an# select +## New -tem menu item an# select 1ataSet from t"e list an# )eep 1ataSet9.:s# as name. ;. T"e ne:t screen is Ta!le+#apter wi<ar#& w"ic" as)s .ou =uestion a!out .our #ata!ase connection an# SQL =ueries w"ere .ou want #ata to !e selecte# from. >ust follow t"e wi<ar# steps one !. one an# .ou will en# up seeing 1ataSet in #esigner mo#e wit" .our ta!le name an# columns. *lose t"e #esigner. 5. Now rig"t clic) on t"e Pro0ect again an# select +## New -tem an# select Report from t"e availa!le items an# clic) +## !utton. See 6igure 9.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure 9. 6. -t will a## Report.r#lc file to .our pro0ect an# will open t"e report #esigner& w"ic" loo)s li)e 6igure 2. +s .ou can see in t"e left si#e& .ou will see .our 1ataSet.

6igure 2. 7. Now .ou can e:pan# .our 1ataSet an# #rag w"atever column .ou want in t"e report to t"e report #esigner. select t"ree columns in 6igure %.

6igure %. 8. Now if .ou rig"t clic) an# select Properties on a Te:t7o:& .ou will see 6igure ;& w"ere .ou can appl. settings li)e visi!ilit.& navigation& format& font& an# sorting. 6or now& .ou can sa. ?@ on t"is #ialog. - will #iscuss t"ese properties in more #etails in m. following articles.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure ;. 9. Now go to t"e 5e! page an# clic) t"e smart tag on ReportViewer an# select Report.r#lc from t"e list. See 6igure 4.

6igure 4. 9 . T"at8s it. Now if .ou run t"e application& .ou will see report. Aou can navigate t"roug" recor#s& an# e:port it to E:cel or P16 using t"e E:port option.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

"uilding Reports #rom an $%&ect Collection


+n o!0ect collection is -n t"is article& - will #iscuss "ow to create reports from a !usiness o!0ects collection. - will !e creating a 5in#ows 6orms application wit" reporting !eing processe# as local processing mo#e. See m. article& /etting starte# wit" ReportViewer control to un#erstan# local an# remote processing mo#es.

Step 9. *reate an ?!0ects *ollection


- "ave a class t"at stores an emplo.ee8s information. T"e class loo)s li)e Listing 9.
class Employee { private string name; public string Name { get { return name; } set { name = value; } } private string address; public string Address { get { return address; } set { address = value; } } private string ssn; public string Ssn { get { return ssn; } set { ssn = value; } } private Int16 age; public Int16 Age { get { return age; } set { age = value; } } public Employee(string EmpName { t"is#name = EmpName; t"is#address = EmpAddress; t"is#ssn = EmpSsn; t"is#age = EmpAge; } } string EmpAddress string EmpSsn Int16 EmpAge!

Listing 9. Now - !uil# a *ompan. class& w"ic" is a generic collection of Emplo.ee wit" t"ree recor#s in it. T"e *ompan. class is liste# in Listing 2.
class $ompany { private %ist&Employee' m(employees; public $ompany(! { m(employees = ne) %ist&Employee'(!; m(employees#Add(ne) Employee(*+a"es" $"and* *11, Ne) -oad $"adds .ord /A* *1,01,11 02!!; m(employees#Add(ne) Employee(*3ac4 +o"ita* */ear %ane Ne) 5or4 ,0,01* *67611,1 ,0!!; m(employees#Add(ne) Employee(*-enee Singer* *Near medo) /"iladelp"ia /A* *9621221 ,2!!; } public %ist&Employee' :etEmployees(! { return m(employees; }

1,1,* ,008* ,0,2*

Listing 2. -n Listing 2& t"e /etEmplo.ees met"o# returns all t"e emplo.ees in t"e compan..

Step 2. +## a Report to t"e Pro0ect

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer


Now a## a report to t"e pro0ect !. Rig"t clic) on t"e pro0ect B +## B New -tem an# select Report from t"e items listing. -t will a## Report9.r#lc file to t"e pro0ect.

Step %. +## a 1ata Source


Now - am going to a## a #ata source to t"e pro0ect. 1ou!le clic) on .our 6orm so 6orm 1esigner is open. Now from t"e 1ata menu of Visual Stu#io& select +## New 1ata Source item. See 6igure 9.

6igure 9. -t will launc" t"e 1ata Source *onfiguration 5i<ar#. Select ?!0ect item on t"e first page. See 6igure 2.

6igure 2. Now on ne:t screen .ou s"oul# see .our application namespace. -f .ou #on8t see t"e namespace& ma)e sure to Re!uil# t"e application. -f .ou e:pan# t"e namespace& .ou s"oul# see Emplo.ee class. See 6igure %. Select Emplo.ee an# clic) Ne:t an# clic) 6inis" on t"e ne:t screen.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure %. Now .ou s"oul# see Emplo.ees in 1ata Sources win#ow. See 6igure ;.

6igure ;. Note: 5"en .ou a## a 1ataSource to t"e pro0ect& t"e #esigner a##s t"e Emplo.ee7in#ingSource to t"e #esigner& w"ic" will pla. a ma0or role later.

Step ;. 7in# 1ata Source wit" Report


Now open Report9.r#lc file an# #rag an# #rop a Ta!le from t"e Tool!o: to t"e report #esigner. +## Name& +##ress& +ge& an# Ssn items from t"e 1ataSource to t"e secon# row of t"e ta!le. See 6igure 4. Note: 6irst row in t"e ta!le is Cea#er an# t"ir# row in t"e ta!le is 6ooter. T"e secon# row is t"e 1etails row. $a)e sure .ou #o not #rop t"e items on t"e 6irst an# T"ir# rows of t"e ta!le& ot"erwise .ou will see onl. one recor# in .our report.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure 4. Now .ou can format t"e ta!le t"e wa. .ou want !. rig"t clic)ing on t"e ta!le& column& or a cell an# select Properties item.

Step 4. 7in#ing Emplo.ee7in#ingSource an# 1ata


Now last step is to !in# Emplo.ee7in#ingSource wit" t"e Emplo.ee #ata. -f .ou remem!er& we a##e# a /etEmplo.ees met"o# to t"e *ompan. class t"at returns all t"e Emplo.ees in t"at compan.. Now we simpl. call /etEmplo.ees& w"ic" returns a collection an# set 1ataSource propert. of Emplo.ee7in#ingSource as seen in Listing %. private void .orm1(%oad(ob;ect sender EventArgs e! { $ompany company = ne) $ompany(!; Employee<indingSource#=ataSource = company#:etEmployees(!; t"is#report>ie)er1#-e?res"-eport(!; } Listing %.

Step D. 7uil# an# Run


Now !uil# an# run t"e application. T"e output loo)s li)e 6igure D.

6igure D.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer

"uilding Reports #rom '() *ocuments


-n t"is stepE!.Estep tutorial& .ou will learn "ow to create reports from an 2$L #ocument using t"e ReportViewer control an# Visual Stu#io 2 4.

T"e 1ata

- "ave an 2$L file 1ata.:ml& w"ic" loo)s li)e t"is: FG:ml versionHI9. I enco#ingHIutfEJI GB - F*ompan.B - FEmplo.eesB FEmplo.ee NameHI(ahesh ChandI +geHI+0I P"oneHI,-0-2+++++I 3B FEmplo.ee NameHIRose !arnerI +geHI5,I P"oneHI2-++.2/00/I 3B FEmplo.ee NameHIAmger 1apI +geHI22I P"oneHI2022+.2/00I 3B FEmplo.ee NameHI(i3e !oldI +geHI+5I P"oneHI220/0///2+I 3B FEmplo.ee NameHIRenee 4lowerI +geHI-2I P"oneHI././20-00+I 3B F3Emplo.eesB F3*ompan.B

Step 9. /enerate an# +## t"e Sc"ema 6ile

$. first goal is to generate sc"ema file& w"ic" will represent t"e #ata. - ta)e "elp of t"e 1ataSet class an# its met"o# 5rite2mlSc"ema. T"e co#e liste# in Listing 9 rea#s t"e 1ata.:ml file an# generates a sc"ema file calle# 1ata.:s#. T"is file is create# in t"e Debug fol#er of .our application. 1ataSet #s H new 1ataSetKLM #s.Rea#2mlKI1ata.:mlILM #s.5rite2mlSc"emaKI1ata.:s#ILM Listing 9. Let8s a## 1ata.:s# file to .our pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer& select +## BB E:isting -tem an# !rowse for 1ata.:s# file an# a## it to t"e pro0ect. Now let8s Re!uil# t"e pro0ect.

Step 2. *reate t"e Report

Now we will a## a new report file to t"e pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer an# select +## BB New -tem an# select Report from t"e -tems list. -t will a## Report9.r#lc file to t"e pro0ect. ?nce t"e report is a##e#& our ne:t step is to a## a #ata source. 6irst #ou!le clic) on t"e 6orm an# select 1ata $enu item from t"e $ain $enu. *lic) on 1ata $enu item an# select +## New 1ata Source item. -t will launc" 1ata Source *onfiguration 5i<ar#. Select ?!0ect from t"e list an# clic) t"e Ne:t !utton on t"e 5i<ar#. See 6igure 2.

*op.rig"t 2

' $in#crac)er LL*

Practical .NET Series SQL Server Reporting Services Report Viewer


6igure 2. ?n ne:t #ialog& .ou s"oul# see all namespaces an# classes in .our pro0ect. E:pan# .our namespace an# .ou will see class *ompan.. See 6igure %.

6igure %. Select *ompan. class an# clic) t"e Ne:t !utton. ?n ne:t #ialog& .ou will see a confirmation message. Select 6inis" t"ere an# get out of t"e wi<ar#. Now #ou!le clic) on t"e Report9.r#lc file an# .ou s"oul# see 6igure ; in .our 1ata Sources win#ow.

6igure ;. Now let8s create an# format t"e report. 1rag a Ta!le from t"e Tool!o: an# #rag Name& +ge& an# P"one columns from t"e 1ata Sources to t"e report8s mi##le row. +s .ou can see from 6igure 4& t"e name of t"e column is automaticall. a##e# to t"e "ea#er KfirstL row of t"e report.

*op.rig"t 2

' $in#crac)er LL*

10

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure 4.

Step %. *reate a Report Viewer an# 7in# t"e Report


Now open t"e 6orm9 again an# #rag a ReportViewer control from t"e Tool!o: to t"e 6orm. *lic) on t"e smart tag an# select Report9.r#lc from t"e *"oose Report #rop #own list. See 6igure D.

6igure D. 7. #oing so& .ou will see an Emplo.ee7in#ingSource control is a##e# at t"e !ottom of t"e 6orm. See 6igure '. T"e 7in#ingSource control provi#es connection !etween t"e #ata an# t"e ReportViewer control.

6igure '.

Step ;. 6ill t"e 1ata

Now write t"e co#e liste# in Listing 2 on t"e 6orm8s loa# event "an#ler. T"is co#e creates a 1ataSet& loa#s t"e #ata from 1ata.:ml file an# sets Emplo.ee7in#ingSource.1ataSource as 1ataSet. T"e last line is a##e# !. .ou !. t"e #esigner. =ataSet ds = ne) =ataSet(!; ds#-ead@ml(*=ata#Aml*!; Employee<indingSource#=ataSource = ds; t"is#report>ie)er1#-e?res"-eport(!; Listing 2.

Step 4. 7uil# an# Run

T"at8s all. 7uil# an# run t"e application. T"e output s"oul# loo) li)e 6igure J.

*op.rig"t 2

' $in#crac)er LL*

11

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure J.

*op.rig"t 2

' $in#crac)er LL*

12

Practical .NET Series SQL Server Reporting Services Report Viewer

"uilding Reports #rom a *ataSet


-n t"is stepE!.Estep tutorial& .ou will learn "ow to create reports from a 1ata!ase using a 1ataSet an# t"e ReportViewer control an# Visual Stu#io 2 4.

Step 9. /enerate a T.pe# 1ataSet

6irst of all& we nee# a t.pe# 1ataSet t"at will represent t"e #ata t.pes. Aou can generate a t.pe# 1ataSet from a #ata!ase ta!le& store# proce#ure& an# a view or from a SQL =uer.. +fter generating t"e t.pe# 1ataSet& report generation is same. Rig"t clic) on t"e pro0ect in Solution E:plorer& select +## BB New -tem an# select 1ataSet from t"e list. *"ange 1ataSet9.:s# to *ompan..:s#. +fter a##ing t"e 1ataSet& .ou will see t"e #esigner. ?n t"e #esigner& clic) Server E:plorer lin)& w"ic" will let .ou create a 1ata!ase connection an# can view #ata!ase o!0ects inclu#ing ta!les& views& an# store# proce#ures. 1rag a #ata!ase ta!le& or view& or store# proce#ure w"ere .ou want to #ispla. t"e #ata from an# it will create a 1ataSet sc"ema for .ou. ?nce .ou "ave a t.pe# 1ataSet& 0ust follow t"ese steps. 7efore t"at& ma)e sure .ou Re!uil# t"e pro0ect.

Step 2. *reate t"e Report

Now we will a## a new report file to t"e pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer an# select +## BB New -tem an# select Report from t"e -tems list. -t will a## Report9.r#lc file to t"e pro0ect. ?nce t"e report is a##e#& our ne:t step is to a## a #ata source. 6irst #ou!le clic) on t"e 6orm an# select 1ata $enu item from t"e $ain $enu. *lic) on 1ata $enu item an# select +## New 1ata Source item. -t will launc" 1ata Source *onfiguration 5i<ar#. Select ?!0ect from t"e list an# clic) t"e Ne:t !utton on t"e 5i<ar#. See 6igure 2.

6igure 2. ?n ne:t #ialog& .ou s"oul# see all namespaces an# classes in .our pro0ect. E:pan# .our namespace an# .ou will see class *ompan.. See 6igure %.

*op.rig"t 2

' $in#crac)er LL*

13

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure %. Select *ompan. class an# clic) t"e Ne:t !utton. ?n ne:t #ialog& .ou will see a confirmation message. Select 6inis" t"ere an# get out of t"e wi<ar#. Now #ou!le clic) on t"e Report9.r#lc file an# .ou s"oul# see 6igure ; in .our 1ata Sources win#ow.

6igure ;. Now let8s create an# format t"e report. 1rag a Ta!le from t"e Tool!o: an# #rag Name& +ge& an# P"one columns from t"e 1ata Sources to t"e report8s mi##le row. +s .ou can see from 6igure 4& t"e name of t"e column is automaticall. a##e# to t"e "ea#er KfirstL row of t"e report.

*op.rig"t 2

' $in#crac)er LL*

14

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure 4.

Step %. *reate a Report Viewer an# 7in# t"e Report


Now open t"e 6orm9 again an# #rag a ReportViewer control from t"e Tool!o: to t"e 6orm. *lic) on t"e smart tag an# select Report9.r#lc from t"e *"oose Report #rop #own list. See 6igure D.

6igure D. 7. #oing so& .ou will see an Emplo.ee7in#ingSource control is a##e# at t"e !ottom of t"e 6orm. See 6igure '. T"e 7in#ingSource control provi#es connection !etween t"e #ata an# t"e ReportViewer control.

6igure '.

Step ;. 6ill t"e 1ata

Now write t"e co#e liste# in Listing 2 on t"e 6orm8s loa# event "an#ler. T"is co#e creates a 1ataSet& loa#s t"e #ata from 1ata.:ml file an# sets Emplo.ee7in#ingSource.1ataSource as 1ataSet. T"e last line is a##e# !. .ou !. t"e #esigner. 1ataSet #s H new 1ataSetKLM #s.Rea#2mlKI1ata.:mlILM Emplo.ee7in#ingSource.1ataSource H #sM t"is.reportViewer9.Refres"ReportKLM Listing 2.

Step 4. 7uil# an# Run

T"at8s all. 7uil# an# run t"e application. T"e output s"oul# loo) li)e 6igure J.

*op.rig"t 2

' $in#crac)er LL*

15

Practical .NET Series SQL Server Reporting Services Report Viewer

6igure J.

*op.rig"t 2

' $in#crac)er LL*

16

Practical .NET Series SQL Server Reporting Services Report Viewer

mplementing Search in Reports


T"e ReportViewer control provi#es 6in# an# 6in#Ne:t met"o#s to fin# a te:t in t"e report. T"e 6in# met"o# ta)es two parameters t"e te:t an# t"e starting page num!er. - "ave 6orm wit" a Te:t7o: an# a 7utton control. See 6igure 9.

6igure 9. - #efinite t"e following varia!le in t"e class: private bool ?irst.ound = ?alse; T"e following co#e written on t"e 6in# 7utton clic) event "an#ler. private void .ind<utton($lic4(ob;ect sender EventArgs e! { i? (?irst.ound! t"is#report>ie)er1#.indNeAt(!; else i? (t"is#report>ie)er1#.ind(.indBeAt<oA#BeAt 1! '= 2! ?irst.ound = true; } Now if - t.pe N*"an#O in t"e Te:t7o: an# clic) t"e 6in# !utton& t"e first row is selecte#. *lic)ing 6in# again select t"e ne:t row t"at "as te:t *"an# in it. See 6igure 2.

6igure 2.

*op.rig"t 2

' $in#crac)er LL*

17

Practical .NET Series SQL Server Reporting Services Report Viewer

*op.rig"t 2

' $in#crac)er LL*

18

Você também pode gostar