REPORTING WITH REPORTING SERVICES

Oh ok, so i don’t even need to worry downloading crystal reports for my Visual Studio 2010. Reporting services has been there for a while but did not bother to consider because I have been comfortable with crystal reports, but working on my first project with VS 2010 I had to, because was not seeing crystal reports and had to go through some processes to integrate. So i decided to give Reporting services a try and guys it is very cooooool. Are you a beginner in Reporting Services in Visual Studio, then this will offer you a great help.

Lets go through how to create a report using reporting services and dataset as the data source.

It is assumed you are working with a project with a dataset.

1. Right click to add a new item to your project. On the Reporting tab, choose “Report Wizard” and enter the name of your report in the “name” field provided at the bottom of the “add new item” dialog box. Click on the “Add’ button to proceed.

image

2. In this stage , you choose the data source of your report. You start by entering the name of your Dataset. Take note of the name you enter as you will use it in your code. Select the data source ( the dataset of your project) and then select the table you want to display in your report. Click next to go to the next stage.

image

3.Here, you choose the fields to display on your reports and how to group your records, either by rows or columns. Drag the fields you want to display in the “Values” area. In case you want group your records by rows or columns, drag that field to the “row groups” or “column groups “ respectively. Functions like sum,average, etc can be applied to  fields in the “values’ area. Click next to go the next tab

image 

4. In this stage you choose the layout of your report. Select your layout and click on the next button.

image

5. Select your report style and click on the finish button.

image

 

Your report is then displayed with the selected fields. You can then drag the table to a position of your choice on the report form. You can also insert text and image onto your report as it is done in crystal report.

image

After creating the reports you then have to integrate it into your forms. First you need to add a report viewer. You do this by creating a new form and dragging the report viewer onto it as shown below.

image

After this, you write some small codes to view your report.  First you have to make sure the data table is filled programmatically. Let say at form load event we want to display the report. We start by declaring a procedure that can that accepts the dataset name, report name and the datasource.

   1: public void fillReport(String dataset, String reportname, DataTable loadData)

   2:        {

   3:            this.reportViewer1.ProcessingMode = ProcessingMode.Local;

   4:            //choosing the report source

   5:            this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "/" + reportname + ".rdlc";

   6:            //add the datasource to the report

   7:            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataset, loadData));

   8:            this.reportViewer1.RefreshReport();

   9:  

  10:        }

so on form load we fill the data table and call the method above to fill the show the report.

“fillReport("DataSet1", "purchaseSearch", this.dataSet1.land”

Note that the dataset name should be the same as shown in the created report.

Enjoy…

HIDING CONTROLS WHEN PRINTING WITH WINDOW.PRINT()

Sometimes in web applications (ASP.NET) we prefer printing a window without some controls that appear on the interface. For example, printing a window by clicking on a print button without showing the print button on the printed document. In this example we are printing a student’s results without showing the Print and the close buttons.

To start, we place the two control in a DIV control.Drag the “DIV” control from the HTML controls section. Name the DIV control by giving a value to ID property as shown below. Place the print and close button in the DIV control.

clip_image002

Note that in creating ASP.NET applications, there are two types of source codes, the VB/C# source code and the HTML code. First we create a script to hide the DIV control as shown below. In the script below the “dv1” is the ID value of the DIV control

Add this script to the HTML source

   1: <script language="javascript" type="text/javascript">

   2:    function hidePrint(){

   3:     document.getElementById("dv1").style.display='none';

   4:     window.print();

   5:     window.close();

   6:             }

   7: </script>

Note that the script should be in between the <head></head> tag of the html source code

Select the print button and go to the html source code and add the onclick property . The value of the onclick property should be the created javascript function (named hidePrint()). The button’s html code should look like the one below after the addition of the onclick property

   1: <input type="button" id="Button1" runat="server" 

   2: value="Print" class="fltbutton"  style="width:60px" 

   3: onclick="javascript:hidePrint();" />

This is all you have to do and you are good to go.

Below is a window showing the student’s results and the Print and the Close Button

clip_image004

What is printed after clicking on the “print” button

clip_image006

Cheers!!!!