Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports ModCommon
Partial Class BankMaster
    Inherits System.Web.UI.Page
    Shared Search As Boolean
    Shared g_User As String
    Shared g_Company As String
    Shared g_FinYear As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            If (Not IsPostBack) Then        'If IsPostBack is false then
                ClearFields()               'Clear the TextFields
                UnLockTextBox(False)        'Unlock (Enable) TextFields
                'g_User = "U1"
                'g_Company = "COM1"
                'g_User = Session("g_User").ToString
                g_Company = Session("g_Company").ToString
                g_FinYear = Session("g_FinYear").ToString()
                FillGrid("")                  'Fills the DatagGrid with All Records
                Call CancelClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, Session("g_User"), "Bank Master")
            End If
            txtBankCode.MaxLength = 20 : txtBankName.MaxLength = 50
            txtBankName.Attributes.Add("onblur", "return CheckNull('txtBankName');")
            txtBankName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")
            '  txtBankName.Attributes.Add("onblur", "return toUpper('txtBankName');")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub _Default_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Not IsValidConnectionString() Then CreateMessageAlert(Me, "Check the Connection String !", "StrKeyVal") 'If not a valid ConnectionString then Display the message 

    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/06/06 </DateOfCreation>
    '   <Developer> Rajesh </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            txtBankCode.Text = ""                           'Assign Null to the TextField
            txtBankName.Text = ""                           'Assign Null to the TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UnLockTextBox">
    '   <Desc> For Locking & UnLocking the TextFields </Desc>
    '   <Input> 
    '   <Param Name="Action">
    '   True or False
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/06/06 </DateOfCreation>
    '   <Developer> Rajesh </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            'txtBankCode.Enabled = Action                    'Enable / Disable the TextField
            txtBankName.Enabled = Action                    'Enable / Disable the TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillGrid">
    '   <Desc> To Fill the Grid with All Records </Desc>
    '   <Input> 
    '  <Param Name="PTN">
    '   PartType_Name
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/06/06 </DateOfCreation>
    '   <Developer> Rajesh </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub FillGrid(ByVal PTN As String)
        Try

            Dim qry As String                                   'Define a String Variable for Query
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                          'Open the Connection
            gvBank.Columns(1).Visible = True
            If Trim(PTN) = "" Then
                qry = "select distinct Bank_Kid,Bank_Code as [Code],Bank_Name as [Bank] from Bank_Master where Bank_isdeleted='0' and Bank_companyId='" & g_Company & "'"
            Else
                qry = "select distinct Bank_Kid,Bank_Code as [Code],Bank_Name as [Bank] from Bank_Master where Bank_isdeleted='0' and Bank_companyId='" & g_Company & "' and bank_name like '" & Trim(PTN) & "%'"
            End If
            Dim cmd As New SqlCommand(qry, con)                 'Define command variable to execute the query
            Dim dr As SqlDataReader                             'Define DataReader variable for storing the output of the query
            dr = cmd.ExecuteReader                              'Assign the output of query to DataReader variable
            gvBank.DataSource = dr                          'Set the DataReader Variable as DataSource for the Grid
            gvBank.DataBind()                               'Bind the Data to the Grid
            gvBank.Columns(1).Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            'If CBool(Session("Add_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Add New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)                                 'Enable the TextFields
            Call AddClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Call GenerateBankCode()
            txtBankName.Focus()                                 'Put the Focus to BankCode TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub gvBank_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvBank.SelectedIndexChanged
        Try
            txtBankId.Text = gvBank.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            txtBankCode.Text = gvBank.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtBankName.Text = gvBank.SelectedRow.Cells(3).Text     'Assign the value of cell to the textField
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClick(Me, btnAdd, btnSave, btnEdit, btnDelete, btnCancel, btnSearch)
            Search = False
            txtBankName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                 'Display the Error Message
        End Try
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            'If CBool(Session("Add_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Add New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If Trim(txtBankCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Bank Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtBankName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                       'Open the Connection
            If IsAlreadyPresent("select count(*) from Bank_Master where Bank_Code='" & txtBankCode.Text.Trim & "'and isdeleted='0' and company_code='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Bank Code Already Present !", "StrKeyVal")      'Display the Message
                txtBankCode.Text = "" : txtBankCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Bank_Master where Bank_Name='" & txtBankName.Text.Trim & "'and isdeleted='0' and company_code='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Bank Name Already Present !", "StrKeyVal")      'Display the Message
                txtBankName.Text = "" : txtBankName.Focus()
                Exit Sub
            End If

            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Bank_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@Bank_Kid", SqlDbType.NVarChar, 20)).Value = Trim(txtBankId.Text)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBankCode.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Name", SqlDbType.NVarChar, 25)).Value = Trim(txtBankName.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(g_Company)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_User"))             'Add Parameter UserCode and assign the value of Session("Session("g_User")").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Bank Master"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Insert"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call SaveClick(Me, btnAdd, btnSave, btnEdit, btnDelete, btnCancel, btnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Try
            'If CBool(Session("Modify_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Modify An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If Trim(txtBankCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Bank Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtBankName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from Bank_Master where bank_code!='" & Trim(txtBankCode.Text) & "' and  Bank_Name='" & txtBankName.Text.Trim & "'  and isdeleted='0' and company_code='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Bank Name Already Present !", "StrKeyVal")      'Display the Message
                txtBankName.Text = "" : txtBankName.Focus()
                Exit Sub
            End If

            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                      'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Bank_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@Bank_Kid", SqlDbType.NVarChar, 20)).Value = Trim(RemoveLiterals(txtBankId.Text))       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBankCode.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Name", SqlDbType.NVarChar, 25)).Value = Trim(txtBankName.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(g_Company)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_User"))             'Add Parameter UserCode and assign the value of Session("Session("g_User")").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Bank Master"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Update"                                   'Add Parameter Mode and assign the DML Operation to Execute (Update)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call EditClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            'If CBool(Session("Delete_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Delete An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If Trim(txtBankCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Bank Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBankCode.Focus()
                Exit Sub
            End If
            If Trim(txtBankName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBankName.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Branch_Master where Branch_BankCode='" & txtBankCode.Text.Trim & "'and isdeleted='0' and company_code='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Unable to Delete, Referenced to Other Records... !", "StrKeyVal")      'Display the Message
                btnCancel_Click(sender, e)
                Exit Sub
            End If
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                      'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Bank_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@Bank_Kid", SqlDbType.NVarChar, 20)).Value = Trim(RemoveLiterals(txtBankId.Text))       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBankCode.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_Name", SqlDbType.NVarChar, 25)).Value = Trim(txtBankName.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Bank_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(g_Company)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Bank_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_User"))             'Add Parameter UserCode and assign the value of Session("Session("g_User")").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Bank Master"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Delete"                                   'Add Parameter Mode and assign the DML Operation to Execute (Delete)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Search = False
            txtBankName.AutoPostBack = False
            Call FillGrid("")
            Call CancelClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
        Search = True                                       'Set Search Mode True
        Call SearchClick(Me, btnAdd, btnSave, btnEdit, btnDelete, btnCancel, btnSearch)
        txtBankName.Enabled = True
        txtBankName.AutoPostBack = True
        'Enable txtPartTypeName TextField
        'Call txtBankName_TextChanged(sender, e)
        txtBankName.Focus()                             'Set Focus to txtPartTypeName TextField
    End Sub

    Protected Sub txtBankName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBankName.TextChanged
        If Search = True Then                               'If Search Mode is true then
            Call FillGrid(Trim(txtBankName.Text))  'Fill the Grid filtering on txtStockLocationName 
        End If
        If BtnSave.Enabled Then BtnSave.Focus()
    End Sub
    Protected Sub GenerateBankCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            txtBankId.Text = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "BNK" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from Bank_Master where Bank_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    txtBankCode.Text = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub


End Class
