Imports System.Data.SqlClient
Imports MetroFramework
Imports MetroFramework.Forms
Public Class frmBank

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If lblSet.Text = "Branch" Then
            frmBranchMaster_Bank.fillCombo()
        End If
        Me.Close()
    End Sub
    Sub Reset()
        txtBankName.Text = ""
        txtBank.Text = ""
        If IsSaveAllowed(lblUser.Text, "Banking") = True Then
            btnSave.Enabled = True
        Else
            btnSave.Enabled = False
        End If
        If IsViewAllowed(lblUser.Text, "Banking") = True Then
            Getdata()
        Else
            dgw.Rows.Clear()
        End If
        btnDelete.Enabled = False
        btnUpdate.Enabled = False
        txtBankName.Focus()
    End Sub
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Reset()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtBankName.Text = "" Then
            MetroMessageBox.Show(Me, "Please enter Bank name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtBankName.Focus()
            Return
        End If

        Try
            con = New SqlConnection(cs)
            con.Open()
            Dim ct As String = "select BankName from Bank where BankName=@d1"
            cmd = New SqlCommand(ct)
            cmd.Parameters.AddWithValue("@d1", txtBankName.Text)
            cmd.Connection = con
            rdr = cmd.ExecuteReader()

            If rdr.Read() Then
                MetroMessageBox.Show(Me, "Bank Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                txtBankName.Text = ""
                txtBankName.Focus()
                If (rdr IsNot Nothing) Then
                    rdr.Close()
                End If
                Return
            End If

            con = New SqlConnection(cs)
            con.Open()

            Dim cb As String = "insert into Bank(BankName) VALUES (@d1)"
            cmd = New SqlCommand(cb)
            cmd.Parameters.AddWithValue("@d1", txtBankName.Text)
            cmd.Connection = con
            cmd.ExecuteReader()
            con.Close()
            Dim st As String = "added the new Bank '" & txtBankName.Text & "'"
            LogFunc(lblUser.Text, st)
            MetroMessageBox.Show(Me, "Successfully Saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Reset()
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If txtBankName.Text = "" Then
            MetroMessageBox.Show(Me, "Please enter Bank name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtBankName.Focus()
            Return
        End If

        Try

            con = New SqlConnection(cs)
            con.Open()

            Dim cb As String = "Update Bank set BankName=@d1 where BankName=@d2"
            cmd = New SqlCommand(cb)
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@d1", txtBankName.Text)
            cmd.Parameters.AddWithValue("@d2", txtBank.Text)
            cmd.ExecuteReader()
            con.Close()
            Dim st As String = "updated the Bank '" & txtBankName.Text & "'"
            LogFunc(lblUser.Text, st)
            MetroMessageBox.Show(Me, "Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Reset()
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub
    Private Sub DeleteRecord()

        Try
            Dim RowsAffected As Integer = 0
            con = New SqlConnection(cs)
            con.Open()
            Dim cl As String = "select Bank.BankName from BankBranch,Bank where BankBranch.BankName=Bank.BankName and Bank.BankName=@d1"
            cmd = New SqlCommand(cl)
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@d1", txtBank.Text)
            rdr = cmd.ExecuteReader()
            If rdr.Read Then
                MetroMessageBox.Show(Me, "Unable to delete..Already in use in Branch Master Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                If Not rdr Is Nothing Then
                    rdr.Close()
                End If
                Exit Sub
            End If
            con.Close()
            con = New SqlConnection(cs)
            con.Open()
            Dim cq As String = "delete from Bank where BankName=@d1"
            cmd = New SqlCommand(cq)
            cmd.Parameters.AddWithValue("@d1", txtBank.Text)
            cmd.Connection = con
            RowsAffected = cmd.ExecuteNonQuery()
            If RowsAffected > 0 Then
                Dim st As String = "deleted the Bank '" & txtBankName.Text & "'"
                LogFunc(lblUser.Text, st)
                MetroMessageBox.Show(Me, "Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Reset()
            Else
                MetroMessageBox.Show(Me, "No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Reset()
            End If
            If con.State = ConnectionState.Open Then
                con.Close()

            End If
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            If MetroMessageBox.Show(Me, "Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                DeleteRecord()
            End If
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
        Try
            If dgw.Rows.Count > 0 Then
                Dim dr As DataGridViewRow = dgw.SelectedRows(0)
                txtBank.Text = dr.Cells(0).Value.ToString()
                txtBankName.Text = dr.Cells(0).Value.ToString()
                If IsDeleteAllowed(lblUser.Text, "Banking") = True Then
                    btnDelete.Enabled = True
                Else
                    btnDelete.Enabled = False
                End If
                If IsUpdateAllowed(lblUser.Text, "Banking") = True Then
                    btnUpdate.Enabled = True
                Else
                    btnUpdate.Enabled = False
                End If
                btnSave.Enabled = False
            End If
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString()
        Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
        If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
            dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
        End If
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))

    End Sub
    Public Sub Getdata()
        Try
            con = New SqlConnection(cs)
            con.Open()
            cmd = New SqlCommand("SELECT RTRIM(BankName) from Bank order by BankName", con)
            rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            dgw.Rows.Clear()
            While (rdr.Read() = True)
                dgw.Rows.Add(rdr(0))
            End While
            con.Close()
        Catch ex As Exception
            MetroMessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class
