Creating a Visual Studio 2010 ASP.Net Reports Web Site with a LINQ DBML data source

by Heathesh 8. June 2010 02:48

With Visual Studio 2008, it appears the only way to connect to a LINQ DBML and use it as a data source was to create your own Data Processing Extensions implementation. With Visual Studio 2010 it's a little bit simpler, but can still cause some pain.

To begin with, create a ASP.Net Reporting Web Site. I first tried this by creating a web application and after much hair pulling decided to use the preconfigured Reporting website project type to save myself the configuration issues.

Once you've creating the website, the Data Source configuration wizard should start automatically. I simply cancelled this. As part of the website you'll see that it adds a "Report1.rdlc" file, I deleted this and added a new report, and called it "PeopleDetails".

If you do delete the "Report1.rdlc", be sure to open the HTML of the Default.aspx and change the report name accordingly:

        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt">
            <LocalReport ReportPath="PeopleDetails.rdlc">

Next add a new "Class Library" project to your Solution. This class library project will be used to store your LINQ DBML in, and it needs to be separate in order for you to make it accessible on the machine so the report can read it.

You can delete the sample "Class1.cs" file created by the IDE, and add a new "LINQ To SQL Classes" item. Next add the relevant tables etc. to the DBML as required. Then add the newly created project as a reference to your website. Make sure to add the connection string as required to the Web.Config of your website.

        <add name="PeopleDbml.Properties.Settings.SampleConnectionString"
            connectionString="Data Source=.\server;Initial Catalog=Sample;Integrated Security=True"
            providerName="System.Data.SqlClient" />

Next add a class to your "App_Code" folder, I called mine DataReader simply because it seemed to be the most relevant name. In your DataReader class, add a method to retrieve a List of the data you wish to diplay on your report. In my case I wanted a list of People from the Person table in the database. So I implemented a method called GetPeople which accepts the data context as a parameter:

    /// <summary>
    /// Gets a list of people from the database
    /// </summary>
    /// <returns></returns>
    public List<Person> GetPeople(PeopleDataClassesDataContext dx)
        return (from people in dx.Persons
                select people).ToList();

Now comes the weird part. For some reason the report will not be able to find the PeopleDbml dll. If you tried to add a table for example, and tried to add a data source to that table you will get an error that it can't find the dll. To solve this problem, compile your PeopleDbml class library, find the dll and copy it to the "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies" folder.

Once you've done that, close Visual Studio 2010 (because I haven't found another way of doing this), reopen it and reopen your solution. Then try and add a table to your report and it should work now without any errors. Simply set the name of the DataSet as required, I called mine PeopleDataSet, set the DataSource to "global" and you should be able to select "DataReader (GetPeople)" in the "Available DataSets" drop down.

Once the DataSet has been added, design your table (i.e. setup your fields as required, I simply clicked and dragged three different fields into the three columns available).

The last thing you need to do is set the ReportDataSource for the report. I did this by adding the following code to my Default.aspx page:

using Microsoft.Reporting.WebForms; //using for the ReportDataSource
using PeopleDbml;

public partial class _Default : System.Web.UI.Page
    /// <summary>
    /// Gets the connection string from the web.config
    /// </summary>
    private string _connectionString
        get { return ConfigurationManager.ConnectionStrings["PeopleDbml.Properties.Settings.SampleConnectionString"].ConnectionString; }

    /// <summary>
    /// Gets or sets the data context
    /// </summary>
    private PeopleDataClassesDataContext _dataContext

    /// <summary>
    /// Handles the page load event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            if (_dataContext == null)
                _dataContext = new PeopleDataClassesDataContext(_connectionString);

            //create an instance of my data reader class
            DataReader dataReader = new DataReader();
            //create the report data source, specifying that it's for the PeopleDataSet
            ReportDataSource reportDataSource = new ReportDataSource("PeopleDataSet", dataReader.GetPeople(_dataContext));
            //add the data source to the report viewer's local report data sources

    /// <summary>
    /// Handle the dispose to clean up the data context, which overriden to clean up the data context
    /// </summary>
    public override void Dispose()

        if (_dataContext != null)

That was it. Happy Reporting!


Tags: , , , , ,

Development | .Net | Visual Studio 2010 | VS2010 | LINQ

ASP.Net 4 Web.Config Changes

by Heathesh 5. May 2010 06:23

UPDATE: Before using web.config transforms as indicated in point 2 below, please view this known issue: Web.config transform writes extra line break/spaces to values elements under applicationSettings section

With the release of .Net 4 and ASP.Net 4.0 there have been two major changes to the web.config that I really like. I personally think these changes address two very useful features that all developers should know about:

1. The file has been refactored

The web.config has now been refactored, and with ASP.Net 4 a lot of the settings that were previously found in the web.config file have now been moved to the machine.config file. This significantly reduces the size of the file, which I think is a great bonus.

With this change, your web.config file can be EMPTY or alternatively simply contain just the following section:

    <?xml version="1.0"?>
                <compilation targetFramework="4.0" />

2. Web.config transformations

Web.config transformations cater for moving your application between your relevant environments (e.g. Development, QA, Production). The transformations work on the relevant configurations you setup.

To create your own Configuration build with configuration transformations, create a new ASP.Net Web Application in Visual Studio 2010. Next, in the menu select "Build" and then "Configuration Manager". In the "Active Solution Configuration" drop down, select "New". Name the relevant configuration, for example I'm calling mine "Development" and copying the settings from the "Debug" configuration.

Make sure "Create new project configurations" is selected. Once you click okay, you will see your Web.config file now has a "+" next to it in your solution explorer.

If you don't see the "+", build you solution, right click the web.config file and select "Add Config Transformations".

You will see for each of your build configurations there will be a "Web.[Build Configuration Name].config" file. If you open any of these files, you will see place holders for different sections of your original web.config file.

To change settings per your relevant build configuration, check out the following MSDN Article: Web.config Transformation Syntax for Web Application Project Deployment

Happy deploying!

Tags: , , , , ,

Development | .Net | Visual Studio 2010 | VS2010

Powered by BlogEngine.NET (with enhancements by Heathesh)
Theme by Mads Kristensen (with tweeks by Heathesh)


Microsoft Certified Professional

Microsoft Certified Technology Specialist

Answer Questions


Tag cloud


<<  May 2017  >>

View posts in large calendar