Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data.OleDb
Imports System.IO

Public Class Reports
  
    Private Sub Reports_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            OleCn.Close()
            Me.Dispose()

        Catch ex As Exception
        End Try
    End Sub

    Private Sub Reports_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        e.SuppressKeyPress = e.Control
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub Reports_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            CrystalReportViewer1.Height = Me.Height - 118
            CrystalReportViewer1.Width = Me.Width - 16
            Me.ComboBox1.SelectedItem = "Serial No"
            TextBox1.Focus()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Try
            OleCn.Close()
            Me.Close()

        Catch ex As Exception
        End Try
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

        Dim cryRpt As New ReportDocument
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table

        If Me.ComboBox1.Text = "" Then
            MsgBox("First You Should Select The Search Category !!!", MsgBoxStyle.Critical, "Attention...")
            Exit Sub
        End If

            Try
                With OleCn
                    If .State <> ConnectionState.Open Then
                        .ConnectionString = StrConnection()
                        .Open()
                    End If
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try

        If Me.ComboBox1.Text = "Serial No" Then
            Try
                Me.Cursor = Cursors.WaitCursor
                cryRpt.Load(My.Application.Info.DirectoryPath & "\Reports\Report1.rpt")

                With crConnectionInfo
                    .ServerName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .DatabaseName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .UserID = ""
                    .Password = ""
                End With

                CrTables = cryRpt.Database.Tables
                For Each CrTable In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo
                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next


                Dim QueryString As String
                QueryString = "Select SerialNo,CabinNo,Dat,CustomerName,ID_Proof,LoginTime,LogoutTime,Duration,Amount,Particular from Table1 where SerialNo like @SerialNo Order by SerialNo"

                Dim Cmd As New OleDbCommand(QueryString, OleCn)
                Cmd.Parameters.Add("@SerialNo", OleDbType.VarChar).Value = TextBox1.Text

                Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(Cmd)
                Dim ds As DataSet = New DataSet()
                Adapter.Fill(ds, "Table1")

                cryRpt.SetDataSource(ds)
                Me.CrystalReportViewer1.ReportSource = cryRpt
                'Me.Show()
                OleCn.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.Message(), MsgBoxStyle.Critical, "Print Report Error...")
            End Try

            Return
        End If


        If Me.ComboBox1.Text = "Name" Then
            Try
                Me.Cursor = Cursors.WaitCursor
                cryRpt.Load(My.Application.Info.DirectoryPath & "\Reports\Report1.rpt")

                With crConnectionInfo
                    .ServerName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .DatabaseName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .UserID = ""
                    .Password = ""
                End With

                CrTables = cryRpt.Database.Tables
                For Each CrTable In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo
                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next


                Dim QueryString As String
                QueryString = "Select SerialNo,CabinNo,Dat,CustomerName,ID_Proof,LoginTime,LogoutTime,Duration,Amount,Particular from Table1 where CustomerName like @CustomerName Order by SerialNo"

                Dim Cmd As New OleDbCommand(QueryString, OleCn)
                Cmd.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = TextBox1.Text

                Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(Cmd)
                Dim ds As DataSet = New DataSet()
                Adapter.Fill(ds, "Table1")

                cryRpt.SetDataSource(ds)
                Me.CrystalReportViewer1.ReportSource = cryRpt
                'Me.Show()
                OleCn.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.Message(), MsgBoxStyle.Critical, "Print Report Error...")
            End Try

            Return
        End If

        If Me.ComboBox1.Text = "Cabin No" Then
            Try
                Me.Cursor = Cursors.WaitCursor
                cryRpt.Load(My.Application.Info.DirectoryPath & "\Reports\Report1.rpt")

                With crConnectionInfo
                    .ServerName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .DatabaseName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .UserID = ""
                    .Password = ""
                End With

                CrTables = cryRpt.Database.Tables
                For Each CrTable In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo
                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next


                Dim QueryString As String
                QueryString = "Select SerialNo,CabinNo,Dat,CustomerName,ID_Proof,LoginTime,LogoutTime,Duration,Amount,Particular from Table1 where CabinNo like @CabinNo Order by SerialNo"

                Dim Cmd As New OleDbCommand(QueryString, OleCn)
                Cmd.Parameters.Add("@CabinNo", OleDbType.VarChar).Value = TextBox1.Text

                Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(Cmd)
                Dim ds As DataSet = New DataSet()
                Adapter.Fill(ds, "Table1")

                cryRpt.SetDataSource(ds)
                Me.CrystalReportViewer1.ReportSource = cryRpt
                'Me.Show()
                OleCn.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.Message(), MsgBoxStyle.Critical, "Print Report Error...")
            End Try

            Return
        End If

        If Me.ComboBox1.Text = "Date" Then
            Try
                cryRpt.Load(My.Application.Info.DirectoryPath & "\Reports\Report1.rpt")

                With crConnectionInfo
                    .ServerName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .DatabaseName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                    .UserID = ""
                    .Password = ""
                End With

                CrTables = cryRpt.Database.Tables
                For Each CrTable In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo
                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next


                Dim QueryString As String
                QueryString = "Select SerialNo,CabinNo,Dat,CustomerName,ID_Proof,LoginTime,LogoutTime,Duration,Amount,Particular from Table1 where Dat like @Dat Order by SerialNo"

                Dim Cmd As New OleDbCommand(QueryString, OleCn)
                Cmd.Parameters.Add("@Dat", OleDbType.VarChar).Value = TextBox1.Text

                Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(Cmd)
                Dim ds As DataSet = New DataSet()
                Adapter.Fill(ds, "Table1")

                cryRpt.SetDataSource(ds)
                Me.CrystalReportViewer1.ReportSource = cryRpt
                'Me.Show()
                OleCn.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.Message(), MsgBoxStyle.Critical, "Print Report Error...")
            End Try
        End If

    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click, cmdRefresh.Click

        Me.Cursor = Cursors.WaitCursor
        Dim cryRpt As New ReportDocument
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table

        Try
            With OleCn
                If .State <> ConnectionState.Open Then
                    .ConnectionString = StrConnection()
                    .Open()
                End If
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try


        Try
            cryRpt.Load(My.Application.Info.DirectoryPath & "\Reports\Report1.rpt")

            With crConnectionInfo
                .ServerName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                .DatabaseName = Application.StartupPath & "\BackUp\Printmaker11.Accdb"
                .UserID = ""
                .Password = ""
            End With

            CrTables = cryRpt.Database.Tables
            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = crConnectionInfo
                CrTable.ApplyLogOnInfo(crtableLogoninfo)
            Next


            Dim QueryString As String
            QueryString = "Select SerialNo,CabinNo,Dat,CustomerName,ID_Proof,LoginTime,LogoutTime,Duration,Amount,Particular from Table1 order by SerialNo"

            Dim Cmd As New OleDbCommand(QueryString, OleCn)
            Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(Cmd)
            Dim ds As DataSet = New DataSet()
            Adapter.Fill(ds, "Table1")

            cryRpt.SetDataSource(ds)
            Me.CrystalReportViewer1.ReportSource = cryRpt
            TextBox1.Text = ""
            TextBox1.Focus()
            'Me.Show()
            OleCn.Close()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.Message(), MsgBoxStyle.Critical, "Print Report Error...")
        End Try
        TextBox1.Focus()
    End Sub
End Class