Telerik Reporting: Connecting to Stored Procedures

Saturday, March 14, 2009 by Reporting Team | Comments 21

I’ve blogged in the past about the many ways you can connect your reports to data. Because the developers who build Telerik’s Reporting Tool wanted to make it as flexible as possible, they designed it to have the ability to bind to any object that implements one of the four supported interfaces: IEnumerable, IListSource, ITypedList, and IDbDataAdapter. What does this mean for you? No matter where your data comes from – plain SQL queries, stored procedures, web services, etc – you can bind it to a Telerik report.

Okay, I realize that showing an example is much more effective than just talking about about how easy it is to bind data to your reports. So I’m going to walk you through connecting a report to the stored procedures in your database.

First, create a class library project in Visual Studio and add a Telerik Report item to your project. When you add this item, you’ll be greeted with the Telerik Report Wizard. Go ahead and cancel out of it as we’ll be designing our report manually this time.

1For this example, I’m going to connect to the SalesByCategory stored procedure in Northwind. This stored procedure will return rows with two fields each, ProductName and TotalPurchase. Go ahead and use your report items to create a simple report that binds to these two fields. Here’s what I created:

2Next, we’ll need to a way to retrieve data from the database using the stored procedure. We could write ADO.NET code, but I prefer to take the easier route and create a DataSet item to my report. To do so, right-click on your project and select Add | New Item… From the Add New Item dialog select a DataSet item, name it SalesDataSet.xsd and click Add.

3

When the new DataSet is generated, you’ll be greeted with a blank design surface. Drag a TableAdapter onto the design surface from your Visual Studio Toolbox. This will launch the TableAdapter Configuration Wizard. On the first page of the wizard, select the connection string you want to use to connect to the Northwind database. If one is not already set up and available in the drop down list, click New Connection… to create a new one.

4Click Next. On the following page, select the Use existing stored procedures radio button to choose your command type.

5Click Next. You’ll be given the option to choose four different stored procedures for CRUD operations. From the Select drop down list, choose the SalesByCategory stored procedure.

6Click Next. One the following page you can choose your own method names for methods that will be generated to fill a DataTable or return a DataTable. For the Return a DataTable method name, enter GetSalesByCategory.

7Click Finish. Now that we’ve set up a DataSet with which we can call our stored procedure, it’s time to set up our report to bind to the data from the DataSet. Since the stored procedure requires two parameters, we need to add a way to dynamically request these values from the user of the report. With Telerik Reporting we can use Report Parameters to satisfy this requirement.

To add two new report parameters to the report, go to your report properties in the Visual Studio Properties window and select the ellipses next to the Report Parameters property.

8You will be greeted with the ReportParameter Collection Editor. Add two new parameters, with a Name and Text of Category and Year, each of type String. Set the AllowBlank property to False and the Visible property to True. Click OK.

9Now that we have two parameters that we can pass to our stored procedure, the last thing we must do is call the stored procedure and bind the results to the report. We can do this by taking advantage of the report’s NeedDataSource event. To add an event handler to this event, go the to report’s events in the Visual Studio Properties window and double-click on the NeedDataSource event. This will add the event handler automatically to your code-behind.

10In the newly-generated event handler, add the following code to retrieve the values from our report parameters, pass them to the stored procedure and bind the results to the report.

private void AnnualSalesReport_NeedDataSource(object sender, EventArgs e)
{
    string category = (string)this.ReportParameters["Category"].Value;
    string year = (string)this.ReportParameters["Year"].Value;
    SalesTableAdapter adapter = new SalesTableAdapter();
    SalesDataSet.SalesDataTable data = adapter.GetSalesByCategory(category, year);
    ((Telerik.Reporting.Processing.Report)sender).DataSource = data.AsDataView();
}

Now you can preview the results in the report designer by clicking on the Preview tab.11

Conclusion

Binding to any data source is easy with Telerik Reporting if you remember one thing: the objects that are returned from your data source must match one of the supported interfaces.

If you’d like to see more examples of using stored procedures with Telerik reports, check out this Knowledge Base article on the topic.

