views:

20

answers:

1

Hi can any one help me out for this.

I have RDLC Report displayed on my web page using asp.net And C#.net I want to export it to PDF on button click.

Please can you help me?

Thanks

A: 

I did something like this a while ago. Below is the code I used in the page_load event of a page. It is in VB and isn't the best code in the world but might help you get a solution..

    Dim jobid As Integer = Request("jobid")
    Dim rv As New Microsoft.Reporting.WebForms.ReportViewer
    Dim r As String = "apps/Reports/legal_document.rdlc"
    Dim ds As New jobmanagerTableAdapters.JobInformationTableAdapter
    Dim ds2 As New ordermanagementTableAdapters.RecoveryItemsInformationTableAdapter
    Dim ds3 As New expensemanagerTableAdapters.tbl_expensesTableAdapter
    Dim ds4 As New ordermanagementTableAdapters.tbl_orders_collection_itemsTableAdapter
    Dim ds5 As New attachmentsmanagerTableAdapters.tbl_attachmentsTableAdapter
    Dim ds6 As New notesmanagerTableAdapters.tbl_notesTableAdapter
    Dim ds7 As New payments_managerTableAdapters.tbl_paymentsTableAdapter


    Dim rptSource1 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource2 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource3 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource4 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource5 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource6 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptsource7 As New Microsoft.Reporting.WebForms.ReportDataSource

    rptSource1.Name = "jobmanager_JobInformation"
    rptSource1.Value = ds.GetJobInfobyJobID(jobid)

    rptSource2.Name = "ordermanagement_RecoveryItemsInformation"
    rptSource2.Value = ds2.GetRecoveryItemsbyJobIDOrderID(jobid, 0)

    rptSource3.Name = "expensemanager_tbl_expenses"
    rptSource3.Value = ds3.GetExpensesbyJobIDOrderID(jobid, 0)

    rptSource4.Name = "ordermanagement_tbl_orders_collection_items"
    rptSource4.Value = ds4.GetDataByJobIDOrderID(jobid, 0)

    rptSource5.Name = "attachmentsmanager_tbl_attachments"
    rptSource5.Value = ds5.GetAllAttachmentsbyJobID(jobid)

    rptSource6.Name = "notesmanager_tbl_notes"
    rptSource6.Value = ds6.GetAllNotesbyJobID(jobid)

    rptsource7.Name = "payments_manager_tbl_payments"
    rptsource7.Value = ds7.GetPaymentsbyJobID(jobid)


    rv.LocalReport.DataSources.Clear()

    rv.LocalReport.ReportPath = r.ToString
    rv.LocalReport.DataSources.Add(rptSource1)
    rv.LocalReport.DataSources.Add(rptSource2)
    rv.LocalReport.DataSources.Add(rptSource3)
    rv.LocalReport.DataSources.Add(rptSource4)
    rv.LocalReport.DataSources.Add(rptSource5)
    rv.LocalReport.DataSources.Add(rptSource6)
    rv.LocalReport.DataSources.Add(rptsource7)

    'Page.Controls.Add(rv)

    Dim warnings As Warning() = Nothing
    Dim streamids As String() = Nothing
    Dim mimeType As String = Nothing
    Dim encoding As String = Nothing
    Dim extension As String = Nothing
    Dim bytes As Byte()



    'Get folder on web server from web.config
    Dim FolderLocation As String
    FolderLocation = Server.MapPath("reports")


    'First delete existing file
    Dim filepath As String = FolderLocation & "\legal.PDF"
    File.Delete(filepath)



    'Then create new pdf file
    bytes = rv.LocalReport.Render("PDF", Nothing, mimeType, _
        encoding, extension, streamids, warnings)


    Dim fs As New FileStream(FolderLocation & "\legal.PDF", FileMode.Create)
    fs.Write(bytes, 0, bytes.Length)
    fs.Close()

    Response.Redirect("reports/legal.pdf")
DevDave
Requires write access to the filesystem. Why not just read the bytes into a MemoryStream, clear the response stream, set the HTTP headers correctly, and stream the file down to the client directly?
tomfanning
Yeppers, that would work. I figured since it was already read into bytes that it would be obvious that was an option.
DevDave