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

Converting report object to Byte[]

6 Answers 419 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mirzodaler
Top achievements
Rank 1
Veteran
Mirzodaler asked on 17 Apr 2020, 01:40 PM

Hi,

We are starting to use the Reporting Web Designer. We need to implement custom report storage as explained here: https://docs.telerik.com/reporting/how-to-implement-a-report-definition-storage

 

This method: public byte[] GetDefinition(string definitionId) { // Retrieve the report definition bytes from the database. } needs to return byte array of the report.

I have tried converting the report object to byte[] using serialize as shown here https://stackoverflow.com/questions/4865104/convert-any-object-to-a-byte/45402065. But it says that Report object is not serializable.

System.Runtime.Serialization.SerializationException: 'Type 'Telerik.Reporting.Report' in Assembly 'Telerik.Reporting, Version=14.0.20.219, Culture=neutral, PublicKeyToken=a9d7983dfcc261be' is not marked as serializable.'

 

Is there any specific byte[] serializers available with your API? I see there is ReportXmlSerializer to convert it to xml.

 

Thanks,

6 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 22 Apr 2020, 10:38 AM

Hi Mirzodaler,

Consider the following implementation of the method GetDefinition using the ReportPackager we provide:

public byte[] GetDefinition(string definitionId)
{
    var reportInstance = new BarcodesReport();// select the report based on the received 'definitionId'

    var reportPackager = new ReportPackager();
    using (var targetStream = new MemoryStream())
    {
        reportPackager.Package(reportInstance, targetStream);
        return targetStream.ToArray();
    }
}

See also Package Report Definition article.

Regards,
Todor
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Mirzodaler
Top achievements
Rank 1
Veteran
answered on 23 Apr 2020, 06:40 AM

Hi Todor,

Thank you for the solution. I am getting following error in a web designer after implementing custom definition:

Could not open report 'TEST_PERSONLISTREPORT.TRDX'. Error: An error has occurred. Data at the root level is invalid. Line 1, position 1.

For more detailed error see an image in an attachment. 

Is FileDefinitionStorage implementation for byte[] GetDefinition  is similar as you have provided in a solution? Or can you provide with a source code of FileDefinitionStorage?

Thank you,

0
Todor
Telerik team
answered on 28 Apr 2020, 09:25 AM

Hello Mirzodaler,

Indeed, when you select a file name with extension TRDX from the list, it will be treated as such, and the Web Report Designer will attempt to utilize the byte[] as a TRDX report definition. The sample implementation returns byte[] of a TRDP report definition, hence the error. So, you need to provide the list of reports with extensions 'TRDP'.

Alternatively, you may use the ReportXmlSerializer to obtain the byte[] for the TRDX reports.

Regards,
Todor
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Mirzodaler
Top achievements
Rank 1
Veteran
answered on 30 Apr 2020, 11:29 AM

Hi Todor,

I am still having the same problem and I am aware of trdp and trdx should be loaded differently. I have following code to process public byte[] GetDefinition(string definitionId) - for trdx:

var reportPackager = new ReportPackager();
            using (var targetStream = new MemoryStream())
            {
                Report reportFile;
                using (var sourceStream = System.IO.File.OpenRead("C:\\Users\\Mirzodaler\\Desktop\\test-report.trdx"))
                {
                    ReportXmlSerializer xmlSerializer = new ReportXmlSerializer();
                    reportFile = (Telerik.Reporting.Report)xmlSerializer.Deserialize(sourceStream);
                    reportPackager.Package(reportFile, targetStream);
                    return targetStream.ToArray();
                }
            }

 

Similar for trdp:

var reportPackager = new ReportPackager();
using (var targetStream = new MemoryStream())
{
    Report reportFile;
    using (var sourceStream = System.IO.File.OpenRead("C:\\Users\\Mirzodaler\\Desktop\\ForReportStorage2.trdp"))
    {
        reportFile = (Telerik.Reporting.Report)reportPackager.Unpackage(sourceStream);
        reportPackager.Package(reportFile, targetStream);
        return targetStream.ToArray();
    }
}

 

Can you provide a full solution for this?

The error is still the same: Error: An error has occurred. Data at the root level is invalid. Line 1, position 1.     at StorageService._callee2$ 

 

Thank you,

0
Accepted
Todor
Telerik team
answered on 05 May 2020, 07:42 AM

Hi Mirzodaler,

In the code for TRDX files, you still use the ReportPackager that is suitable only for TRDP reports.

Here is sample code for creating byte[] for both TRDX and TRDP report definitions:

public byte[] GetDefinition(string definitionId)
{
    var reportInstance = new BarcodesReport();// create the necessary report instance
	
    var extension = Path.GetExtension(definitionId);
    if (extension == ".trdp")
    {
        return this.GetZipBytes(reportInstance);
    }

    return this.GetXmlBytes(reportInstance);
}

private byte[] GetXmlBytes(BarcodesReport reportInstance)
{
    using (var targetStream = new MemoryStream())
    {
        ReportXmlSerializer xmlSerializer = new ReportXmlSerializer();

        xmlSerializer.Serialize(targetStream, reportInstance);
        return targetStream.ToArray();
    }
}

private byte[] GetZipBytes(BarcodesReport reportInstance)
{
    using (var targetStream = new MemoryStream())
    {
        var reportPackager = new ReportPackager();
        reportPackager.Package(reportInstance, targetStream);
        return targetStream.ToArray();
    }
}
Regards,
Todor
Progress Telerik

 

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Tommy
Top achievements
Rank 1
Iron
Iron
Iron
answered on 16 Aug 2021, 12:41 AM
@Mirzodaler, did you get all your issues resolved? We are also implementing the web designer and have similar challenges to you...
Mirzodaler
Top achievements
Rank 1
Veteran
commented on 18 Aug 2021, 10:12 AM

@Tommy, yes, the answer provided by Todor and marked as accepted solved our issue.

Thanks,

Tags
General Discussions
Asked by
Mirzodaler
Top achievements
Rank 1
Veteran
Answers by
Todor
Telerik team
Mirzodaler
Top achievements
Rank 1
Veteran
Tommy
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or