Public Class ItemRankByCategoryCustomerSalesmanReportViewer
    Inherits Functions
    Dim Report As New ItemRankByCategoryCustomerSalesmanReport

    Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/ItemRankByCategoryCustomerSalesmanReportViewer.aspx?pdf=true&SD=" & txtStartDate.Text & "&ED=" & txtEndDate.Text & "&PIN=" & txtSalesmanPIN.Text & "&Cust=" & txtCustomer.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/ItemRankByCategoryCustomerSalesmanReportViewer.aspx?pdf=false&SD=" & txtStartDate.Text & "&ED=" & txtEndDate.Text & "&PIN=" & txtSalesmanPIN.Text & "&Cust=" & txtCustomer.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 dsItemRankByCategoryCustomerSalesman
        Dim TTableAdapter As New dsItemRankByCategoryCustomerSalesmanTableAdapters.ItemRankByCategoryCustomerSalesmanTableAdapter
        Dim connection As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim StartDate As String = Request.QueryString("SD")
        Dim EndDate As String = Request.QueryString("ED")
        Dim SalesmanPIN As String = Request.QueryString("PIN")
        Dim Customer As String = Request.QueryString("Cust")

        Authenticate()

        If Request.QueryString("pdf") <> "" Then
            TTableAdapter.SelectCommandTimeout = 1800
            TTableAdapter.Connection = connection
            TTableAdapter.FillByDateCustomerSalesman(TDataSet.ItemRankByCategoryCustomerSalesmanDataTable, StartDate, EndDate, SalesmanPIN, Customer)

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

            Me.CrystalReportViewer1.ReportSource = Report

            If Request.QueryString("pdf") = True Then
                ExportReport()
            Else
                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