The purpose of this article is to create a report with windows service using the schedule timer. Sometimes users need to create reports without a GUI and they would like to create it with schedule base. These are the times you would choose windows service and crystal reports.

Introduction:

Without good reports no financial application can be a 100% completed or a desired system for end users.  Most end users like to read data in a simple format, say they can identify the primary key data, date column data and so on.  I’m going to elaborate in this article how we can create a report using schedule base with windows service and crystal reports.

Prerequisites:

1. Sql server 2005 express
2. Visual studio 2005/2008
    
First you need to design a database for this article demonstration.

 
For this demonstration we’ll design a simple database with a table. The table is called BookInfo.

Then you need to create a project in visual studio. The type should be windows service project and named as “ReportWithWidowsService. You can download sample project end of this article.
 


Let’s start a design report with the created project in visual studio.   We’ll ignore how to configure the windows service section because you should know that already.  If you would like to know more about windows service in .net framework please visit:

1. http://www.codeproject.com/KB/system/WindowsService.aspx
2. http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx

First you need to add to a report to the project: right click on your windows service project then select the report wizard.

 

Now you have to select the report wizard; it’s the Microsoft report control for .NET Framework, and name the report “BookInforReport”. Click the add button add to your project.

When clicked the add button shows a message window like the one below.  It’s just information on what you need to do, to create a complete report.
 


Then click next button to continue the report creation process.
Now we need to select which type of data source we need to supply the report. It’s given below:

 

Next, we will select the database source.  Because our goal is creating reports with windows service we need to select a simple process data source.  You should be able to select the service or object then click next button to continue.

Now we’ve selected data source and we need to create a connection string to access the database.
 


Here, if we have the database connection we need to create a new connection with the “new connection” button. We’ll assume we already have connection with database, then click next button to continue.

Then we need to select which table or view to use to build this report with our data source.
 


Now we’ll select a table to create report and name the dataset “RealworldDataSet”. Then click finish button to finish data source creation and start the next process (when we click finish it closes and focuses on the second window which we opened in the first step).

Now we are ready to attach this data source to the report.

 

Now we’ll select which table is/are used to create the report.  We can add new data sources also by clicking “add data source” but here we don’t need add another data source because the one we already added is enough to process.Click next button to continue.

And this point we’ll ignore two steps which are related to formatting reports.   After we pass the formatting and grouping then finally you should see the
following form:

 
    
Now we’ve created the report so click to “finish” button to add to your project. When we click the finish button you should get the next image.
 


Finally we’ve designed a report and added to our project. Now let’s write code to access the database and produce the reports.  We’ll write code for windows execution and also the reports.
First we need to add a timer to the service object.


We will take records from the report occasionally, create in pdf format and save to a local directory. If you need the send email feature we would send / email the pdf report as an attachment.
First we need to enable the timer and start it, but before we start the timer we’ll also set the time interval to timer object.
Then we need write code to access the database and build reports from the records.


First we need create a localreport object to work.


As we discussed earlier this is the fundamental level database access code which is presented in  the first few lines.

Now we need the build report code which starts from here:



Next you should set the report file name as embeddedresource for our report object.
Then we need to add the report data source and add it to your local report object, then set a few attributes like following.


Next you need to render our report in PDF format and save to a local directory using the following code.


There is one more option you can add within this scenario which sends the created file by email to a user.


For emailing you need to write simple code like the above, however we won’t discuss the email sending part deeply.

For the rest of the code you can view the sample project.   So we now see how to build the service and attach within the windows service root.

For this project I would also like to suggest a few useful links.   Please read the following to get an idea of how to  can install windows service  with our .net framework and installation.

1. http://www.codeproject.com/KB/system/WindowsService.aspx
2. http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx
3. http://arcanecode.wordpress.com/2007/05/23/windows-services-in-c-adding-the-installer-part-3/

Now we’ll assume we’ve hosted everything successfully, so after 60 seconds we should get results like following.
Report results.    



Few recommendations for working with reports in .NET

1.    We  should set report name dataset_tablebame
2.    When we set the report file to to local report  we should give filename with namespace.



Conclusion:

In this article we learned how create reports with windows service and create occasional reports. As you saw, most of the operations associated with building reports wired base operations in the report creation process.

References:

1. http://www.gotreportviewer.com/
2. http://www.codeproject.com/KB/webforms/ReportViewer.aspx

Author:
My name is RRaveen, currently living and working in Singapore. Ii am highly focusing on to provide better Solutions to business problems which are commonly an organization facing, retrieve information on time using information technologies.Since i loves windows mobile technology design and implementation business applications which is based on C#.And my other specializations are Asp.net,WPF and WCF .
i have been working on many back ends which are Mysql,Sqlserver and Oracle/PL/SQL/TOAD hands on experience more than 3 and a half year.