ReportViewer data binding (NOT design time) in ASP.Net, please help

I am trying to bind a report to a datatable (or dataset) in asp.net. The only way I can see is via an ObjectDataSource (unlike windows where I can bind direct to the datatable). Is this the case, is it not possible to bind direct to the table or dataset

Assuming we have to go through an ObjectDataSource, I have succeeded in building a working example using design time binding of the report to the ObjectDataSource, and adding a handler to the ObjectCreating event to put my datatable into its object data source property (see snippet1). However if I try to create the ObjectDataSource programatically (see snippet2) then the report doesnt get any data, showing an error message "A data source instance has not been supplied for the datasource dataset_tablename". How can I fix this, we need to create these objects at runtime as we dont know the number of data sources a report needs until then (different reports have different numbers of subreports).

Please help. Thanks


snippet1
AddHandler ObjectDataSource1.ObjectCreating, AddressOf bob

Private Sub bob(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)

e.ObjectInstance = dt

End Sub

snippet2

ObjectDataSource1 = New ObjectDataSource

AddHandler ObjectDataSource1.ObjectCreating, AddressOf bob

ObjectDataSource1.ID = "ObjectDataSource1"

ObjectDataSource1.SelectMethod = "Select"

ObjectDataSource1.TypeName = "dstMonthlyReturn+titleDataTable"

ObjectDataSource1.EnableViewState = True



Answer this question

ReportViewer data binding (NOT design time) in ASP.Net, please help

  • Mike Jocher

    Have you considered building your own data object I imagine you would either have to build a 'generic' strongly typed dataset, which could be ugly (if you push datasets too far), or a data object of your own that can be bound to the objectdatasource. This object can then be as flexible as you need.
  • Robert Mulroney

    In Beta 2 the ASP.NET ReportViewer control does not support binding directly to DataTables. This limitation has been removed in the final version.

    The fix is already available in the June CTP release. (Note: The June CTP is not recommended for designing reports because several dialogs in Report Designer are broken.)


  • dougal83

    Yes, you will be able to do this:

    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));

  • spoox2

    OKEJ, Thanks a lot

    I will try this.

    The problem I have is that I do everything dynamic, so I dont know what kind of DataSet I will get.
    Maybee you can build a class that creates a Typed DataSet anyway from my not Typed DataSet :)

    I try it your way first and I get back to you.

    Thanks
    /Anders





  • Nixies

    Hey this sounds like great news are you going to be able to do this then

    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));

    please say that you are....



  • RichardB2

    Hello
    Have you figure it out because I am in a simular situation and I need help

    /Anders

  • ptech

    Anders - I have a partial answer.

    Create your strongly typed datatable
            (class level) Private dt As New dstMonthlyReturn.titleDataTable
            dt.AddtitleRow("hoho")
            dt.AddtitleRow("boohoo")
            dt.AcceptChanges()

    Create a binding object at asp page class level
          Protected WithEvents ObjectDataSource1 As ObjectDataSource

    in page load set the binding object up
            ObjectDataSource2 = New ObjectDataSource
            Me.form1.Controls.Add(ObjectDataSource2)
            AddHandler ObjectDataSource2.ObjectCreating, AddressOf bob
            ObjectDataSource2.ID = "ObjectDataSource2"
            ObjectDataSource2.SelectMethod = "Select"
            ObjectDataSource2.TypeName = "dstMonthlyReturn+titleDataTable"
            ObjectDataSource2.EnableViewState = True
            ObjectDataSource2.Select()
            ObjectDataSource2.DataBind()
    (note the TypeName should be the dataset_definition_name+tablenameDataTable where your objects are in italics)

    set up the report viewer
               ReportViewer1.ProcessingMode = ProcessingMode.Local
                ReportViewer1.LocalReport.ReportPath = "reps\Report.rdlc"
                Dim params(0) As ReportParameter
                params(0) = New ReportParameter("HighlightRow", "2")
                ReportViewer1.LocalReport.SetParameters(params)
               ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("dstMonthlyReturn_title", ObjectDataSource1.ID))

    'refresh the report
    ReportViewer1.LocalReport.Refresh()

    'Add an event handler on ObjectDataSource
    to catch when the ObjectDataSource creates its binding object (your datatable) so you can have access to it and replace it with your populated datatable.
        Private Sub bob(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)
            e.ObjectInstance = dt
        End Sub

    Note that some of the above lines of code may be redundant, i havent experimented taking a few out yet, especially the Databind calls. However the code above works.

    Note also that this code works for the main report but not for subreports, i have a feeling there is a bug in .Net.

    I cant help but think life would be much much simpler if we could bind directly to a dataset or table as we can in windows, but it seems not to be possible. The binding object examples I have found tend to rely on static data population methods being built in to the object you are binding to, i.e. datasets would have built in tableadapters with built in SQL!!!!, which makes N tier design an absolute pain in the *** as you can see, with event handlers needed on the ObjectDataSource.

    Allen

     


  • David A.

    Hmm I thougth this should be easy. Everything works in Windows.

    My dataset is like this

    DocumentID
    CreatedDate
    fCol1 -- this can change all the time
    fCol2
    fCol3

    I have a DocumentObejct

    DocumentID
    CreatedDate
    Fields --- that is a HashTable (if i write Fields["fCol1"].Value I get the same value as in fCol1 in dataset.)

    Do you think I can bind a Collection of my DocumentObject to ObjectBindingSource, or am I way out of it

    /Anders



  • ventura

    I am trying same thing.

    The problem with below solution is :-

    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));

    I have a reportviewer with a local datasource(.rdlc) which has many textboxes i need to bind with the datatable i am generating dynamically. I could not figure out a way to bind all textboxex with my datatable using above solution.

    Another way i am trying to bind a datatable to my reportviewer dynamically is by taking a new dataset(.xsd) at design time and creating a exact datatable with same schema as my dynamically generated datatable. Then when i run my application at run time i take the entire row(single) and add it to this design time datatable.

    But through this way i face a problem as my dataset is assigned to reportviewer before it gets value from my dynamic datatable.

    If anyone could help me out on this problem.


  • kenichi yamazaki

    Wonderful news. Thanks Rajeev.
  • ReportViewer data binding (NOT design time) in ASP.Net, please help