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

Public Class RecordsSelection2

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click


        Try
            If txtFirstDate.Text = "" Or TxtLastDate.Text = "" Then
                MessageBox.Show("Please Enter First Date and Last Date", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            Dim msg As DialogResult = MessageBox.Show("Do you want to delete Records? ", "Response", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If (msg = vbYes) Then

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

                Try
                    Dim dap As OleDbDataAdapter = New OleDbDataAdapter("select * from Table1 where [Dat] between  '" & Me.txtFirstDate.Text & "' and '" & Me.TxtLastDate.Text & "'", OleCn)
                    Dim ds As DataSet = New DataSet
                    dap.Fill(ds, "table1")

                    Dim sSQL As String = "Delete From Table1 where [Dat] between '" & txtFirstDate.Text & "' and '" & TxtLastDate.Text & "'"
                    Dim cmd As OleDbCommand = New OleDbCommand(sSQL, OleCn)

                    If cmd.ExecuteNonQuery() Then

                        MsgBox("( " & Me.BindingContext(ds, "Table1").Count & " ) Records Deleted Successfully", MsgBoxStyle.Information, "Records Deleted")
                        OleCn.Close()
                        MainFrm.cmdRefresh.PerformClick()
                        Me.Close()

                    Else
                        MsgBox("Records Deletion Failed ", MsgBoxStyle.Critical, "Deletion Failed")
                        Return
                    End If

                Catch ex As Exception
                    MessageBox.Show(ex.Message.ToString(), "Deletion Error")
                    Exit Sub
                End Try
            End If

        Catch ex As Exception
        End Try
    End Sub

    Private Sub RecordsSelection2_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 cmdPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

        Me.Cursor = Cursors.WaitCursor
        If Me.CBFormState.Text = "Print" Then

            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 where Dat between '" & txtFirstDate.Text & "' and '" & TxtLastDate.Text & "' Order by SerialNo "

                Dim Cmd As New OleDbCommand(QueryString, OleCn)
                'Cmd.Parameters.Add("@SerialNo", OleDbType.VarChar).Value = PrintmakerGridView.SelectedCells(0).Value.ToString()

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

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

    End Sub

    Private Sub RecordsSelection2_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
End Class