Using Object data source in ASP.NET 4 ReportViewer

2019-02-13 01:24发布

问题:

OK, I think I'm getting mad here... I thought this should be super simple, but I just can't figure out how to do that.

This is what I'm trying to do: I want to create an rdlc report using the ReportViewer control in ASP.NET 4 (VS 2010), and, as its datasource, use a class with some properties. I tried everything I can think of, but I just can't figure this out. All the docs I found state that the object should appear in the DataSource pane of the website, but I can't make it appear there. I would like the fields of the class to appear in the report desiger so I can use them - but I can't do that either. Using the designer, I can only define new dataset - I don't want to use dataset, but business objects!

So - how can I do that? Do I have to use some kind of DataSource control? How can I make the report designer know about the business object?

Thanks!

Memi

回答1:

did you follow this tutorial?
everything you must do is:

  • define your DTO classes or generate it using EF4 (for example)
  • define your business classes with some methods (like GetAll...)
  • build your solution (that's important)

now from your report designer you can choose methods from business classes as dataset and drag and drop field from the DTO classes
when you choose that report to display in the reportviewer, the datasource object will be added for you



回答2:

I found this blog very helpful.
When you create a new datasource for your rdlc, in the Dataset Properties dialog:
1) In the Data source drop down, select the namespace that contains the class which contains the public method (see #2).
2) In the Available datasets drop down, select the public method that returns an IQueryable of your business objects.



回答3:

Is your business object class marked as public? I've seen in a video that it must be public.



回答4:

I have the same problem and found a way around it. For some reason if you develop a ASP.NET application Microsoft took away add new datasource functionality. The way around is not great but it does work. I use all objects and I use the Enterprise library and I want to use my objects for my reports it only makes sense why they don't enable you to do this. I have no idea why Microsoft would not allow this functionality for web apps.

But that leaves windows apps it works so what I did was create a separate windows project include my objects that I want to bind to in that project and create the report on the forms project. I then bring that report into my Asp.net web app and call it through code. Here is a few pieces of code that I use to do this. This is in VB but could be converted to C#. I also have a drop down list that selects the report that is needed and a case statement that gets the data.

Private Sub LoadReport()

    Try
        pnlReport.Visible = True

        Dim Dal As New DataAccess
        Dim objRptOutputData = New Model.RptClientCollection
        Dim lr As LocalReport = OutputReportViewer.LocalReport
        Dim rds As New ReportDataSource

        lr.DataSources.Clear()

        OutputReportViewer.Visible = True
        OutputReportViewer.ProcessingMode = ProcessingMode.Local
        OutputReportViewer.LocalReport.EnableHyperlinks = True

        Dim SelectedReport As Integer = 0

        If Me.ddlReport.SelectedItem.Value IsNot "" Then
            SelectedReport = Me.ddlReport.SelectedItem.Value
        End If

  Select Case SelectedReport
      Case ConstantEnum.Reports.ActiveWaitingList

        objRptOutputData = Dal.GetRptClientsByStatus(ConstantEnum.Status.ActiveWaitingList)
        lr.ReportPath = "Reporting\Report1.rdlc"
        rds.Name = "dsClient"
        rds.Value = objRptOutputData
        Me.lblCount.Text = "Count: " & objRptOutputData.Count
      Case ConstantEnum.Reports.InactiveWaitingList
        ' This is a small app I have about 15 case statements if it was bigger I would of done this selection a bit different. 

        End Select



        lr.DataSources.Add(rds)
        lr.Refresh()
        OutputReportViewer.DataBind()

    Catch ex As Exception
        ExceptionUtility.SendError(ex, "Reports", "LoadReport")
    End Try
End Sub


回答5:

Have you seen this earlier version? Is this what you need:

http://msdn.microsoft.com/en-us/library/ms252073(v=VS.80).aspx