1

I have completely stacked on one issue in Telerik Reporting R3 2020. I am working on HTML5 Report Viewer using Telerik Custom Resolver. Everything is going fine, successfully getting the results from DB. However, getting an error when trying to bind the data in xmlReportSource at the CustomReportSourceResolver. Following is my source codes. Hope, it will be helpful to understand. I have already go for Telerik Reporting documentations several times, but can't find any solution yet.

Error: Unable to get report parameters. An error has occurred. Data at the root level is invalid. Line 1, position 1.

enter image description here

ReportsController

public class ReportsController : ReportsControllerBase
    {

        static ReportServiceConfiguration configurationInstance;

        static ReportsController()
        {
            var resolver = new UriReportSourceResolver(HttpContext.Current.Server.MapPath("~/Reports"))
                .AddFallbackResolver(new TypeReportSourceResolver()
                    .AddFallbackResolver(new CustomReportSourceResolver()));

            configurationInstance = new ReportServiceConfiguration
            {
                HostAppId = "Application1",
                ReportSourceResolver = resolver,
                Storage = new Telerik.Reporting.Cache.File.FileStorage(),
            };
        }

        public ReportsController()
        {
            this.ReportServiceConfiguration = configurationInstance;
        }
    }

CustomReportSourceResolver

public Telerik.Reporting.ReportSource Resolve(string reportId, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
        {
            var cmdText = "SELECT Definition FROM Reports WHERE ID = @ReportId";

            var reportXml = string.Empty;
            using (var conn = new System.Data.SqlClient.SqlConnection(@"server=(local);database=REPORTS;integrated security=true;"))
            {
                var command = new System.Data.SqlClient.SqlCommand(cmdText, conn);
                command.Parameters.Add("@ReportId", System.Data.SqlDbType.Int);
                command.Parameters["@ReportId"].Value = reportId;

                try
                {
                    conn.Open();
                    reportXml = (string)command.ExecuteScalar();
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.Message);
                }
            }

            if (string.IsNullOrEmpty(reportXml))
            {
                throw new System.Exception("Unable to load a report with the specified ID: " + reportId);
            }

            return new Telerik.Reporting.XmlReportSource { Xml = reportXml };
        }

HTML5 Viwer

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Telerik HTML5 Report Viewer</title>
   
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

    <link href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.blueopal.min.css" rel="stylesheet" />

    <!--If Kendo is used it should be added before the report viewer.-->
    <script src="/api/reports/resources/js/telerikReportViewer-14.2.20.916.min.js/"></script>

    <style>
        #reportViewer1 {
            position: absolute;
            left: 5px;
            right: 5px;
            top: 5px;
            bottom: 5px;
            overflow: hidden;
            font-family: Verdana, Arial;
        }
    </style>
</head>
<body>

    <div id="reportViewer1">
        loading...
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#reportViewer1")
                .telerik_ReportViewer({
                    serviceUrl: "/api/reports",

                    reportSource: {
                        report: 1
                    },

                    viewMode: telerikReportViewer.ViewModes.INTERACTIVE,

                    scaleMode: telerikReportViewer.ScaleModes.SPECIFIC,

                    scale: 1.0,
                    enableAccessibility: true,

                    sendEmail: { enabled: false },

                    ready: function () {
                    },
                });
        });
    </script>

</body>
</html>
Abhijit Mondal Abhi
  • 1,364
  • 4
  • 15
  • 34
  • Does the report definition you're getting use any parameters? You're setting the `Xml` property in `XmlReportSource`, but you haven't added any parameters for the report to use. – ShawnOrr Oct 25 '20 at 19:52
  • No. I just need to load the TestReport.trdp to my HTML report viewer. Ops, I didn't call the TestReport.trdp from any where! Can you please tell me where should I put the TestReport.trdp name? I am following this: https://docs.telerik.com/reporting/telerik-reporting-rest-custom-report-resolver – Abhijit Mondal Abhi Oct 25 '20 at 20:08
  • Ok if you have a .trdp file you can set a path to that file in the viewer's reportSource. [Here](https://docs.telerik.com/reporting/html5-report-viewer-jquery-fn-telerik-reportviewer#examples) is an exemple from Telerik that shows how to set the path. – ShawnOrr Oct 25 '20 at 20:44
  • Ok. But, if I use reportSource: { report: "TestReport.trdp" }, HTML Viewer is loading the trdp file. But, can not call the reportResolver and that is why can not bind the data from database. reportResolver is only calling hen I write reportSource: { report: 1 }. – Abhijit Mondal Abhi Oct 25 '20 at 20:50

1 Answers1

1

Have a look at this question: xml.LoadData - Data at the root level is invalid. Line 1, position 1

In my case, there was a hidden BOM in the XML string.

Per the other answer, this bit stripped the invalid character.

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
xml = xml.Remove(0, _byteOrderMarkUtf8.Length);

}

personaelit
  • 1,633
  • 3
  • 31
  • 59