locked
Binding an Entity Data Source to a Subform in ReportViewer RRS feed

  • Question

  • User1014298547 posted

    I'm still very new at using Reporting Services in ASP.NET.

    As near as I can tell, there seems to be no properties or events in the ReportViewer dealing with subreports. Is this correct? If so, is it true that the only way to set a datasource for a subreport in ReportViewer is by using a custom event handler in the code behind of a web form?

    I have found several examples of how to do this using DataSets, but I have not found any using the Entity Framework. Can anyone explain to me how this can be done or where I can find a tutorial about how to do it?

    Tuesday, February 5, 2013 8:20 PM

Answers

  • User1014298547 posted

    I finally managed to get this to work. He's how I did it in case anyone else has this problem. I basically used DataSets with ObjectDataSources instead of Entities with EntityDataSources. Apparently you cannot use subreports with EntityDataSources??? If someone can accomplish this with Entities, please reply and let me know how you did it!

    I also had to make some changes in the code behind that links the subreport datasource in the FormView. Orginally I was adding the SubreportProcessingHandler only when the dropdown had no value. Dumb mistake! Of course it needed to be added when a value in the dropdown was selected, not the other way around.

    Finally another problem I ran into was that the report was loading very slowly. This was because I was not filtering the subreport data with the dropdown.

    Here's the revised code:

    ASP Page:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SummeryReportOutBrief.aspx.cs" Inherits="Secured_Reports_SummeryReportOutBriefAll" %>
    <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
     
    <asp:Content ID="Main" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">
     
        <asp:DropDownList ID="DropDownListVisit" runat="server" DataTextField="VisitList" 
            DataValueField="VisitID" AutoPostBack="True"  AppendDataBoundItems="true"
            onselectedindexchanged="DropDownListVisit_SelectedIndexChanged" 
            DataSourceID="EntityDataSourceVisitDropDown">
            <asp:ListItem Text="--- Select Ship & Visit ---" Value="" Selected="True"></asp:ListItem>
        </asp:DropDownList>
     
        <asp:EntityDataSource ID="EntityDataSourceVisitDropDown" runat="server" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_VisitList" 
            Select="it.[VisitID], it.[VisitList], it.[ShipName], it.[StartDate]" 
            OrderBy="it.[ShipName] asc, it.[StartDate] desc">
        </asp:EntityDataSource>
     
        <rsweb:ReportViewer ID="ReportViewerSummeryReportOutBrief" runat="server" Font-Names="Verdana" 
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="1139px" 
            Height="894px" PageCountMode="Actual" PromptAreaCollapsed="True" 
            style="margin-right0px">
            <LocalReport ReportPath="Secured\Reports\SummeryReportOutBrief.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSourceVisitInfo" 
                        Name="VisitInfo" />
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSourceWorkOrders" 
                        Name="WorkOrders" />
                </DataSources>
            </LocalReport>
     
        </rsweb:ReportViewer>
     
        <asp:ObjectDataSource ID="ObjectDataSourceVisitInfo" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID" 
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_VisitInfoTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        <asp:ObjectDataSource ID="ObjectDataSourceWorkOrders" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID" 
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_WorkOrdersTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        <asp:ObjectDataSource ID="ObjectDataSourcePartsDataByWorkOrder" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID"           
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_PartsDataByWorkOrderTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        </asp:Content>
     
     
    

    And here's the code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Reporting.WebForms;
    using System.Configuration;
     
    public partial class Secured_Reports_SummeryReportOutBriefAll : System.Web.UI.Page
    {    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DropDownListVisit.SelectedValue == "")
            {
                this.ReportViewerSummeryReportOutBrief.Visible = false;
            }
            else
            {
                //ReportViewerSummeryReportOutBrief.LocalReport.Refresh();
                ReportViewerSummeryReportOutBrief.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
            }
        }
     
        public void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
        {
            e.DataSources.Add(new ReportDataSource("PartsDataByWorkorder"this.ObjectDataSourcePartsDataByWorkOrder));
        }
     
        protected void DropDownListVisit_SelectedIndexChanged(object sender, EventArgs e)
        {
            //In App_Code _TemplateFiles
            Reports.ShowReport(this.DropDownListVisit, this.ReportViewerSummeryReportOutBrief);
        }
    }

    I hope this helps, but I'd still like to know why I can't seem to get Entities to work with this!

    ~Sonny

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 18, 2013 2:34 PM

