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

Viewing Report After Created Programmatically - C# MVC

5 Answers 369 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Evan
Top achievements
Rank 1
Evan asked on 07 Oct 2014, 06:35 PM
Hello,

From the documentation, it doesn't seem hard to create a report programmatically: 

Telerik.Reporting.Report report = new Telerik.Reporting.Report();
  
            string sql = @"SELECT * FROM Recipient";
            string connectionString = "Data Source=EVAN-PC\\SQLEXPRESS;Initial Catalog=AccufloMaster;Integrated Security=True;User ID=sa;Password=creative";
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(sql, connectionString);
            report.DataSource = adapter;
            report.Name = "recipientReport";
              
            return View(report);




However, After I have the report created, how do I view it using the MVC report viewer (which I already have set up). Are there some additional properties I need to set? Ideally, I thought I could just pass the entire Report object to the view and into the viewer, but there is no overlord for that. So once I have the report created programmatically (I have to do this for reasons beyond this thread), how I can view it in the report viewer?


@model Telerik.Reporting.Report
 
 
@using Telerik.Reporting.Examples.CSharp
@{
    ViewBag.Title = "Telerik HTML5 Report Viewer MVC Demo";
}
 
@section styles
{
 
 
 
    <style>
        #reportViewer1 {
            position: absolute;
            left: 5px;
            right: 5px;
            top: 5px;
            bottom: 5px;
            overflow: hidden;
            font-family: Verdana, Arial;
        }
    </style>
 
    <link href="@Url.Content("~/ReportViewer/styles/telerikReportViewer-8.1.14.804.css")" rel="stylesheet" />
}
 
@{
    var typeReportSource = new TypeReportSource() { TypeName = typeof(ReportCatalog).AssemblyQualifiedName };
   // typeReportSource.Parameters.Add("OrderNumber", Model.SelectedInvoice);
    //ReportSource(new UriReportSource() { Uri = "Product Catalog.trdx" })
}
 
 
@(Html.TelerikReporting().ReportViewer()
        // Each report viewer must have an id - it will be used by the initialization script
        // to find the element and initialize the report viewer.
        .Id("reportViewer1")
        // The url of the service which will provide the report viewer with reports.
        // The service must be properly configured so that the report viewer can
        // successfully communicate with the server.
        // For more information on how to configure the service please check http://www.telerik.com/help/reporting/telerik-reporting-rest-conception.html.
        .ServiceUrl(Url.Content("~/api/reports/"))
        // The url for the report viewer template. The template can be edited -
        // new functionalities can be added and unneeded ones can be removed.
        // For more information please check http://www.telerik.com/help/reporting/html5-report-viewer-templates.html.
        .TemplateUrl(Url.Content("~/ReportViewer/templates/telerikReportViewerTemplate-8.1.14.804.html"))
        // Strongly typed ReportSource - TypeReportSource or UriReportSource.
        .ReportSource(Model.Name)
        // Specifies whether the viewer is in interactive or print preview mode.
        // PRINT_PREVIEW - Displays the paginated report as if it is printed on paper. Interactivity is not enabled.
        // INTERACTIVE - Displays the report in its original width and height witn no paging. Additionally interactivity is enabled.
        .ViewMode(ViewModes.PRINT_PREVIEW)
        // Sets the scale mode of the viewer.
        // Three modes exist currently:
        // FIT_PAGE - The whole report will fit on the page (will zoom in or out), regardless of its width and height.
        // FIT_PAGE_WIDTH - The report will be zoomed in or out so that the width of the screen and the width of the report match.
        // SPECIFIC - Uses the scale to zoom in and out the report.
        .ScaleMode(ScaleModes.SPECIFIC)
        // Zoom in and out the report using the scale
        // 1.0 is equal to 100%, i.e. the original size of the report
        .Scale(1.0)
        // Sets whether the viewer’s client session to be persisted between the page’s refreshes(ex. postback).
        // The session is stored in the browser’s sessionStorage and is available for the duration of the page session.
        .PersistSession(false)
        // Sets the print mode of the viewer.
        .PrintMode(PrintMode.AutoSelect)
)
 
@section scripts
{  
    <!--kendo.all.min.js can be used as well instead of kendo.web.min.js and kendo.mobile.min.js-->
    <script src="@Url.Content("~/ReportViewer/js/telerikReportViewer-8.1.14.804.js")"></script>
 
    <!--kendo.mobile.min.js - optional, if gestures/touch support is required-->
 
}

5 Answers, 1 is accepted

Sort by
0
Evan
Top achievements
Rank 1
answered on 07 Oct 2014, 06:40 PM
It seems I have carelessly left some connection string information in my post. If an admin could remove that - despite it being irrelevant, I would greatly appreciate it.
0
KAMIL
Top achievements
Rank 1
answered on 08 Oct 2014, 06:53 PM
Looks like I have simillar problem. Im deserializing report file and then I'm setting up connection string then I can't pass Report object to ReportViewer. How to do this?
0
Evan
Top achievements
Rank 1
answered on 08 Oct 2014, 07:56 PM
Kind of sucks. They make it super easy to manipulate the report viewer in web forms, but with MVC, it like you don't have access to anything. It would be nice if the ReportSource just took a report object.
0
KAMIL
Top achievements
Rank 1
answered on 09 Oct 2014, 06:30 AM
I managed this situation just by modify global datasource in web.config just before report run. However I'm afraid that it can be not safe - what in situation when few user are trying to run report and each user should use his own datasource connection string?
0
Stef
Telerik team
answered on 10 Oct 2014, 10:00 AM
Hi everyone,

You can display customized or dynamically created reports in the HTML5 Report Viewer.

The HTML5 Report Viewer is a client-side widget served by a running Reporting REST service, which is dedicated to work with the viewer. It is the service that manages the report state and resources. For more details, please check  this forum thread.

In the thread I explain how the viewer and the required REST service work. At the bottom of the post I mention a custom report resolver, which should be used in cases you need to modify reports additionally. The logic for the report generation can be executed in the custom resolver's Resolve method.


Regards,
Stef
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Evan
Top achievements
Rank 1
Answers by
Evan
Top achievements
Rank 1
KAMIL
Top achievements
Rank 1
Stef
Telerik team
Share this question
or