Render PDF from ReportViewer on Button Click

Last post 05-17-2008 5:04 AM by Usha.ankadi. 4 replies.

Sort Posts:

  • Render PDF from ReportViewer on Button Click

    11-16-2007, 3:32 PM
    • Loading...
    • bortiquai
    • Joined on 05-25-2007, 8:48 PM
    • Posts 117

    I am trying to render a PDF from a reportviewer on a button click.  I have found the following code, but it only works when is placed in the Page_Load event.  I can't seem to make it work for the Button_Click event.  I receive an error.  I'm doing this as a "Work Around" for the lack of a print button on the ReportViewer Control when in local mode.  Is there a better way than this?  Thanks.

    Code for Button_Click Event: 

            Dim AuditInv As New DVDWarehouse
            ReportViewer1.ProcessingMode = ProcessingMode.Local
            Dim rep As LocalReport = ReportViewer1.LocalReport
            rep.ReportPath = "InventoryAudit.rdlc"

            Dim dt As DataTable = AuditInv.GetDataTable("Select Inventory.BoxID, Inventory.SKU, DVDInfo.Title, Inventory.ItemCount from Inventory, DVDInfo where DVDInfo.SKU = Inventory.SKU ORDER by BoxID", "Inventory")

            Dim dsAudit As New ReportDataSource
            dsAudit.Name = "InventoryAudit_InventoryAudit"
            dsAudit.Value = dt
            rep.DataSources.Clear()
            rep.DataSources.Add(dsAudit)

            Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing
            Dim streamids As String() = Nothing
            Dim mimeType As String = Nothing
            Dim encoding As String = Nothing
            Dim extension As String = Nothing
            Dim DeviceInfo As String = "<DeviceInfo>" _
                & "  <OutputFormat>PDF</OutputFormat>" _
                & "  <PageWidth>8.5in</PageWidth>" _
                & "  <PageHeight>11.5in</PageHeight>" _
                & "  <MarginTop>0.6in</MarginTop>" _
                & "  <MarginLeft>0.6in</MarginLeft>" _
                & "  <MarginRight>0.4in</MarginRight>" _
                & "  <MarginBottom>0.4in</MarginBottom>" _
                & "</DeviceInfo>"
            Dim bytes As Byte()
            bytes = ReportViewer1.LocalReport.Render("PDF", DeviceInfo, mimeType, encoding, extension, streamids, warnings)
            Response.Clear()
            Response.ContentType = mimeType
            Response.AddHeader("content-disposition", "attachment; filename=Test." & extension)
            Response.BinaryWrite(bytes)
            Response.End()

     

    Error Message when i click the button:

    Sys.WebForms.PageRequestManagerParserErrorException:  The message received from the server could not be parsed.  Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules or server trace is enabled.
    Details:  Error parsing near '%PDF-1.3%
    1 0 obj'.

     

    Thanks 

  • Re: Render PDF from ReportViewer on Button Click

    11-19-2007, 1:23 AM
    Answer

    Hi:

    bortiquai:
    but it only works when is placed in the Page_Load event.  I can't seem to make it work for the Button_Click event. 

      If so my suggestion is to use <input type="submit'> directly and add JavaScript function to it's "onclick" event. In the JavaScript function you can change a hidden input's value. Then in Page_Load you can know whether the button is clicked via the value of the hidden input.

      Another option is simply add Response.Redirect("url_of_new_page") in the click event handler to redirect to a new page. In the new page's Page_Load event handler you can write the code to render pdf file.

      And check out this article to see whether it can help:

    http://technoblab.blogspot.com/

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Idea [Idea] Re: Render PDF from ReportViewer on Button Click

    11-19-2007, 3:30 AM
    Answer
    • Loading...
    • warnajith
    • Joined on 11-12-2007, 11:11 AM
    • Sri Lanka
    • Posts 50

    The Problem is, since your using AJAX Update panel. Because  AJAX Update panel render page proportionally you can write anything to the Response Object, which means after the page is completely load. That's why you can run this code in the Page_Load event.

    This is the cause for the error.

    bortiquai:
    Response.Clear()
            Response.ContentType = mimeType
            Response.AddHeader("content-disposition", "attachment; filename=Test." & extension)
            Response.BinaryWrite(bytes)
            Response.End()

    But don't worry there's a remedy for this.

     What i recommending is display report in a new pop-page.

    Follow these steps.

    1. Add a new aspx page,says reports.aspx, to the project. In that code behind file, add this coding,  (C# coding)
      protected Page_Load(object sender, EvntArgs e)
      {
      Response.ContentType = "application/pdf";// set the MIME type here
      Response.AddHeader("content-disposition", "attachment; filename=Test." + extension)
      Response.BinaryWrite((byte[])Session["rpt"]);
      }
       
    2. In your Button_Click event ,remove this  
      Response.Clear()
      Response.ContentType = mimeType
      Response.AddHeader("content-disposition", "attachment; filename=Test." & extension)
      Response.BinaryWrite(bytes)
      Response.End()
       and add this (C# coding) 
      Session["report"] = bytes;
      System.Text.StringBuilder _sb = new System.Text.StringBuilder();
      _sb.Append("window.open('reports.aspx','',");
      _sb.Append("'toolbar=0,menubar=0,resizable=yes')");
      ScriptManager.RegisterStartupScript(Page, Page.GetType(), "winOpen", _sb.ToString(), true);
       

    if you want to report display in the same page, 

     _sb.Append("window.open('reports.aspx','_self',"); 
    instead of 
     _sb.Append("window.open('reports.aspx','',");


    Now it should work.  Please note that my codings are in C# syntax.

    Kelum W. Ganegoda (MCSD.NET)
    :: Please mark as answer if you got it right ::
  • Re: Render PDF from ReportViewer on Button Click

    01-09-2008, 11:48 PM

     

     Problem

    html:

    <asp:Button ID="btnCSV" Text="Export Results to CSV" runat="server" OnClick="btnCSV_OnClick" />

     

    code behind:

    protected void btnCSV_OnClick(object sender, EventArgs e)

    {

    LoadFilteredMembers();

    Response.Clear();

    Response.ClearHeaders();

    Response.ClearContent();

    Response.Buffer =
    true;

    Response.ContentType = "text/csv";

    Response.AddHeader("Content-Disposition", "attachment;filename=SiteMembers.csv");

    Response.Charset = "";

    this.EnableViewState = false;ProduceCSV(dt, Response.Output, true);

    Response.End();

     

    }

     

    public static void ProduceCSV(DataTable dt, System.IO.TextWriter httpStream, bool WriteHeader)

    {

    httpStream.WriteLine(
    "Member ID, User Name, First Name, Last Name, Associated Member Id, Created Date Time");for (int j = 0; j < dt.Rows.Count; j++)

    {

    string[] dataArr = new String[dt.Columns.Count];

    for (int i = 0; i < dt.Columns.Count; i++)

    {

    object o = dt.Rows[j][i];

    dataArr[i] = GetWriteableValue(o);

    }

    httpStream.WriteLine(
    string.Join(",", dataArr));

    }

    }

    public static string GetWriteableValue(object o)

    {

    if (o == null || o == Convert.DBNull)

    return "";

    else if (o.ToString().IndexOf(",") == -1)

    return o.ToString();

    else

    return "\"" + o.ToString() + "\"";

    }

     

     

    Solution:

    <Triggers>

    <asp:PostBackTrigger ControlID="btnCSV"/>

     

     

    www.JeremyThompson.Net (MCSD.Net, MCT 06/07)

  • Re: Render PDF from ReportViewer on Button Click

    05-17-2008, 5:04 AM
    • Loading...
    • Usha.ankadi
    • Joined on 05-17-2008, 4:57 AM
    • Posts 1

    I used the above mentioned code but still my problem dint get solved,

    now using

    <Triggers>

    <asp:PostBackTrigger ControlID="btnCSV"/>

    is not giving parseerror exception but a new window is opening

    please help me in this

    opening this in new page is not giving any error but only in update panel i am getting the new window in button click

    below is the code i am using

    '**clear repsponce

    Response.Clear()

    '**raises down load window of operating system and allows to select application type.

    Response.AddHeader("content-disposition", "filename=outload.ezo")

    '**no need to set content type so that you can save in any format

    Response.ContentType = "application/txt"

    '** remove the charset from the content-type header.

    Response.Charset = ""

    Response.Write(ConvertfeesetCollectionToTxt)

    Response.End()

     

    //

    Private Function ConvertfeesetCollectionToTxt() As String

    Dim _FeeSchd As New EZCAPNETEntity.FeeSchedule_Class

    Dim _ColFeeSchdInfo As New List(Of EZCAPNETEntity.FeeSchedule_Class)

    Dim _BAL As New EZCAPNETBLLayer.BLLServiceCodesDim ds As New DataSet

    _FeeSchd.ServiceType = ddlServiceType.SelectedValue

    ds = _BAL.getFeescheduleNew_OutLoad(_FeeSchd, "O")

    Dim _StringWriter As New System.IO.StringWriter()

    Dim dr As DataRow

    Dim arr() As Object

    For Each dr In ds.Tables(0).Rows

    arr = dr.ItemArray

    For i1 As Integer = 0 To UBound(arr)

    _StringWriter.Write(arr(i1).ToString & vbTab)

    Next

    _StringWriter.Write(vbCrLf)

    Next

    Response.Write(_StringWriter.ToString)

    End Function

    thanks

    Usha

Page 1 of 1 (5 items)