Tuesday, February 21, 2012

ReportViewer: creating PDF on the fly

Hi all,

I'm writing a offer web page using ReportViewer control.
This offer page will be shown in PDF format.

I understood that the ReportViewer control can't work without any DataSource, but for my needs I use report parameters and not DataSet (or somesimilar object). So, I defined the unreal DataSet just for deceive the ReportViewer (I don't know if this solution is wise, but probably it's what I ableSmile).

<rsweb:ReportViewer runat="server" ID="ReportViewer1" Font-Names="Verdana" Font-Size="8pt" Height="400px" ShowToolBar="False" Width="685px"> <LocalReport ReportPath="Offer.rdlc" > <DataSources> <rsweb:ReportDataSource DataSourceId="objectDataSource" Name="DataSet1" /> </DataSources> </LocalReport></rsweb:ReportViewer><asp:ObjectDataSource runat="server" ID="objectDataSource" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="DataSet1TableAdapters.MyTableAdapter"></asp:ObjectDataSource>
So far so good. When I activate the offer page, it shown properly, but when I add the following code for create the PFD document from the page, I get an error:
A data source instance has not been supplied for the data source "DataSet1"
protected void Page_Load(object sender, EventArgs e){ Warning[] warnings;string[] streamids;string mimeType;string encoding;string extension;byte[] myBytes =this.ReportViewer1.LocalReport.Render("PDF",null,out mimeType,out encoding,out extension,out streamids,out warnings); Response.Buffer =true; Response.Clear(); Response.ContentType = mimeType; Response.BinaryWrite(myBytes); Response.Flush(); Response.End();}

Please help!

Thanks in advance!

Hi,

Take a look over here :

http://blogs.msdn.com/bimusings/archive/2005/07/01/434659.aspx

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--

|||

Hi Suprotim Agarwal,

Thank you for the quick response.
I already saw this short article, but it not exactly answers to my case - the error appears only when I create a PDF document from my predefined ReportViewer. If I delete an aforementioned code, the Offer.aspx page work properly.

|||

Hi,

Based on the code you provided, you are going to render the report in the code behind, right?

I think you should declare and add the report datasource explicitly in your code-behind file before you are invoking the render method, see the following code snippet:

protected void Page_Load(object sender, EventArgs e)
{
this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
this.ReportViewer1.LocalReport.ReportPath =
@."c:\Reports\Report1.rdl";
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Sales", LoadSalesData())); declare and add the datasource explicitly.

}
protected void Button1_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = ReportViewer1.LocalReport.Render(
"Excel", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(@."c:\output.xls",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();

Label1.Text = "Report exported to output.xls";

}
}

Thanks.

|||

The problem solved when I moved a code that creates a PDF document from Page_Load to Page_SaveStateComplete event.

Thanks to all of you!

No comments:

Post a Comment