Você está na página 1de 41

Introduction

I was searching the internet for Crystal reports in 2010 and I found in 2005, but was not able to find any particular tutorials for 2010. So I thought, let me take a chance to write it for some beginners like me who have not worked onCrystal Reports earlier. In this article, I will show you simple report creation process with screenshots. A picture is worth more than a thousand words, so I always believe in an article with screenshots. et!s start by creating a new website in "S2010. See the following screen#

Figure 1

As per figure $, create a new website in "S2010 and name it as per your choice. %ow let me show you the database table structure.

Figure 2

&he above figure shows the db table structure. And the below figure 'Figure 3) will show you some sample data in the table#

Figure 3

If you want to run this sample pro(ect directly, then you can download the database script from the link at the top. %ow we have to create an )sd file as a blank data source as we are going to use strong data type. *ere I will divide this tutorial in 5 sub sections as mentioned below# Simple report using Crystal Reporting Tool Group report Chart report Sub report Cross tab report

Simple Report using Crystal Report


&he below figure shows you the process to create an +S, file. -or adding an +S, file, click on Solution .)plorer /0 1ight 2lick on 3ro(ect /0 click on Add new Item and then it will show you the below screen.

Figure 4

2lick on the ok button, so it will ask for confirmation to put that file in App_Code folder. 4ust click ok and that file will open in the screen as a blank screen. %ow we will add one blank datatable to that +S,file. 4ust right click on the file and select Add -> Datata le. It will add one DataTable1 to the screen. -igure 5 shows how to add datatable to +S, file.

Figure 5

%ow datatable1 is added to +S, file. %ow we will add data column to the datatable1 as per figure 5. 1emember whatever fields 'columns6 we add here, it will be available to show on the report. So add column which you want to display in your reports one by one here.

Figure 6

1emember to give the e)act same name for data column as in database and also select data type which is the same as database, otherwise you will get an error for field and data type mismatch. 7nce we add all the re8uired columns in datatable, then set property for the datacolumn as it has in database. &he below figure will show you how to set property for data columns. ,efault datatype for all the columns is string here so if datatype is other than string then only change it manually. 4ust right click on the datacolumn in datatable and select property and from property window, select appropriate datatype from DataType Dropdown for that datacolumn.

Figure 7

&hat!s it. +S, file creation has been done. %ow we will move to create Crystal report design. 4ust click on the Solution .)plorer /0 1ight click on the pro(ect name and select crystal reports. %ame it as per your choice and hit the add button. -igure 9 will show you the creation process of Crystal reports.

Figure 8

2lick on the add button and one .rpt file will be added to the solution. And also, it will ask for the report creation type of how you want to create the report. -igure : will show you a screenshot.

Figure 9

4ust click ok button to proceed. It will lead you to figure $0#

Figure 10

;nder pro(ect data, e)pand AD!"#$% Datasets and select DataTable1 and add to the selected table portion located at the right side of the windows using > button. %ow click on the -inish button and it will show the ne)t screen '-igure $$6#

Figure 11

7nce report file is added, you can see Field $&plorer on the left side near server e)plorer. .)pand Data ase Fields, under that you will be able to find ,atatable that we have created earlier. 4ust e)pand it and drag one by one filed from -ield .)plorer to the rpt file under detail section" %ow the report design part is over. %ow we have to fetch the data from database and bind it to dataset and then bind that dataset to the report viewer. et!s go step by step. -irst ,rag a CrystalReportViewer control on asp) page from tool bo) as per below screen#

Figure 12

%ow we will fetch the data, pass data to the dataset and then add that dataset to the Crystal 1eport. <elow is the 2= code which will do the (ob#
2ollapse > 2opy 2ode
using using using using using CrystalDecisions.CrystalReports.Engine; CrystalDecisions.Shared; System.Data; System.Data.SqlClient; System.Con iguration;

<elow is the final code for reports#


2ollapse > 2opy 2ode
protected !oid "age#$oad%ob&ect sender' E!ent(rgs e) * ReportDocument rptDoc + new ReportDocument%); dsSample ds + new dsSample%); // .xsd file name DataTable dt + new DataTable%); // Just set the name of data table dt.Table,ame + -Crystal Report Example-; dt + get(ll.rders%); //This function is located below this function ds.Tables/01.2erge%dt); // Your .rpt file path will be below rptDoc.$oad%Ser!er.2ap"ath%-..3Reports3SimpleReports.rpt-)); //set dataset to the report viewer. rptDoc.SetDataSource%ds); CrystalReportViewer1.ReportSource + rptDoc; 4

