Creating an ASP.NET report using Visual Studio 2010 - Part 1 - Raj Kaimal

Creating an ASP.NET report using Visual Studio 2010 - Part 1

This tutorial walks you through creating an ASP.NET report based on the Northwind sample database. It shows how to add a client report definition file (RDLC), create a dataset for the RDLC, define queries using LINQ to Entities, design the report and add a ReportViewer web control to render the report in a ASP.NET web page. The report will have a chart control. The result can be filtered by two drop downs at the top..

At the end of the walkthrough, you should have a UI like the following.  As shown, there is a product list report with an embedded chart. The chart shows the sum of Unit price for a given category. It is possible to filter the results by Category and Supplier. The drop downs auto post back when the selection is changed.  This demo uses Visual Studio 2010 RTM.

This post is split into three parts. The last part has the sample code attached.

Creating an ASP.NET report using Visual Studio 2010 - Part 2
Creating an ASP.NET report using Visual Studio 2010 - Part 3

image 

Lets start by creating a new ASP.NET empty web application called “NorthwindReports”
image
Creating the Data Access Layer (DAL)

Add a web form called index.aspx to the root directory. You do this by right clicking on the NorthwindReports web project and selecting “Add item..”
.
Create a folder called “DAL”. We will store all our data access methods and any data transfer objects in here.

 image

Right click on the DAL folder and add a ADO.NET Entity data model called Northwind.

image

Select “Generate from database” and click Next.

image
Create a connection to your database containing the Northwind sample database and click Next.

image 
From the table list, select Categories, Products and Suppliers and click next.

image
Our Entity data model gets created and looks like this:

image  
Adding data transfer objects

Right click on the DAL folder and add a ProductViewModel. Add the following code. This class contains properties we need to render our report.

public class ProductViewModel
{
    public int? ProductID { get; set; }
    public string ProductName { get; set; }
    public System.Nullable<decimal> UnitPrice { get; set; }
    public string CategoryName { get; set; }
    public int? CategoryID { get; set; }
    public int? SupplierID { get; set; }
    public bool Discontinued { get; set; }
}


Add a SupplierViewModel class. This will be used to render the supplier DropDownlist.
public class SupplierViewModel
{
    public string CompanyName { get; set; }
    public int SupplierID { get; set; }
}

Add a CategoryViewModel class.
public class CategoryViewModel
{
    public string CategoryName { get; set; }
    public int CategoryID { get; set; }
}

Create an IProductRepository interface. This will contain the signatures of all the methods we need when accessing the entity model.  This step is not needed but follows the repository pattern.

interface IProductRepository
    {
        IQueryable<Product> GetProducts();
        IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID);
        IQueryable<SupplierViewModel> GetSuppliers();
        IQueryable<CategoryViewModel> GetCategories();
    }

Create a ProductRepository class that implements the IProductReposity above. The methods available in this class are as follows:

  • GetProducts – returns an IQueryable of all products.
  • GetProductsProjected – returns an IQueryable of ProductViewModel. The method filters all the products based on SupplierId and CategoryId if any. It then projects the result into the ProductViewModel.
  • GetSuppliers() – returns an IQueryable of all suppliers projected into a SupplierViewModel
  • GetCategories() – returns an IQueryable of all categories projected into a CategoryViewModel 
public class ProductRepository : IProductRepository
{
    /// <summary>
    /// IQueryable of all Products
    /// </summary>
    /// <returns></returns>
    public IQueryable<Product> GetProducts()
    {
        var dataContext = new NorthwindEntities();
        var products = from p in dataContext.Products
                       select p;
        return products;
    }
 
    /// <summary>
    /// IQueryable of Projects projected 
    /// into the ProductViewModel class
    /// </summary>
    /// <returns></returns>
    public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID)
    {
        var projectedProducts = from p in GetProducts()
                                select new ProductViewModel
                                {
                                    ProductID = p.ProductID,
                                    ProductName = p.ProductName,
                                    UnitPrice = p.UnitPrice,
                                    CategoryName = p.Category.CategoryName,
                                    CategoryID = p.CategoryID,
                                    SupplierID = p.SupplierID,
                                    Discontinued = p.Discontinued
                                };
        // Filter on SupplierID 
        if (supplierID.HasValue)
        {
            projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID);
        }
 
        // Filter on CategoryID 
        if (categoryID.HasValue)
        {
            projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID);
        }
 
        return projectedProducts;
    }
 
 
    public IQueryable<SupplierViewModel> GetSuppliers()
    {
        var dataContext = new NorthwindEntities();
        var suppliers = from s in dataContext.Suppliers
                        select new SupplierViewModel
                        {
                            SupplierID = s.SupplierID,
                            CompanyName = s.CompanyName
                        };
        return suppliers;
    }
 
    public IQueryable<CategoryViewModel> GetCategories()
    {
        var dataContext = new NorthwindEntities();
        var categories = from c in dataContext.Categories
                         select new CategoryViewModel
                         {
                             CategoryID = c.CategoryID,
                             CategoryName = c.CategoryName
                         };
        return categories;
    }
}


