Partial Public Class EndOfDayReportViewer
    Inherits Functions
    Dim Report As New EndOfDayReport

    Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/EndOfDayReportViewer.aspx?pdf=true','_blank')")
        Response.Write("</script>")
    End Sub
    Protected Sub btnXLS_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnXLS.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/EndOfDayReportViewer.aspx?pdf=false','_blank')")
        Response.Write("</script>")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim TDataSet As New dsEndOfDay
        Dim TTableAdapter As New dsEndOfDayTableAdapters.EndOfDayTableAdapter
        Dim connection As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        Authenticate()

        If Request.QueryString("pdf") <> "" Then
            TTableAdapter.SelectCommandTimeout = 1800
            TTableAdapter.Connection = connection
            TTableAdapter.Fill(TDataSet.EndOfDayDataTable)

            Report.SetDataSource(TDataSet)

            Me.CrystalReportViewer1.ReportSource = Report

            If Request.QueryString("pdf") = True Then
                Report.Section1.SectionFormat.EnableSuppress = False
                Report.Section5.SectionFormat.EnableSuppress = False
                ExportReport()
            Else
                Report.Section1.SectionFormat.EnableSuppress = True
                Report.Section5.SectionFormat.EnableSuppress = True
                ExportReportXLS()
            End If
        End If
    End Sub

    Protected Sub ExportReport()

        Dim oStream As New System.IO.MemoryStream

        oStream = Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/pdf"
        '--------------------------------------------------------------

        Try
            Response.BinaryWrite(oStream.ToArray())
            Response.End()
        Catch err As Exception
            Response.Write("< BR >")
            Response.Write(err.Message.ToString)
        End Try
    End Sub
    Protected Sub ExportReportXLS()

        Dim oStream As New System.IO.MemoryStream

        oStream = Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        '--------------------------------------------------------------

        Try
            Response.BinaryWrite(oStream.ToArray())
            Response.End()
        Catch err As Exception
            Response.Write("< BR >")
            Response.Write(err.Message.ToString)
        End Try
    End Sub
End Class