public DataTable get(ll.rders%) * //Connection string replace 'databaseservername' with your db server name string sqlCon + -5ser 6D+sa;"7D+sa; ser!er+databaseser!ername;6,6T6($ C(T($.8+SampleD9;- : -"ERS6STSEC5R6T; 6,<.+<($SE;Connect Timeout+0-; SqlConnection Con + new SqlConnection%sqlCon); SqlCommand cmd + new SqlCommand%); DataSet ds + null; SqlData(dapter adapter; try * Con..pen%); //Stored procedure calling. t is already in sample db. cmd.CommandTe=t + -get(ll.rders-; cmd.CommandType + CommandType.Stored"rocedure; cmd.Connection + Con; ds + new DataSet%); adapter + new SqlData(dapter%cmd); adapter.<ill%ds' -5sers-); 4 catch %E=ception e=) * throw new E=ception%e=.2essage); 4 inally * cmd.Dispose%); i %Con.State >+ ConnectionState.Closed) Con.Close%); 4 return ds.Tables/01; 4

%ow (ust save everything and run report. It will look like the below figure#

Figure 13

'rouping in Crystal Report *ere we will see only report design and rest of the things, you can refer from Section 1. *ere we will groupCustomer, "roduct, .rder and ?uantity. -or details, (ust go through -igure $?. 4ust add a report '.rpt6 file to the solution. Select the appropriate dataset from popup window. 7nce it!s done, then select grouping option like figure $?.

Figure 14

%ow, right click on the report, select Report -> 'roup $&perts and the resulting window will look like figure $5#

Figure 15

%ow we want to group like Customer #ame and (roduct name so first add Customer to the right 3anel. &hen move product name to the right panel like figure $5#

Figure 16

&his time Crystal report design will be different than the previous time. See figure $@.

8roup@eaderSection1 and 8roup@eaderSectionA are added to the report designer.


*ere Aroup =$ %ame refers to 2ustomer %ame and Aroup =2 %ame refers to 3roduct %ame.

And also 8roup<ooterSection1 and 8roup<ooterSectionA are added below if you want to add something to group footer.

Figure 17

%ow under every group, we want to show the number of orders per customer and productwise, so for that, we have to add summary to the 8roup<ooterSectionA. 1efer to -igure $9.

Figure 18

1ight 2lick on the 8roup<ooterSection select Insert -> Summary" It will show you the ne)t screen '-igure $:6. And I have also added Order_ID and Product_Qty field to the detail 'section B6 part.

Figure 19

In summary window, select the column which you want to summariCe in the first dropdown. Select Sum '-irst option6 from the calculate drop down. Summary ocation is already set to the report footer. So (ust click ok to place that summary field to the report. <y default, Summary field is added to the 1eport -ooter section so move it to the group<ooterSectionA if you want to group product wise, move it to the 8roup<ooterSection1 if you want to group 2ustomer wise or keep it at original place if you want to sum all ordered products. I have moved to the <ooterSection1 so it will show 2ustomer Dise &otal Euantity. 1efer to -igure 20.

Figure 20

%ow save the report and run it finally. It looks like figure 2$.

Figure 21

C)art in Crystal Report


2hart is the most important and visible part of the reporting tool. Crystal has very powerful feature to add chart in report. et!s see how to add chart in 21. *ere also, we will see only designing of the chart for other thing. 3lease refer to Section 1. *ere we will show customer wise product ordered 8uantity in chart. + portion will display Customer name and F portion will display customers total ordered 8uantity. -irst add charts to the report design. 1ight click on the .rpt file and select Insert->C)art. 1efer to figure 22.

Figure 22

