Public Class CustomerLineUpAndPricingReportViewer
    Inherits Functions
    Dim Report As New CustomerLineUpAndPricingReport

    Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/CustomerLineUpAndPricingReportViewer.aspx?pdf=true&Cust=" & txtCustomer.Text & "&PIN=" & txtSalesmanPIN.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/CustomerLineUpAndPricingReportViewer.aspx?pdf=false&Cust=" & txtCustomer.Text & "&PIN=" & txtSalesmanPIN.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 dsCustomerLineUpAndPricing
        Dim TTableAdapter As New dsCustomerLineUpAndPricingTableAdapters.CustomerLineUpAndPricingTableAdapter
        Dim connection As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Cust As String = Request.QueryString("Cust")
        Dim SalesPin As String = Request.QueryString("PIN")

        Authenticate()

        If Request.QueryString("pdf") <> "" Then
            TTableAdapter.SelectCommandTimeout = 1800
            TTableAdapter.Connection = connection
            TTableAdapter.FillByCustomerAndSalesman(TDataSet.CustomerLineUpAndPricingDataTable, Cust, SalesPin)

            Report.SetDataSource(TDataSet)
            'Dim TextField As CrystalDecisions.CrystalReports.Engine.TextObject
            'TextField = Report.ReportDefinition.ReportObjects.Item("txtDateRange")
            'TextField.Text = StartDate & " - " & EndDate

            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
        Else
            If Page.IsPostBack = False Then
                'txtStartDate.Text = Now.Date
                'txtEndDate.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

        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