Wednesday, December 22, 2010

Designing the Report

Hey All,


In my previous blog post I created a Crystal Report. Today I’m going to explain on how to design your Crystal Report. Below is the screen shot of the report that I created earlier.
























First I will explain about the different sections of the report. As you can see in the screen shot there are 5 sections in the report.
  1. Report Header
  2. Page Header
  3. Details
  4. Report Footer
  5. Page Footer 


Report Header
Objects that are placed in the Report Header section are printed only once, at the beginning of the report.


Page Header
Objects that are placed in this section are printed at the beginning of each new page.
Note: You cannot place charts or cross tabs in this section.


Details

Objects that are placed inside this section are printed with each new record.


Report Footer  
Objects that are placed in this section are printed only once at the end of the report.

Page Footer

Objects that are place in this section are printed at the end of each page.

Now let’s design the report. First up I will include a company name for the report and the telephone no. You want the company name and the telephone no to be printed only once in the report. Therefore the best place to place these is the Report Header Section. Then it will only be printed once per report.

First right click on the Report Header tab and select Don’t Suppress.



















If you want to add the company logo with the company name and the telephone no as an Image then just right click on the area between the Report Header and the Page Header tabs and the select Insert and then click on Picture. Then select your picture and place it In the Report Header.
























If you want to add them as just text you can insert them by using a text box which u can find in the tool box.

 














Now click on the Text object tool and drag it to your Report Header Section. Then type the details inside the text object. You can add many text objects as you want. If you don’t see the cursor in the text object, then right click on the text object and select Edit Text Object.










In the Page header section you can add objects which you want to be printed in all the pages of the report. In this Report I’m only making the text Bold in the Headings.


I’m leaving the Details Section as it is but if you want to format it you can do it in the same way as above.
In the Report Footer Section you can add any object that needs to be printed at the end of each report. For an example I can place a formula to calculate the total Quantity of the items. (How to implement a formula will be explained in the next blog post.)







I have drawn a line by using a line object which can be found in the Tool Box.

Then in the Page Footer section I’m including the page no which is a special filed. It is automatically added to the Page Footer section at the time of report creation. So it will print the page no per page.

OK now when I run my application I’m seeing my newly designed report with the page no printed on the bottom of the report.












That’s it for this post. In the next post I will explain how to format the fields.

Sunday, December 19, 2010

Creating a Crystal Report


Hey All,
            In this series of blog posts I will explain to you on how to use crystal reports in Visual Studio to create your reports.

First up we need to have a database where the crystal report will take data from. You can create the report by connecting directly to the database or by creating a Data Set in your project and adding a Data Table to it and then creating the report by using that Data Table. In this post I will explain on how to create a crystal report by directly connecting to the database.

For all this demos I will create 3 tables in my database and create reports based on them. The 3 tables are Item, Sold and Sold Items. The SQL code is as follows.
 
CREATE TABLE Item(
itemCode INT,
itemName VARCHAR(50),
quantity INT,
price REAL,
CONSTRAINT pk_item PRIMARY KEY (itemCode)
);

CREATE TABLE Sold(
soldID INT,
dateOfSale DATETIME,
CONSTRAINT pk_sold PRIMARY KEY (soldID)
);

CREATE TABLE SoldItems(
soldID INT,
itemCode INT,
quantity INT,
totalPrice REAL,
CONSTRAINT pk_soldItems PRIMARY KEY (soldID,itemCode),
CONSTRAINT fk_soldItems1 FOREIGN KEY (soldID) REFERENCES Sold (soldID),
CONSTRAINT fk_soldItems2 FOREIGN KEY (itemCode) REFERENCES Item (itemCode)
);

So first up create a new windows project in Visual Studio.































I’m using C# as the language. Then select Windows Forms Application and then create the project. Now we have to add a crystal report to our project.

Right click on your project and click on Add --> New Item.



















Then select Reporting and select Crystal Report and then click on OK then a crystal report will be added to the project.














Then you will get a window as shown below.





















Here you get 3 options. You can either use the report creation wizard or create a blank report or choose an existing crystal report. For this demo I will use the wizard. So select the Using the Report Wizard and click on OK.

The next window that is appearing allows you to connect your report with the data.

 















Here it allows you various ways of connecting to the data. Here we will create a New Connection. So expand the Create New Connection.











Since I’m using SQL Server as my database I will be creating an OLE DB (ADO) connection. So expand OLE DB (ADO) in the list then a new window will appear.





















Select SQL Native Client and then click next.





















You have to provide the connection information to your server here. First you have to select the server. If you are using Windows authentication to connect with data SQL server then tick the Integrated Security check box. If you are using SQL Authentication then provide the User ID and Password. Then select the database in where you created the tables then click on Finish. Then a new connection will be made.
 



















Then select the Item table and then click on the arrow button to place it in the Selected Tables section then click next.



















Select the fields that you want to display in your report and then click on the arrow button. Click next then it will ask you what field needs to be grouped. If you don’t want to group it click next. Then it will ask you what fields are to be filtered. If you don’t want to filter then click next. Then it will ask you to select the format of the report.
 

















Here you can select from a range of styles and once you are finished then click on the finish button then your report will get created.














Ok we have created the report now we have to view it. To do so add a Crystal Report Viewer control to Form1 and write the following code in the Form’s Load event.










Now run the application and you will see the data you added to the Item table. As for me I get the following output.


  
 
Now we have created a crystal report. In my next blog post I will explain how to design the crystal report in Visual Studio.Hope you'll will find my blog interesting.