7nce you add chart to the report, it will not show chart on the report file but with mouse pointer you can see one blank rectangle is moving. So (ust click on the 1eport header. It will open popup for chart style and other options. 1efer to figure 2B.

Figure 23

%ow from type tab, select type of the charts like bar chart, line chart, pie chart, etc. form the left side. Select sub type from the right pane like side y side c)art, percentage ar c)art, etc. I am not going into the detail of it. I am leaving it for you to practice work. And also select vertical or horiContal radio button from the below section if you want to change the chart style vertically or horiContally. 2heck *se dept) e++ect check bo) if you need shadow effect on the graph. 1efer to figure 2B.

Figure 24

As per figure 2?, move to the ne)t tab data. &here are three bo)es, available fields, on change of and show values. So move 2ustomer %ame from available fields to on changes of bo), and move 3roduct Euantity filed to the show value bo) and click ok. %ow you can see chart is added to the report header section as per figure 25.

Figure 25

%ow, (ust save the report and run it. Fou can see a 1eport as a chart on the screen.

Report Inside Report ,Su

Report)

Crystal reports provide reports inside report feature which are normally known as a subreport feature. et me e)plain it in detail. *ere also, we will design only sub report design. -or rest of the things, refer to Section 1. Add new report to the solution. &hen add Report->'roup and select only Customer name because we want to design report for each customer and sub report product wise. So there will be only one group header inside theCustomergroup header as per figure 25.

Figure 26

%ow right click on ,etail section and select Insert->Su report" 1efer to figure 2@.

Figure 27

7nce we add subreport, it will show screen like figure 29.

Figure 28

As per figure 29, by default, choose a Crystal Report in project is selected if you want to add report from the pro(ect, then otherwise select create a subreport ith the report i!ar"# 7nce we select create a subreport with 1eport DiCard 'Brd radio button6, we have to click on the 1eport DiCard button to select report type and data source (ust do as (art 1 first. &hen click on ok button so like chart report it will show a moving rectangle around mouse, click on the detail section where you want to show subreport. %ow to edit the sub report, refer to figure 2:.

Figure 29

2lick on the edit subreport option and format the report as per your need. *ere I will suggest add product name and product 8uantity or you can add chart also for sub report. Dhen you click on the subreport button, it will open subreport designer, actually 21 will create a separate .rpt file but it will remain hidden inside the main .rpt file so we can!t see it.. %ow run the report and you can see the result, report inside report like figure B0.

Figure 30

*ere number $ is the main report and number 2 is the subreport it!s showing title as 3roduct wise.

Cross %a

Report in Crystal Report

-irst, let me make it clear as to what is a 2ross tab report. %ormally, we generate report row wise like first we show customer name, then product wise, etc. <ut suppose we want to see report as column wise like product name should be displayed as column in report, then cross tab report comes into the picture. See result of 2ross &ab report in figure B$.

Figure 31

*ere also, I will show how to design cross tab report only, for rest of the things, refer to Section 1. -irst add .rpt file to the solution. &hen add cross report to the 1eport *eader section as per the below figure '1efer to figure B26. 1emember we can add cross tab report only in 1eport header or report footer section.

Figure 32

7nce we click on cross tab report options, it will show moving rectangle around mouse pointer (ust place it to the report header section. As we click on header section, it will lead to the figure BB.

Figure 33

As per the figure, move Customer name field to the 1ows section, "roduct name we want to show as a 2olumn so move it to the 2olumns fields, and we want to show product total so move it to the summariCed fields. &hat!s it. 4ust run the report and you can see the output as shown in figure B$. It!s as simple as that. If you have any 8ueries regarding any type of the above mentioned reports, please let me know by way of comments. I will try my level best to fulfill your re8uest. et!s en(oy reportingG Article Source#

,ot%et Stuff

Grouping
Here we will see only report design and rest of things you can refer from the Tutorial part - 1. Here we will group CustomerProduct Order and Quantity. For details just go through figure 14. Just add a report .rpt! file to the solution. "elect the appropriate dataset from popup window. Once it#s done then select grouping option li$e figure 14.

Figure 14 %ow right clic$ on the report select Report Group Experts and &esulting window will loo$ li$e figure 1'

Figure 15 %ow we want to group li$e Customer Name and Product name so first add Customer to the right Panel. (hen mo)e product name to the right panel li$e figure 1*

Figure 16

(his time crystal report design will +e different than pre)ious time. "ee figure 1, -roupHeader"ection1 and -roupHeader"ection. are added to the report designer. Here -oup /1 %ame refers Customer %ame and -roup /. %ame refer Product %ame 0nd also -roupFooter"ection1 and -roupFooter"ection. is added +elow if you want to add something to group footer.

Figure 17 %ow under e)ery group we want to show the num+er of order per customer and product wise so for that we ha)e to add summary to the -roupFooter"ection.. &efer Figure 11

