Abhijit Mondal Abhi
Abhijit Mondal Abhi

Reputation: 1829

Getting Error in Telerik Reporting, showing unable to get report parameters. An error has occurred

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>

Upvotes: 1

Views: 2744

Answers (1)

personaelit
personaelit

Reputation: 1653

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);

}

Upvotes: 1

Related Questions