views:

4393

answers:

5

I've seen the designer code, and I have seen code which builds the ObjectDataSource in the code-behind, however both methods communicate directly with the database via either text commands or stored procs. This seems like unnecessary code duplication to me since my data access layer has a method already which returns a datatable with the data I need for this report.

How can I programmatically build and link the ODS to my data access layer's method?

EDIT:

Thanks to everyone who answered. This was very poorly phrased on my part. There was too much that I did not understand when I wrote this question originally. What I should have asked is:

How do I programmatically bind a .Net Reporting Services Report (*.rdlc) to a method in my Data Access Layer instead of an ADO.Net DataSet.

See my answer below.

A: 

do you use object data source or datatable? make a choose from one of them that not to be duplicate

mtt
In other places in the code I databind to the datatable returned from my DAL method. In this case though I am binding to a Reporting Services report which is expecting a ReportDataSource/ObjectDataSource, not a datatable.
Rob Allen
A: 

An object datasource can bind to any object who's class implements IEnumerable. If your DAL method is returning collections or DataTable/DataView or things on the same line. You just need to assign the object datasource object's datasource property with the return val of the method and call DataBind(). You may even use the Data Server Control's DataSource property directly and do the same.

Perpetualcoder
ObjectDataSource doesn't appear to have a .DataSource property in .Net 2.0
Rob Allen
He mean's the control's DataSource property. The ODS IS the DataSource (well, technically, to use it as-intended, one would set the DataSourceID to the ID of the ODS control).
Robert C. Barth
A: 

The question doesn't make a whole lot of sense. If there's an ObjectDataSource (ODS), then that is probably being used to fill whatever control is displaying the data (mostlikely via the DataSourceID field). On the markup page, you'll see the ODS has the name of the class and the name of the method on that class to retrieve the data. That doesn't mean it's doing it twice, that's just how you set it up.

Robert C. Barth
That doesn't allow for passing arguments into the method, unless Parameter = argument and it's all worked out on the backend (which I don't think it is).
Rob Allen
Yeah it does. The markup permits parameters via the querystring and other things, in addition, you can handle the selecting event wherein you can manually set and add parameters to the ODS.
Robert C. Barth
A: 

When you say that the datasource communicates directly with the database, do you mean the SqlDataSource?

If you have a data access layer class it's rather easy to connect it to the ObjectDataSource. Just do it declaratively:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
SelectMethod="[InsertYourMethodHere]" TypeName="[InsertYourDALClassHere]">
<SelectParameters>
[Add Your Parameters Here]
</SelectParameters>
</asp:ObjectDataSource>

If you use the designer it can be smart to decorate your DAL class with the DataObject and DataObjectMethod attributes. If you'd rather do it programmatically, just use the same properties in your codebehind.

chriscena
A: 

In order to use a standard .Net DataSet as an DataSource for a Reporting Services Report I had to:

1 - Create an ADO DataSet which uses the same Stored Procedure as your DAL method

2 - Use the ADO DataSet to populate the fields in your Report in the designer

3 - Use the following

In the aspx page:

  <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" Height="655px" Width="980px">
        <ServerReport ReportServerUrl="" />
              <LocalReport>

              </LocalReport>
  </rsweb:ReportViewer>

In the code-behind

ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim report As LocalReport = ReportViewer1.LocalReport
report.ReportPath = "<your report path>"
report.DataSources.Clear()

Dim rds As New ReportDataSource()
rds.Name = "<dataset name>_<stored proc name>"
rds.Value = <your DAL method ()>

report.DataSources.Add(rds)
report.Refresh()

4 - Once you have tested this and are comfortable with the report you get, you can safely exclude the ADO DataSet from your project

Notes: This is far from ideal and there are likely steps I did which are unnecessary or something I missed. One thing that gave me a real headache was that the XML in the RDLC contained definitions for old ADO datasets that were no longer relevant. To Strip those out, right click the rdlc file in your Solution Explorer and select "Open With" then XML Editor in the next menu.

Rob Allen