Figure 1 &ight Clic$ on the -roupFooter"ection select !nsert "ummar#$ 2t will so you ne3t screen Figure 14!. 0nd 2 ha)e also added %rder&!' and Product&(t# field to the detail section5! part.

Figure 1) 2n summary window select the column which you want to summarise in first dropdown. "elect "um First option! from the calculate drop down. "ummary 6ocation is already set to the report footer. "o just clic$ o$ to place that summary field to the report. 7y default "ummary field is added to the &eport Footer section so mo)e it to the groupFooter"ection. if you want to group product wise8 mo)e it to the -roupFooter"ection1 if you want to group Customer 9ise or $eep it at original place if you want to sum all ordered product. 2 ha)e mo)e to the Footer"ection1 so it will show Customer 9ise (otal Quantity. &efer Figure .:

Figure *+ %ow sa)e the report and run it finally it loo$s li$e figure .1

Figure *1

1. 2. 3. 4. 5.

Other post for this tutorials Simple report using Crystal Reporting Tool. Group report.(Current) Chart report. Sub report. Cross tab report.

2t#s as simple as that. 2f you ha)e any ;uery regarding any type of a+o)e mentioned report just let me $now +y comment 2 will try my le)el +est to fulfil your re;uest. 6et#s enjoy reporting...................

No, ,e ,ill -etc. t.e data/ pass data to t.e dataset and t.en add t.at dataset to t.e Cr#stal Report$ 0elo, is t.e C1 Code ,.ic. ,ill do t.e 2o3
using Crystal<ecisions.Crystal&eports.=ngine> using Crystal<ecisions."hared> using "ystem.<ata> using "ystem.<ata.";lClient> using "ystem.Configuration> 7elow is the final code for reports. protected )oid Page?6oad o+ject sender8 =)ent0rgs e! @ &eport<ocument rpt<oc A new &eport<ocument !> ds"ample ds A new ds"ample !> BB .3sd file name <ata(a+le dt A new <ata(a+le !> BB Just set the name of data ta+le dt.(a+le%ame A CCrystal &eport =3ampleC> dt A get0llOrders !> BB(his function is located +elow this function ds.(a+lesD:E.Ferge dt!> BB Gour .rpt file path will +e +elow rpt<oc.6oad "er)er.FapPath C..B&eportsB"imple&eports.rptC!!> BBset dataset to the report )iewer. rpt<oc."et<ata"ource ds!> Crystal&eportHiewer1.&eport"ource A rpt<oc> I pu+lic <ata(a+le get0llOrders ! @ BBConnection string replcae Jdata+aseser)ernameJ with your d+ ser)er name string s;lCon A CKser 2<Asa>P9<Asa> ser)erAdata+aseser)ername>2%2(206 C0(06O-A"ample<7>P=&"2"( "=CK&2(G 2%FOAF06"=>Connect (imeoutA:C> ";lConnection Con A new ";lConnection s;lCon!> ";lCommand cmd A new ";lCommand !> <ata"et ds A null> ";l<ata0dapter adapter> try @ Con.Open !> BB"tored procedure calling. 2t is already in sample d+. cmd.Command(e3t A Cget0llOrdersC> cmd.Command(ype A Command(ype."toredProcedure> cmd.Connection A Con> ds A new <ata"et !> adapter A new ";l<ata0dapter cmd!> adapter.Fill ds8 CKsersC!>

I catch =3ception e3! @ throw new =3ception e3.Fessage!> I finally @ cmd.<ispose !> if Con."tate LA Connection"tate.Closed! Con.Close !> I return ds.(a+lesD:E> I

Using Charts in Crystal Reports


Chart is the most important and )isi+le part of the reporting tool. Crystal has )ery powerful feature to add chart inreport. 6et#s see how to add chart in C&. Here also we will see only designing of the chart for other thing please refers PartM1. Here we will show customer wise product ordered ;uantity in chart. N portion will display Customer name and G portion will display customers total ordered ;uantity. First add charts to the report design. &ight clic$ on the .rpt file and select !nsert C.art refer figure ..

Figure ** Once you add chart to the report it will not show chat on the report file +ut with mouse pointer you can see one +lan$ rectangle is mo)ing. "o just clic$ on the &eport header it will open popup for chart style and other options. &efer figure .5

Figure *4 %ow from type ta+ select type of the charts li$e +ar chart8 line chart8 part chart etc form the left side. "elect su+ type from the right pane li$e side 3# side c.art/ percentage 3ar c.art etc 2 am not going to the detail of it. 2 am lea)ing it for you to practice wor$. 0nd also select )ertical or horiOontal radio +utton from the +elow section if you want to change the chart style )ertically or horiOontally. Chec$ 5se dept. e--ect chec$ +o3 if you need shadow effect on the graph. &efer figure .5

Figure *4 0s per figure .4 mo)e to the ne3t ta+ data there are three +o3es8 a)aila+le fields8 on change of and show )alues. "o mo)e Customer %ame from a)aila+le fields to on changes of +o38 and mo)e Product Quantity filed to the show )alue +o3 and clic$ o$. %ow you can see chart is added to the report header section as per figure .'

Figure *5 %ow just sa)e the report and run it you can see a &eport as a chart on the screen.