Your solution explorer should look like the following.
image

Build your project and make sure you don’t get any errors.

In the next part, we will see how to create the client report definition file using the Report Wizard.

 

Creating an ASP.NET report using Visual Studio 2010 - Part 2

 

 

Other Posts

Published Sunday, May 09, 2010 11:13 PM by rajbk

Comments

# Creating an ASP.NET report using Visual Studio 2010 - Part 3

We continue building our report in this three part series. Creating an ASP.NET report using Visual Studio

Monday, May 10, 2010 12:20 AM by Raj Kaimal

# re: Creating an ASP.NET report using Visual Studio 2010 - Part 1

Nice article.

Monday, May 10, 2010 12:54 AM by habdulrauf

# Twitter Trackbacks for Creating an ASP.NET report using Visual Studio 2010 - Part 1 - Raj Kaimal [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Creating an ASP.NET report using Visual Studio 2010 - Part 1 - Raj Kaimal         [asp.net]        on Topsy.com

# Daily tech links for .net and related technologies - May 10-12, 2010

Daily tech links for .net and related technologies - May 10-12, 2010 Web Development jQuery Templates

Monday, May 10, 2010 6:18 AM by Sanjeev Agarwal

# Daily tech links for .net and related technologies &#8211; May 10-12, 2010 | OOP - Object Oriented Programing

Pingback from  Daily tech links for .net and related technologies &#8211; May 10-12, 2010 | OOP - Object Oriented Programing

# How to render client report definition files (.rdlc) directly to the Response stream without preview - Raj Kaimal

Pingback from  How to render client report definition files (.rdlc) directly to the Response stream without preview - Raj Kaimal

# Rendering an RDLC directly to the Response stream in ASP.NET MVC | OOP - Object Oriented Programing

Pingback from  Rendering an RDLC directly to the Response stream in ASP.NET MVC | OOP - Object Oriented Programing

# Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 2 | OOP - Object Oriented Programing

Pingback from  Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 2 | OOP - Object Oriented Programing

# ASP.NET MVC Paging/Sorting/Filtering using the MVCContrib Grid and Pager

This post walks you through creating a UI for paging, sorting and filtering a list of data items. It

Tuesday, May 18, 2010 8:23 PM by Raj Kaimal

# CascadingDropDown jQuery Plugin for ASP.NET MVC

CascadingDropDown is a jQuery plug-in that can be used by a select list to get automatic population using

Friday, May 21, 2010 8:24 AM by Raj Kaimal

# Adding an expression based image in a client report definition file (RDLC)

In previous posts, I showed you how to create a report using Visual Studio 2010 and how to add a hyperlink

Saturday, May 22, 2010 3:03 AM by Raj Kaimal

# Adding an expression based image in a client report definition file (RDLC) | OOP - Object Oriented Programing

Pingback from  Adding an expression based image in a client report definition file (RDLC) | OOP - Object Oriented Programing

# Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 3 &laquo; DANRESA Consultoria de Inform??tica

Pingback from  Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 3 &laquo; DANRESA Consultoria de Inform??tica

# Useful Charting Tools | NineSYS

Pingback from  Useful Charting Tools | NineSYS

Saturday, September 04, 2010 3:21 PM by Useful Charting Tools | NineSYS

# Creating an ASP.NET report using Visual Studio 2010 &laquo; Technology Dynamics

Pingback from  Creating an ASP.NET report using Visual Studio 2010 &laquo; Technology Dynamics

# Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 3 &laquo; C# .net &laquo; Object Oriented Programing &laquo; Object Oriented Programing

Pingback from  Creating an ASP.NET report using Visual Studio 2010 &#8211; Part 3 &laquo; C# .net &laquo; Object Oriented Programing &laquo; Object Oriented Programing

# Adding a hyperlink in a client report definition file (RDLC) | Object Oriented Programing

Pingback from  Adding a hyperlink in a client report definition file (RDLC) | Object Oriented Programing

# Rendering an RDLC directly to the Response stream in ASP.NET MVC | Object Oriented Programing

Pingback from  Rendering an RDLC directly to the Response stream in ASP.NET MVC | Object Oriented Programing

# ReportViewer - Add a dropdown or checkbox to the content

Pingback from  ReportViewer - Add a dropdown or checkbox to the content

# Miszka blog &raquo; Blog Archive &raquo; Creating an ASP.NET report using Visual Studio 2010

Pingback from  Miszka blog  &raquo; Blog Archive   &raquo; Creating an ASP.NET report using Visual Studio 2010

# How to create reports (rdlc) and load inside report viewer? | Arnold Sia&#039;s space

Pingback from  How to create reports (rdlc) and load inside report viewer? | Arnold Sia&#039;s space

Leave a Comment

(required) 
(required) 
(optional)
(required)