RDLC Reporting in Windows Application

Raj.Trivedi
Posted by in C# category on for Intermediate level | Points: 250 | Views : 71696 red flag
Rating: 4 out of 5  
 2 vote(s)

In this article we will be exploring the RDLC reporting feature of .NET.In VS 2010 Crystal Reports are not supported and to create Reporting Systems we can use rdlc feature.

Introduction


There are many times where we need to implement reporting functionality in the project.MS Visual Studio has given us an inbuilt tool to serve the purpose known as RDLC Reporting.

RDLC feature allows us to generate reports in very quick time. 

Objective

Create Reports in Windows Application using RDLC Feature.

Using the code

Go to MS Visual Studio 2010 -> Open a New Project -> Select Windows Application.

Now right click on the Project Name and Add A Folder and Name it as Reports.



 
Now Right Click the Folder that we have just created and Select Add New Item and Search for Report.
Select the report and Name it as SampleReport as shown in the Image.




Now Right Click on the Project Name again and Select Add New Item -> Look for Dataset. -> When Found Select it and Name it as SampleDataset.

Once done adding the dataset below screen will be visible.




 
Now Right Click on the Empty Region and Select Add New Table Adapter as shown in the Image.




Once the adapter is added configuration wizard will ask to Choose the Connection as in the Screen below.
Select New Connection and then Select Server and Database and Authentication Mode.
Authentication Mode will depend upon how you connect to SQL SERVER.
In this case, we have a Password to SQL server which will let us use SQL Server Authentication.

Please check the Images below. 




Now Click on New Connection but as seen in the Image and then Select the Server and Authentication Type and Database. Check the Below Image

Dot(.) can be for local Database in the place of SQL Server.
In this case, SQL Server has SQL Authentication and its user name is sa and password is sqluser.
Then we have selected the database as DotnetFunda.
Lastly we check the connection by clicking the Test Connection Button and it succeeded.

Check the Below Image.





After this click OK and then click next.
Note :- If your server has SQL Authentication then it will ask you that do you want to show sensitive data i.e. the password for connection to SQL, select Yes.
  
Then the wizard will ask to save the connection string in the AppConfig File. Save it and then Click Next.

After this it will ask on how we want to get the data from tables. In this there are 3 options.
  1. Use SQL Statement :- This will ask to write SQL Query to fetch data.
  2. Create a New Stored Procedure :- This will ask to create a new Stored Procedure to fetch data.
  3. Use Existing Store Procedure :- This will select if already a Stored Procedure is created in the database.
In this article, we already created the Stored Procedure to u
se existing Store Procedure.

Check the Image below



Once selected the Option Click Next.

After Clicking Next, Wizard will ask to select the Stored Procedure. Since we are fetching the data we will select the Stored Procedure from the Select Drop Down as shown in the Image.

In this case my Stored Procedure Name is getDataforMonth.






Now Click Next after selecting the Stored Procedure. After Clicking Next it will let us choose method to generate form. In this let both the option i.e. Fill a Data Table and Method Name as it is and let it be checked.

Refer the Image as shown below




Now Click Next and then it will take us to Wizard Results form, click finish.

Here is the Image for the Above Step




Now we have configured the Data set.

Now go to the Report Folder and open the Report Designer by double clicking the SampleReport that we have added in the Earlier Steps.

Now go to the Tool Box and add a Matrix Control to the Toolbox .

The Matrix control will display data that we are fetching the database.

As soon as we drag and drop the Matrix Control we will be asked ask to set the data set properties.
Give a name to data set for the Matrix Control.Be careful while giving name because this name we will use it in our code behind on the Windows form as a Reference to the dataset for matrix Control.

In this article, we used the name, rajdataset.
Then select the data source as we SampleDataset that we have configured in the earlier Steps. Once we select it the third drop down for available dataset will automatically be filled.
Now Click ok.


Check the Image.




Now start designing the matrix and assign the Properties to the textbox in the Matrix.

Right Click on the data row and select text box Property and then Add the Value as Month from the drop down of Value and on another column add it as Population for other text box in the matrix

Check the Image.


Now we will get the table as shown in the Image.







Now come to the windows form in our Application and drag and Drop a report Viewer from the toolbox under Reporting.
Now on the Right hand side there will be an arrow to select the report to be shown in the report Viewer. 

Check the Image.





Select the first report that you see i.e. Sample Report.Once you select it you will see on the bottom you will get to see the dataset you created and Table Adapters and methods.

Check the Image






Now Come to the code behind add the following namespace

It will automatically get the data from the Database.Since we do not have any parameters to Stored Procedure

using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;

// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace RDLC_Reporting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'SampleDataset.getDataforMonth' table. You can move, or remove it, as needed.
            this.getDataforMonthTableAdapter.Fill(this.SampleDataset.getDataforMonth);

            this.reportViewer1.RefreshReport();
        }
    }
}


Output after executing the Application.




Conclusion

Hope this will be a good resource. 

 
Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)