Public Class KitDetailReportViewer
    Inherits Functions
    Dim Report As New KitDetailReport

    Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/KitDetailReportViewer.aspx?pdf=true&SD=" & txtStartDate.Text & "','_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/KitDetailReportViewer.aspx?pdf=false&SD=" & txtStartDate.Text & "','_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 dsKitDetailReport
        Dim TTableAdapter As New dsKitDetailReportTableAdapters.KitDetailReportTableAdapter
        Dim connection As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim StartDate As String = Request.QueryString("SD")

        Authenticate()

        If Request.QueryString("pdf") <> "" Then
            TTableAdapter.SelectCommandTimeout = 1800
            TTableAdapter.Connection = connection
            TTableAdapter.FillByAsOfDate(TDataSet.KitDetailReportDataTable, StartDate)

            Report.SetDataSource(TDataSet)

            Me.CrystalReportViewer1.ReportSource = Report
            Dim TextField As CrystalDecisions.CrystalReports.Engine.TextObject
            TextField = Report.ReportDefinition.ReportObjects.Item("txtDateRange")
            TextField.Text = "As of Date: " & StartDate

            If Request.QueryString("pdf") = True Then
                ExportReport()
                Report.Section1.SectionFormat.EnableSuppress = False
                Report.Section5.SectionFormat.EnableSuppress = False
            Else
                Report.Section1.SectionFormat.EnableSuppress = True
                Report.Section5.SectionFormat.EnableSuppress = True
                ExportReportXLS()
            End If
        Else
            If Page.IsPostBack = False Then
                txtStartDate.Text = Now.Date
            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
        'Dim objExcelOptions As New CrystalDecisions.Shared.ExcelDataOnlyFormatOptions
        'objExcelOptions.ExcelUseConstantColumnWidth = False
        'objExcelOptions.SimplifyPageHeaders = True
        'Report.ExportOptions.ExportFormatOptions = objExcelOptions
        'Report.ExportOptions.FormatOptions = objExcelOptions


        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