All replies

  • User1014298547 posted

    After finding several examples of code, I've come up with the following code. However, I am getting "Data retrieval failed for the subreport" error on my .aspx page. The main report and subreport works fine in BIDS 2008, so it has to be something wrong my .cs or .aspx pages.

    Here's the .aspx code:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SummeryReportOutBrief.aspx.cs" Inherits="Secured_Reports_SummeryReportOutBriefAll" %>
    
    
    <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
    
    
    <asp:Content ID="Main" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">
    
        <asp:DropDownList ID="DropDownListVisit" runat="server" DataTextField="VisitList" 
            DataValueField="VisitID" AutoPostBack="True"  AppendDataBoundItems="true"
            onselectedindexchanged="DropDownListVisit_SelectedIndexChanged" 
            DataSourceID="EntityDataSourceVisitDropDown">
            <asp:ListItem Text="--- Select Ship & Visit ---" Value="" Selected="True"></asp:ListItem>
        </asp:DropDownList>
    
        <asp:EntityDataSource ID="EntityDataSourceVisitDropDown" runat="server" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_VisitList" 
            Select="it.[VisitID], it.[VisitList], it.[ShipName], it.[StartDate]" 
            OrderBy="it.[ShipName] asc, it.[StartDate] desc">
        </asp:EntityDataSource>
    
        <rsweb:ReportViewer ID="ReportViewerSummeryReportOutBrief" runat="server" Font-Names="Verdana" 
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="1139px" 
            Height="894px" PageCountMode="Actual" PromptAreaCollapsed="True" 
            style="margin-right: 0px">
            <LocalReport ReportPath="Secured\Reports\SummeryReportOutBrief.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="EntityDataSourceVisitInfo" 
                        Name="VisitInfo" />
                    <rsweb:ReportDataSource DataSourceId="EntityDataSourceWorkOrders" 
                        Name="WorkOrders" />
                    <rsweb:ReportDataSource DataSourceId="EntityDataSourcePartsDataByWorkOrder" 
                        Name="PartsDataByWorkOrder" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>
    
        <asp:EntityDataSource ID="EntityDataSourcePartsDataByWorkOrder" runat="server" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_PartsDataByWorkOrder" >
        </asp:EntityDataSource>
    
        <asp:EntityDataSource ID="EntityDataSourceWorkOrders" runat="server" 
            AutoGenerateOrderByClause="True" AutoGenerateWhereClause="True" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_WorkOrders" Where="">
            <WhereParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </WhereParameters>
        </asp:EntityDataSource>
    
        <asp:EntityDataSource ID="EntityDataSourceVisitInfo" runat="server" 
            AutoGenerateOrderByClause="True" AutoGenerateWhereClause="True" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_VisitInfo" Where="">
            <WhereParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </WhereParameters>
        </asp:EntityDataSource>
    
        </asp:Content>



    Here's the .cs code behind for the same page:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Reporting.WebForms;
    using System.Configuration;
    
    public partial class Secured_Reports_SummeryReportOutBriefAll : System.Web.UI.Page
    {    
        protected void Page_Load(object sender, EventArgs e)
        {               
            if (DropDownListVisit.SelectedValue == "")
            {
                this.ReportViewerSummeryReportOutBrief.Visible = false;
    
                ReportViewerSummeryReportOutBrief.LocalReport.Refresh();             
                ReportViewerSummeryReportOutBrief.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);               
            }
        }
    
        public void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
        {
            
            e.DataSources.Add(new ReportDataSource("PartsDataByWorkOrder", this.EntityDataSourcePartsDataByWorkOrder));
        }
    
        protected void DropDownListVisit_SelectedIndexChanged(object sender, EventArgs e)
        {
            //In App_Code _TemplateFiles
            Reports.ShowReport(this.DropDownListVisit, this.ReportViewerSummeryReportOutBrief);
        }
    
    }



    Can anyone tell me what I'm doing wrong?

    Thanks!

    Monday, February 11, 2013 2:40 PM
  • User1014298547 posted

    Still no answers??? Is this problem really that difficult?? Maybe I should try using Datasets instead of Entities? If that's the case, I thought that Entities are the latest and best way to access data, why can't I use them for a ReportViewer?? Any ideas???

    Wednesday, March 6, 2013 11:04 AM
  • User1014298547 posted

    One other thing I forgot to mention: I'm using Visual Studio 2010.

    Friday, March 15, 2013 1:03 PM
  • User1014298547 posted

    I finally managed to get this to work. He's how I did it in case anyone else has this problem. I basically used DataSets with ObjectDataSources instead of Entities with EntityDataSources. Apparently you cannot use subreports with EntityDataSources??? If someone can accomplish this with Entities, please reply and let me know how you did it!

    I also had to make some changes in the code behind that links the subreport datasource in the FormView. Orginally I was adding the SubreportProcessingHandler only when the dropdown had no value. Dumb mistake! Of course it needed to be added when a value in the dropdown was selected, not the other way around.

    Finally another problem I ran into was that the report was loading very slowly. This was because I was not filtering the subreport data with the dropdown.

    Here's the revised code:

    ASP Page:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SummeryReportOutBrief.aspx.cs" Inherits="Secured_Reports_SummeryReportOutBriefAll" %>
    <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
     
    <asp:Content ID="Main" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">
     
        <asp:DropDownList ID="DropDownListVisit" runat="server" DataTextField="VisitList" 
            DataValueField="VisitID" AutoPostBack="True"  AppendDataBoundItems="true"
            onselectedindexchanged="DropDownListVisit_SelectedIndexChanged" 
            DataSourceID="EntityDataSourceVisitDropDown">
            <asp:ListItem Text="--- Select Ship & Visit ---" Value="" Selected="True"></asp:ListItem>
        </asp:DropDownList>
     
        <asp:EntityDataSource ID="EntityDataSourceVisitDropDown" runat="server" 
            ConnectionString="name=Admiral_NSSAEntities" 
            DefaultContainerName="Admiral_NSSAEntities" EnableFlattening="False" 
            EntitySetName="vw_VisitList" 
            Select="it.[VisitID], it.[VisitList], it.[ShipName], it.[StartDate]" 
            OrderBy="it.[ShipName] asc, it.[StartDate] desc">
        </asp:EntityDataSource>
     
        <rsweb:ReportViewer ID="ReportViewerSummeryReportOutBrief" runat="server" Font-Names="Verdana" 
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="1139px" 
            Height="894px" PageCountMode="Actual" PromptAreaCollapsed="True" 
            style="margin-right0px">
            <LocalReport ReportPath="Secured\Reports\SummeryReportOutBrief.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSourceVisitInfo" 
                        Name="VisitInfo" />
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSourceWorkOrders" 
                        Name="WorkOrders" />
                </DataSources>
            </LocalReport>
     
        </rsweb:ReportViewer>
     
        <asp:ObjectDataSource ID="ObjectDataSourceVisitInfo" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID" 
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_VisitInfoTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        <asp:ObjectDataSource ID="ObjectDataSourceWorkOrders" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID" 
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_WorkOrdersTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        <asp:ObjectDataSource ID="ObjectDataSourcePartsDataByWorkOrder" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByVisitID"           
            TypeName="AdmiralNSSA_DatasetTableAdapters.vw_PartsDataByWorkOrderTableAdapter">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownListVisit" DbType="Guid" 
                    Name="VisitID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:ObjectDataSource>
     
        </asp:Content>
     
     
    

    And here's the code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Reporting.WebForms;
    using System.Configuration;
     
    public partial class Secured_Reports_SummeryReportOutBriefAll : System.Web.UI.Page
    {    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DropDownListVisit.SelectedValue == "")
            {
                this.ReportViewerSummeryReportOutBrief.Visible = false;
            }
            else
            {
                //ReportViewerSummeryReportOutBrief.LocalReport.Refresh();
                ReportViewerSummeryReportOutBrief.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
            }
        }
     
        public void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
        {
            e.DataSources.Add(new ReportDataSource("PartsDataByWorkorder"this.ObjectDataSourcePartsDataByWorkOrder));
        }
     
        protected void DropDownListVisit_SelectedIndexChanged(object sender, EventArgs e)
        {
            //In App_Code _TemplateFiles
            Reports.ShowReport(this.DropDownListVisit, this.ReportViewerSummeryReportOutBrief);
        }
    }

    I hope this helps, but I'd still like to know why I can't seem to get Entities to work with this!

    ~Sonny

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 18, 2013 2:34 PM