Geeks With Blogs
Evil Dev's Blog Plots, Schemes, and Plans to take over the Devleoper world.... Mu hahahaha
Well, today I spent most of the day, trying to find an alternative way to render an SSRS Report to the page, other than the report viewer control. Based on a comment on my last post on this, I decieded to dig into the ServerReport.Render function.

Well, this sounded/looked promising, and I was quickly able to write a neat little wrapper object, exposing the various formats as properties. I then proceeded to create a special IRequestManager for reports, and some other stuff to nicely integrate it into our existing infrastructure.

I wrote a couple unit tests to test the wrapper, and after a couple minor bug fixes I was ready to rock. I threw together a quick and dirty XSL page, and decieded to go to town and see if I could get a report to render. Well, turns out, there's a little known hitch to this method that isn't easy to find on forums and blogs. It works just beautifully for the PDF/Excel/MHTML output formats, once you have the right headers in place, and Response.BinaryWrite(Report.PDF) etc to the page, it prompts the user to download the pdf, open it and looks great.

But, again, where the kick you when your down, is the HTML rendering. If you try the ServerReport.Render("HTML4.0") method, it fails to render any of the images in the report, and none of the Drill downs/hyperlinks work. Well, I decieded to dig into it, and figure it out. After a couple painstacking trial and error runs, and googlling, still no luck.

I eventually stumbled onto a blog where someone referenced that it wasn't possible to render the images with the server.render, you needed to use server.renderstream functionality. After some more frustrating trial and error and googlling, I finally figured out the issue. Turns out, SSRS renders images in different streams than the html, but that Server.Render returns the Stream ID's of the additional streams, and with a little bit of TLC, you can write those images to files. Tweak the DeviceInfo XML to change the StreamBase to the base path of where your dumping the images, and it works.

CODE EXAMPLE (And yes, it's in VB.):
Dim rv As New Reportviewer
Dim DeviceInfo As String = "<DeviceInfo><StreamBase>PathToMyPage/Images/</StreamBase></DeviceInfo>
Dim StreamIDs() As String
Dim HTMLBytes() As Byte

With rv.ServerReport
.ReportServerURL = "http://myserver/reportserver"
.ReportPath = "/MyPath/MyReportName"
HTMLBytes = .Render("HTML4.0", DeviceInfo, Nothing, Nothing, ".HTML", StreamIDs, Nothing)
For Each streamID as string In StreamIDs
Dim ImageBytes() As Byte = .RenderStream("HTML4.0", streamID, DeviceInfo, nothing, nothing..)
Dim fs As FileStream = File.Create("HardDiskPath to Images Directory used in DeviceInfo/" & streamID, ImageBytes.Length)
fs.Write(imageBytes, 0, ImageBytes.Length)
fs.close()
Next

Response.ContentType = "text/HTML"
Response.Expires = -1440
Response.Write(System.Text.UTF8Encoding.GetString(HTMLBytes)
Response.End()
End With

NOTE: The above Example is strictly from memory I don't have code in front of me, so I may not have called exactly the right functions, but should give you a good idea what I'm getting at.

This still doesn't fix the issue with the drill downs not working, and for now, I don't think we are going to go this route, we'll stick to having the page post back and using the report viewer control. I eventually may dig into this again, as it bugs me. I also don't feel it's a good Idea to be dynamically writing images to the fillesystem on each report request, and the above code is not designed with concurrency in mind, but I figured I had so damn much trouble getting this kind of working, that I'd help the community out and post my findings. Also, it's better to add a web reference to the Reporting Services Web Service for it's Render and RenderStream Methods, and maybe I'll talk on that more some other time.

If anyone knows more about this stuff, I'd love to hear what solutions you have come up with. Posted on Wednesday, September 16, 2009 11:42 PM SQL Server Reporting Services , .NET Development | Back to top


Comments on this post: Mitch Vs Asp.net Report Viewer Control Round Two

# re: Mitch Vs Asp.net Report Viewer Control Round Two
Requesting Gravatar...
I wan also looking to try something similar, but haven't got spare time yet

Where is your solution, can i try it?

Left by Oghenez on Apr 13, 2015 3:36 PM

# re: Mitch Vs Asp.net Report Viewer Control Round Two
Requesting Gravatar...
Thanks for your comment Oghenez. Unfortunately it has been many years since I last worked on SSRS, and this code was written for a company I no longer work for. As far as I know, the project has also since been decommissioned.

I'm sorry this answer isn't overly helpful, from my very vague memory on this subject, I think the key was that in addition to rendering the HTML, you needed to download the other data streams which contained the image data, write the binary to a file (or stream directly through to the browser), in order for the content to render correctly.
Left by Mitchell Lee on Apr 13, 2015 4:01 PM

Your comment:
 (will show your gravatar)


Copyright © Mitchell Lee | Powered by: GeeksWithBlogs.net