Report inside Report


Crystal reports pro)ide reports inside report feature which normally $nown as a su+report feature. 6et me e3plain it in details. Here also we will design only su+ report design for rest of the thing refer part1 0dd new report to the solution. (hen add Report Group and select only Customer name +ecause we want to design report for each customer and su+ report product wise. "o there will +e only one group header inside the Customergroup header as per figure .*.

Figure *6 %ow right clic$ on <etail section and select !nsert "u3report re-er figure .,

Figure *7 Once we add su+report it will show screen li$e figure .1

Figure * 0s per figure .1 +y default choose a Crystal Report in project is selected if you want to add report from the project then otherwise select create a subreport with the report wizard. Once we select create a su+report with&eport 9iOard 5rd radio +utton! we ha)e to clic$ on the &eport 9iOard +utton to select report type and data source just do as Part - 1 first. (hen clic$ on o$ +utton so li$e chart report it will show a mo)ing rectangle around mouse 8 clic$ on the detail section where you want to show su+report. %ow to edit the su+ report refer figure .4

Figure *) Clic$ on the edit su+report option and format the report as per your need here 2 will suggest add product name and product ;uantity or you can add chart also for su+ report. 9hen you clic$ on the su+report +utton it will open su+report designer8 actually C& will create a separate .rpt file +ut it will +e remain hidden inside the main .rpt file so we can#t see it. %ow run the report and you can see the result8 &eport inside report li$e figure 5:

Figure 4+ Here no 1 is the main report and no . is su+report it#s showing title as Product wise.

Add CrossTab Report in Crystal Report


First let me ma$e you clear what is Cross ta+ report. %ormally we are generating report row wise li$e first we show customer name then product wise etc. 7ut suppose we want to see report as a column wise li$e product name should +e display as column in report then cross ta+ report come into picture. "ee result of Cross (a+report in figure 51

Figure 41 Here also 2 will show how to design cross ta+ report only8 for rest of the thing refer Part-1. First add .rpt file to the solution. (hen add cross report to the &eport Header section as per +elow figure &efer figure 5.! &emem+er we can add cross ta+ report only in &eport header or report footer section.

Figure 4* Once we clic$ on cross ta+ report options it will show mo)ing rectangle around mouse pointer just place it to thereport header section. 0s we clic$ on header section it will lead to the figure 55

Figure 44 0s per figure mo)e Customer name field to the &ows section8 Product name we want to show as a Column so mo)e it to the Columns fields8 and we want to show product total so mo)e it to the summariOed fields. (hat#s it.Just run the report and you can see the output as figure 51.

Você também pode gostar