This is a migrated thread and some comments may be shown as answers.

Setting New ObjectDataSource for a Standalone Telerik Report

1 Answer 174 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Luis
Top achievements
Rank 1
Luis asked on 16 Jul 2015, 12:25 PM

Hi Team,

 

Good Day!

 

I am using a Standalone Telerik Report and I want to set a new ObjectDataSource from a Dataset during run-time. But even how much I tried the report cannot retrieved or used the new ObjectDataSource that I set during run-time. Below is My Code. Please kindly help assist on which part of my code do i get wrong or is this possible in Telerik Report?

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Try


            Me.Text = CurrReport.FileName
            Dim uriReportSource As New Telerik.Reporting.UriReportSource()

            uriReportSource.Uri = IO.Path.GetDirectoryName(Diagnostics.Process.GetCurrentProcess().MainModule.FileName) & "\Reports\" & CurrReport.ReportName

            ReportViewer1.Cursor = Windows.Forms.Cursors.Default
            ReportViewer1.ReportSource = SetUpReportSource(uriReportSource)
            ReportViewer1.RefreshReport()

        Catch ex As Exception
            Glenfield.Common.HandleError(ex)

        End Try

    End Sub

    Public Function SetUpReportSource(sourceReportSource As ReportSource) As ReportSource
        Try
            Dim uriReportSource = DirectCast(sourceReportSource, UriReportSource)
            Dim reportInstance = DeserializeReport(uriReportSource)
            ValidateReportSource(uriReportSource.Uri)
            Me.SetConnectionString(reportInstance)
            Return CreateInstanceReportSource(reportInstance, uriReportSource)

        Catch ex As Exception
            Glenfield.Common.HandleError(ex)

        End Try
    End Function
    Public Function DeserializeReport(uriReportSource As UriReportSource) As Report
        Dim m_Report As Telerik.Reporting.Report = New Telerik.Reporting.Report()
        Dim settings = New System.Xml.XmlReaderSettings()
        settings.IgnoreWhitespace = True
        Try
            Using xmlReader = System.Xml.XmlReader.Create(uriReportSource.Uri, settings)
                Dim xmlSerializer = New Telerik.Reporting.XmlSerialization.ReportXmlSerializer()
                Dim report = DirectCast(xmlSerializer.Deserialize(xmlReader), Telerik.Reporting.Report)
                m_Report = report
                Return m_Report
            End Using
        Catch ex As Exception
            Glenfield.Common.HandleError(ex)
        End Try
    End Function
    Public Sub ValidateReportSource(value As String)
        Try
            If value.Trim().StartsWith("=") Then
                Throw New InvalidOperationException("Expressions for ReportSource are not supported when changing the connection string dynamically")
            End If
        Catch ex As Exception
            Glenfield.Common.HandleError(ex)
        End Try
    End Sub
    Public Sub SetConnectionString(reportItemBase As ReportItemBase)
        Try
            Dim objectDataSource As New Telerik.Reporting.ObjectDataSource()
            objectDataSource.DataSource = CurrReport.m_CurrDataset
            objectDataSource.DataMember = "rptData"

            If reportItemBase.Items.Count < 1 Then
                Return
            End If

            If TypeOf reportItemBase Is Report Then
                Dim report = DirectCast(reportItemBase, Report)
                report.DataSource = objectDataSource

                'For Each item As Telerik.Reporting.ReportItemBase In reportItemBase.Items
                '    'recursively set the connection string to the items from the Items collection
                '    SetConnectionString(item)

                '    'set the drillthrough report connection strings
                '    Dim drillThroughAction = TryCast(item.Action, NavigateToReportAction)
                '    If drillThroughAction IsNot Nothing Then
                '        Dim updatedReportInstance = Me.SetUpReportSource(drillThroughAction.ReportSource)
                '        drillThroughAction.ReportSource = updatedReportInstance
                '    End If

                '    'Covers all data items(Crosstab, Table, List, Graph, Map and Chart)
                '    If TypeOf item Is DataItem Then
                '        Dim dataItem = DirectCast(item, DataItem)

                '        dataItem.DataSource = objectDataSource
                '        Continue For

                '    End If

                'Next

                For Each WkFItem As Microsoft.Reporting.WinForms.ReportParameter In CurrReport.ReportParameters

                    If report.ReportParameters.Contains(WkFItem.Name) Then
                        report.ReportParameters(WkFItem.Name).Value = WkFItem.Values(0)
                    End If

                Next

                report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom
                report.PageSettings.Margins.Left = New Unit(0.25, UnitType.Inch)
                report.PageSettings.Margins.Right = New Unit(0.25, UnitType.Inch)
                report.PageSettings.Margins.Top = New Unit(0.25, UnitType.Inch)
                report.PageSettings.Margins.Bottom = New Unit(0.25, UnitType.Inch)

                If Glenfield.Globals.PaperSize = PrintPaperSize.Letter Then
                    If CurrReport.Orientation = PrintOrientation.Portrait Then
                        report.PageSettings.PaperSize = New SizeU(New Unit(11, UnitType.Inch), New Unit(8.5, UnitType.Inch))
                    Else
                        report.PageSettings.PaperSize = New SizeU(New Unit(8.5, UnitType.Inch), New Unit(11, UnitType.Inch))
                    End If

                    report.PageSettings.PaperKind = PaperKind.Letter
                Else
                    If CurrReport.Orientation = PrintOrientation.Portrait Then
                        report.PageSettings.PaperSize = New SizeU(New Unit(11.69, UnitType.Inch), New Unit(8.25, UnitType.Inch))
                    Else
                        report.PageSettings.PaperSize = New SizeU(New Unit(8.25, UnitType.Inch), New Unit(11.69, UnitType.Inch))
                    End If

                    report.PageSettings.PaperKind = PaperKind.A4
                End If

            End If
        Catch ex As Exception
            Glenfield.Common.HandleError(ex)
        End Try
    End Sub
    Private Function CreateInstanceReportSource(report As IReportDocument, originalReportSource As ReportSource) As ReportSource
        Dim instanceReportSource = New InstanceReportSource() With { _
    .ReportDocument = report _
}
        Try
            instanceReportSource.Parameters.AddRange(originalReportSource.Parameters)
        Catch ex As Exception
            Glenfield.Common.HandleError(ex)
        End Try
        Return instanceReportSource
    End Function

1 Answer, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 20 Jul 2015, 02:29 PM
Hi Luis,

In the SetConnectionString method you are creating an ObjectDataSource object that wraps already instantiated data object. Thus you can skip the wrapping in a data source component and directly set the report's DataSource to the created data object:
report.DataSource = CurrReport.m_CurrDataset.Tables("rptData")
Note that if the report has a nested data item, you need to set its DataSource separately e.g.:
DirectCast(report.Items.Find("table1", True)(0),Telerik.Reporting.Table).DataSource = GetDataHere()


In the same SetConnectionString method you are transferring report parameters values from other non-Telerik object, which lines may introduce issues if you are adding the same parameter's key(name) twice in the CreateInstanceReportSource method.


If you need further help, please elaborate on the issue occurring on running the code.

Regards,
Stef
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Luis
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or