[Source: C#]

21 Comments

  • Mike 16 Mar 2009
    Any idea when you will be able to have multiple datasources in a report, like you can with MS Reporting Services?
  • Aniket 17 Mar 2009
    @Mike

    I think that's the point of a subreport. Setup your subreport with the information you need. remove all headers and footers and then set its datasource  programmatically as needed.
  • Mike 18 Mar 2009
    I have not actually used Telerik Reporting yet, but I figured you could probably do that.  I guess I'm just used to Rpt. Svcs.  You have tables and lists, so it would seem like you should be able to point them to different sets of data.  For a complex report, you might end up with five reports that comprise it all.  One "main" report, and five other reports that have to get loaded and run in order to be used as subreports.  I'd rather be able to have it all packaged up inside a single report instead.

  • Frank 18 Mar 2009
    I agree with Mike, but also there is something good on subreports.
    For example: I have many reports with "contact information". I have one subreport for this and use is on many reports ...
  • Actually, with the release of Telerik Reporting 2009 Q1 you have three new items -- List, Table, and Crosstab. So you can have multiple data sources in one report just as you can is SSRS. You can give it a try.
  • Mike 30 Mar 2009
    Thanks for the response.  That's what I was waiting for, so I will definitely take another look at Telerik Reporting now.
  • vigneshbabu 30 Mar 2009
    im watched ur reporting video its very useful for me . i have one doubt how to connect report  like windows form to telerik reports is there any example , send to my mail id sir, im waiting for ur reply .


  • NSW 05 Apr 2009
    Trying to follow this and all seems to be going well until the very end with "data.AsDataView();" what name space is this coming from? Only info i can find on it is that it is Iinq but even after adding that in does not seem to work
  • AmyK 21 Apr 2009
    Any chance you could post the last code example as VB instead of C#?  I tried to use an online converter but that doesn't seem to work - when I try to preview it's giving me the error "An error has occured while processing Report '': Object reference not set to an instance of an object.   Thanks in advance if you can.
  • Tom 23 Jun 2009
    It would also be very helpful for me if the NeedDataSource event handler could be translated to VB.

    Thank you
  • Simon Murrell 19 Aug 2009
    data.AsDataView() is from the
    System.Data.DataSet.Extensions namespace.

    Also remember to set this.DataSource = null; in the
    report constructor otherwise the NeedDataSource
    doesn't seem to fire.


  • maurizio 06 Dec 2009
    All nice BUT how to do it  when  Category and Year are two session variable coming from a aspx page ?
    IN the needDataSouce event can i write string category = session(category) ?  i guess...no.
    For example when my users log in web app, i know his user_id and  i need to pass that value to query or stored procedure. Now how can built the report ?
  • tiger 12 Dec 2009
    I'm yet to use the reports in a application yet but i'm currently going through training.

    I'm planning to do this in a web application so sessions are the way I will mostly be passing parameters.

    if this is anything like crystal reports then i'm guessing its gonna be pretty simple.

    Im gonna be testing this tomorrow so most likely ill have an example for you maurizo
  • abhi 15 Dec 2009
    Hi guys .. i am new to this forum. i am a sql developer working on ssrs. i am fresher. i was given a task to convert telerik reports to ssrs. any body know s how we can do it or can any one suggest me some important steps or precautions taken while converting. thanks

  • Gerry 12 Jan 2010
    Hi
    I have been trying to do this all day with one of my stored procedures.  But I keep getting the below erro

    'ClassLibrary3.Report1' does not contain a definition for 'sp_dataTableAdapters' and no extension method 'sp_dataTableAdapters' accepting a first argument of type 'ClassLibrary3.Report1' could be found (are you missing a using directive or an assembly reference?)


    Can somebody please help me
                

  • John 18 Jan 2010
    To get .AsDataView() reference to workk add a reference to System.Data.DataSetExtensions then add
    using System.Data and using System.Data.DataSetExtensions to the class.
  • John 18 Jan 2010
    Main issue I was by adding a DataSet to the report, a TableAdapter was not getting added to the report.  Only way around this I could find was to use the report wizard to create a new dataset, which does not prompt for stored procedures.  Once this is complete, a TableAdapter is automatically placed on the report.  At this point I manually modify the Dataset/TableAdapter to reference a Stored Procedure. 
  • Mike 25 Jan 2010
    .AsDataView() is not available in .Net 2.0 any idea how to get this example work in 2.0?
  • ALI 15 Mar 2010
    Can I use stored procedure with mutiple result set in telerik reproting???

    First result set will be company name and address that goes in report header section, second result set will be for detailed rows of the report.

    Is it possbile to use single stored proc returing 2 result sets in telerik reporting???

    Any examples please.
  • Smith 24 Aug 2010
    You are returning a string here:
    string category = (string)this.ReportParameters["Category"].Value;
    because the type you have taken is string and you pass in here:
        SalesDataSet.SalesDataTable data = adapter.GetSalesByCategory(category, year);
    How can i handle this if my StoreProcedure is returning a integer.
    I tried to do:
    int category = (int)this.ReportParameters["ID"].Value;
    and then I did Convert.ToString(category)
    But this doesn't work.
    

  • andy 16 Sep 2010
    Hello Guys, How do a call a Telerik Report from C# page, best using just call report name if anyone knows, by the way, the walk through upstairs is great

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)