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 System.Data
Imports System.Data.SqlClient
Imports ModCommon
Partial Class BranchMaster
    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)
                Call CheckRights(Me, Session("g_User"), "Branch Master")
                Search = False              'Default Value for Search Mode
                Call FillCombos()
            End If
            txtBranchCode.MaxLength = 20 : txtBranchName.MaxLength = 50
            'txtBranchName.Attributes.Add("onblur", "return CheckNull('txtBranchName');")
            txtBranchName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    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
            txtBranchCode.Text = ""                             'Assign Null to the TextField
            txtBranchName.Text = ""                             'Assign Null to the TextField
            ddlBankName.SelectedIndex = -1                              'Assign Null to the DropDown
            ddlCountry.SelectedIndex = -1
            ddlState.SelectedIndex = -1                             'Assign Null to the DropDown
            ddlCity.SelectedIndex = -1                               'Assign Null to the DropDown
            ddlLocation.SelectedIndex = -1                               'Assign Null to the DropDown
        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
            'txtBranchCode.Enabled = Action                      'Enable / Disable the TextField
            txtBranchName.Enabled = Action                      'Enable / Disable the TextField
            ddlBankName.Enabled = Action                        'Enable / Disable the DropDown
            ddlCountry.Enabled = Action                         'Enable / Disable the DropDown
            ddlState.Enabled = Action                           'Enable / Disable the DropDown
            ddlCity.Enabled = Action                            'Enable / Disable the DropDown
            ddlLocation.Enabled = Action                        'Enable / Disable the DropDown
        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
            If Trim(PTN) = "" Then
                qry = "select distinct Branch_Kid,Branch_Code as Code,Branch_Name as [Branch],Bank_Name as [Bank] from Branch_Master br,Bank_Master bm where Branch_BankId=Bank_Kid and br.branch_isdeleted=0 and br.branch_companyId='" & g_Company & "' and bm.bank_isdeleted=0 and bm.bank_companyId='" & g_Company & "' order by branch_Name "
            Else
                qry = "select  distinct Branch_Kid,Branch_Code as Code,Branch_Name as [Branch],Bank_Name as [Bank]  from Branch_Master br,Bank_Master bm where Branch_BankId=Bank_Kid and branch_name like '" & Trim(PTN) & "%'and br.branch_isdeleted=0 and br.branch_companyId='" & g_Company & "' and bm.bank_isdeleted=0 and bm.bank_companyId='" & g_Company & "'  order by branch_name"
            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
            gvBranch.DataSource = dr                          'Set the DataReader Variable as DataSource for the Grid
            gvBranch.DataBind()                               'Bind the Data to the Grid
        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 CType(Session("Add_Flag"), System.Boolean) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Insert 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 GenerateCode()
            txtBranchName.Focus()                             'Put the Focus to PartTypeCode TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub gvBranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvBranch.SelectedIndexChanged
        Try
            Dim Qry As String = ""                                  'Define a String Variable for Query
            Dim Dt As DataTable                                     'Define a Datatable to Fill All TextBox & DDL Values
            Call ClearFields()                                      'Clear the TextFields
            UnLockTextBox(True)                                     'Enable the TextFields
            txtBranchKid.Text = gvBranch.SelectedRow.Cells(1).Text.Trim     'Assign the value of cell to the textField
            txtBranchCode.Text = gvBranch.SelectedRow.Cells(2).Text.Trim     'Assign the value of cell to the textField
            txtBranchName.Text = gvBranch.SelectedRow.Cells(3).Text.Trim     'Assign the value of cell to the textField
            txtBranchCode.Enabled = False

            Qry = "select a.branch_Kid,a.branch_code as Branch_Code,a.branch_name as Branch_Name,b.bank_name as Bank_Name,c.country_name as Country_name,d.state_name as State_name,e.city_name as City_Name,f.Location_Name as Location_Name from "
            Qry = Qry & " branch_master a,bank_master b,country_master c,state_master d,city_master e,location_master f where"
            Qry = Qry & " a.branch_bankId=b.bank_Kid and c.country_Kid=d.state_countryId and d.state_Kid=e.city_stateId and"
            Qry = Qry & " e.city_Kid=f.location_cityId and f.location_Kid=a.Branch_LocationId and a.branch_code='" & Trim(txtBranchCode.Text) & "'and a.branch_isdeleted=0 and a.branch_companyId='" & g_Company & "' and b.bank_isdeleted=0 and b.Bank_companyId='" & g_Company & "' and c.country_isdeleted=0 and c.country_companyId='" & g_Company & "' and d.state_isdeleted=0 and d.state_companyId='" & g_Company & "' and e.city_isdeleted=0 and e.city_companyId='" & g_Company & "' and f.location_isdeleted=0 and f.location_companyId='" & g_Company & "' "


            Dt = ReturnDataTable(Qry)              '' Fill Datatable with Corresponding Rows
            If Dt Is Nothing Then Exit Sub

            If Dt.Rows.Count > 0 Then
                txtBranchKid.Text = Dt.Rows(0).Item("Branch_Kid")     'Assign the DataTable Item value to the textField
                ' txtBranchCode.Text = Dt.Rows(1).Item("Branch_Code")     'Assign the DataTable Item value to the textField
                'txtBranchName.Text = Dt.Rows(2).Item("Branch_Name")     'Assign the DataTable Item value to the textField
                ddlBankName.SelectedIndex = ddlBankName.Items.IndexOf(ddlBankName.Items.FindByText(Dt.Rows(0).Item("bank_Name").ToString))  'Assign the DataTable Item value to the textField
                ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText(Dt.Rows(0).Item("Country_Name").ToString))  'Assign the DataTable Item value to the textField
                ddlCountry_SelectedIndexChanged(sender, e)
                ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(Dt.Rows(0).Item("State_Name").ToString))  'Assign the DataTable Item value to the textField
                ddlState_SelectedIndexChanged(sender, e)
                ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(Dt.Rows(0).Item("City_Name").ToString))  'Assign the DataTable Item value to the textField
                ddlCity_SelectedIndexChanged(sender, e)
                ddlLocation.SelectedIndex = ddlLocation.Items.IndexOf(ddlLocation.Items.FindByText(Dt.Rows(0).Item("Location_Name").ToString))  'Assign the DataTable Item value to the textField

            End If

            Call GridClick(Me, btnAdd, btnSave, btnEdit, btnDelete, btnCancel, btnSearch)
            Search = False
            txtBranchName.Focus()
            Dt = Nothing
        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 CType(Session("Add_Flag"), System.Boolean) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Insert New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If

            If Trim(txtBranchCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Branch Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBranchCode.Focus()
                Exit Sub
            End If
            If Trim(txtBranchName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Branch Name Cannot be Null !", "StrKeyVal")      'Display the Message
                txtBranchName.Focus()
                Exit Sub
            End If
            If ddlBankName.SelectedIndex = 0 Or ddlBankName.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlBankName.Focus()
                Exit Sub
            End If
            If ddlCountry.SelectedIndex = 0 Or ddlCountry.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCountry.Focus()
                Exit Sub
            End If
            If ddlState.SelectedIndex = 0 Or ddlState.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlState.Focus()
                Exit Sub
            End If
            If ddlCity.SelectedIndex = 0 Or ddlCity.SelectedIndex = -1 Then                                       'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCity.Focus()
                Exit Sub
            End If
            If ddlLocation.SelectedIndex = 0 Or ddlLocation.SelectedIndex = -1 Then                                          'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Location Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlLocation.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
            If IsAlreadyPresent("select count(*) from Branch_Master where Branch_Kid='" & txtBranchKid.Text.Trim & "'and branch_isdeleted=0 and branch_companyId='" & g_Company & "' ") Then  ' To Check Existance of Branch Kid
                CreateMessageAlert(Me, " Branch Code Already Present !", "StrKeyVal")      'Display the Message
                txtBranchCode.Text = "" : txtBranchCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Branch_Master where Branch_Code='" & txtBranchCode.Text.Trim & "'and branch_isdeleted=0 and branch_companyId='" & g_Company & "' ") Then  ' To Check Existance of Branch Code
                CreateMessageAlert(Me, " Branch Code Already Present !", "StrKeyVal")      'Display the Message
                txtBranchCode.Text = "" : txtBranchCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Branch_Master where  branch_bankcode='" & ddlBankName.SelectedItem.Value.Trim & "'and  branch_code != '" & txtBranchCode.Text & "' and  Branch_name='" & txtBranchName.Text.Trim & "'and branch_isdeleted=0 and branch_companyId='" & g_Company & "' ") Then  ' To Check Existance of Branch Code
                CreateMessageAlert(Me, " Branch Name Already Present !", "StrKeyVal")      'Display the Message
                txtBranchName.Text = "" : txtBranchName.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 = "Branch_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@Branch_Kid", SqlDbType.NVarChar, 10)).Value = Trim(txtBranchKid.Text)       'Add Parameter Branch_Kid and assign the value of new primary key id to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBranchCode.Text)       'Add Parameter Branch_Code and assign the value of txtBranchCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Name", SqlDbType.NVarChar, 50)).Value = Trim(txtBranchName.Text)       'Add Parameter Branch_Name and assign the value of txtBranchName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_BankId", SqlDbType.NVarChar, 20)).Value = Trim(ddlBankName.SelectedItem.Value)        'Add Parameter Bank_Name and assign the value of ddlBankName DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_LocationId", SqlDbType.NVarChar, 25)).Value = Trim(ddlLocation.SelectedItem.Value)       'Add Parameter Location_Name and assign the value of ddlLocation DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_FinancialYearID", SqlDbType.NVarChar, 20)).Value = g_FinYear.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_CompanyId", SqlDbType.NVarChar, 20)).Value = g_Company.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_UserId", SqlDbType.NVarChar, 20)).Value = Session("g_User").Trim.ToString  'Add Parameter user id and assign the value of currant user id to it  
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "Branch Master"    'Add Parameter form name and assign the value of current form name 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 CType(Session("Modify_Flag"), System.Boolean) = 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(txtBranchCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Branch Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBranchCode.Focus()
                Exit Sub
            End If
            If Trim(txtBranchName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Branch Name Cannot be Null !", "StrKeyVal")      'Display the Message
                txtBranchName.Focus()
                Exit Sub
            End If
            If ddlBankName.SelectedIndex = 0 Or ddlBankName.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlBankName.Focus()
                Exit Sub
            End If
            If ddlCountry.SelectedIndex = 0 Or ddlCountry.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCountry.Focus()
                Exit Sub
            End If
            If ddlState.SelectedIndex = 0 Or ddlState.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlState.Focus()
                Exit Sub
            End If
            If ddlCity.SelectedIndex = 0 Or ddlCity.SelectedIndex = -1 Then                                       'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCity.Focus()
                Exit Sub
            End If
            If ddlLocation.SelectedIndex = 0 Or ddlLocation.SelectedIndex = -1 Then                                          'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Location Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlLocation.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Branch_Master where  branch_bankId='" & ddlBankName.SelectedItem.Value.Trim & "'and  branch_kid != '" & txtBranchKid.Text & "' and  Branch_name='" & txtBranchName.Text.Trim & "'and branch_isdeleted=0 and branch_companyId='" & g_Company & "' ") Then  ' To Check Existance of Branch Code
                CreateMessageAlert(Me, " Branch Name Already Present !", "StrKeyVal")      'Display the Message
                txtBranchName.Text = "" : txtBranchName.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 = "Branch_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@Branch_Kid", SqlDbType.NVarChar, 10)).Value = Trim(RemoveLiterals(txtBranchKid.Text))       'Add Parameter Branch_Kid and assign the value of new primary key id to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBranchCode.Text)       'Add Parameter Branch_Code and assign the value of txtBranchCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Name", SqlDbType.NVarChar, 50)).Value = Trim(txtBranchName.Text)       'Add Parameter Branch_Name and assign the value of txtBranchName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_BankId", SqlDbType.NVarChar, 20)).Value = Trim(ddlBankName.SelectedItem.Value)        'Add Parameter Bank_Name and assign the value of ddlBankName DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_LocationId", SqlDbType.NVarChar, 25)).Value = Trim(ddlLocation.SelectedItem.Value)       'Add Parameter Location_Name and assign the value of ddlLocation DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_FinancialYearID", SqlDbType.NVarChar, 20)).Value = g_FinYear.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_CompanyId", SqlDbType.NVarChar, 20)).Value = g_Company.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_UserId", SqlDbType.NVarChar, 20)).Value = Session("g_User").Trim.ToString  'Add Parameter user id and assign the value of currant user id to it  
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "Branch Master"    'Add Parameter form name and assign the value of current form name to it  
            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 CType(Session("Delete_Flag"), System.Boolean) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Delete The Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If

            If Trim(txtBranchCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Branch Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBranchCode.Focus()
                Exit Sub
            End If
            If Trim(txtBranchName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Branch Name Cannot be Null !", "StrKeyVal")      'Display the Message
                txtBranchName.Focus()
                Exit Sub
            End If
            If ddlBankName.SelectedIndex = 0 Or ddlBankName.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Bank Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlBankName.Focus()
                Exit Sub
            End If
            If ddlCountry.SelectedIndex = 0 Or ddlCountry.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCountry.Focus()
                Exit Sub
            End If
            If ddlState.SelectedIndex = 0 Or ddlState.SelectedIndex = -1 Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlState.Focus()
                Exit Sub
            End If
            If ddlCity.SelectedIndex = 0 Or ddlCity.SelectedIndex = -1 Then                                       'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlCity.Focus()
                Exit Sub
            End If
            If ddlLocation.SelectedIndex = 0 Or ddlLocation.SelectedIndex = -1 Then                                          'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Location Name Cannot be Null !", "StrKeyVal")      'Display the Message
                ddlLocation.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 = "Branch_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@Branch_Kid", SqlDbType.NVarChar, 10)).Value = Trim(RemoveLiterals(txtBranchKid.Text)) 'Add Parameter Branch_Kid and assign the value of new primary key id to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBranchCode.Text)       'Add Parameter Branch_Code and assign the value of txtBranchCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_Name", SqlDbType.NVarChar, 50)).Value = Trim(txtBranchName.Text)       'Add Parameter Branch_Name and assign the value of txtBranchName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Branch_BankId", SqlDbType.NVarChar, 20)).Value = Trim(ddlBankName.SelectedItem.Value)        'Add Parameter Bank_Name and assign the value of ddlBankName DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_LocationId", SqlDbType.NVarChar, 25)).Value = Trim(ddlLocation.SelectedItem.Value)       'Add Parameter Location_Name and assign the value of ddlLocation DropDown to it
            cmd.Parameters.Add(New SqlParameter("@Branch_FinancialYearID", SqlDbType.NVarChar, 20)).Value = g_FinYear.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_CompanyId", SqlDbType.NVarChar, 20)).Value = g_Company.Trim.ToString 'Add Parameter company id and assign the value of current company id to it  
            cmd.Parameters.Add(New SqlParameter("@Branch_UserId", SqlDbType.NVarChar, 20)).Value = Session("g_User").Trim.ToString  'Add Parameter user id and assign the value of currant user id to it  
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "Branch Master"    'Add Parameter form name and assign the value of current form name to it  
            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
            txtBranchName.AutoPostBack = False
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Call CancelClick(Me, btnAdd, btnSave, btnEdit, btnDelete, btnCancel, btnSearch)
            Search = False
            Call FillGrid("")
        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
        Try

            txtBranchName.AutoPostBack = True
            Call SearchClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = True                                       'Set Search Mode True
            txtBranchName.Enabled = True                      'Enable txtBranchName TextField
            Call txtBranchName_TextChanged(sender, e)
            txtBranchName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try 'Set Focus to txtBranchName TextField
    End Sub

    Protected Sub txtBranchName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBranchName.TextChanged
        If Search = True Then                               'If Search Mode is true then
            Call FillGrid(Trim(txtBranchName.Text))       'Fill the Grid filtering on PartType_Name 
        End If
        If BtnSave.Enabled Then BtnSave.Focus()
    End Sub

    Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountry.SelectedIndexChanged
        Try
            Dim Qry As String
            Qry = "Select Distinct State_Kid,State_Name from State_Master " & _
                  " where State_CountryId='" & ddlCountry.SelectedItem.Value & "' and state_isdeleted=0 and state_companyId='" & g_Company & "' " & _
                  " order by State_Name "
            Fill_Combo(Qry, ddlState)
            ddlCity.Items.Clear()
            ddlLocation.Items.Clear()
            If ddlState.Enabled Then ddlState.Focus()
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub ddlState_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlState.SelectedIndexChanged
        Dim Qry As String
        Try
            Qry = "Select Distinct City_Kid,City_Name from City_Master " & _
                  " where City_StateId='" & ddlState.SelectedItem.Value & "' and city_isdeleted=0 and city_companyId='" & g_Company & "'  " & _
                  " order by City_Name "
            Fill_Combo(Qry, ddlCity)
            ddlLocation.Items.Clear()
            If ddlCity.Enabled Then ddlCity.Focus()
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged
        Dim Qry As String
        Try
            Qry = "Select Distinct Location_Kid,Location_Name from Location_Master  where Location_CityId='" & ddlCity.SelectedItem.Value & "' and location_isdeleted=0 and location_companyId='" & g_Company & "' order by Location_Name"
            Fill_Combo(Qry, ddlLocation)
            If ddlLocation.Enabled Then ddlLocation.Focus()
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Private Sub FillCombos()
        Try
            Fill_Combo("select Distinct Bank_Kid,Bank_Name from Bank_Master where bank_isdeleted=0 and bank_companyId='" & g_Company & "'  order by Bank_Name", ddlBankName) '' Fill Country DropDownList
            Fill_Combo("select Distinct Country_Kid,Country_Name from Country_Master where country_isdeleted=0 and country_companyId='" & g_Company & "'  order by Country_Name", ddlCountry) '' Fill Country DropDownList
            Fill_Combo("Select Distinct State_Kid,State_Name from State_Master where state_isdeleted=0 and state_companyId='" & g_Company & "'  order by State_Name", ddlState) '' Fill State DropDownList
            Fill_Combo("Select Distinct City_Kid,City_Name from City_Master where city_isdeleted=0 and city_companyId='" & g_Company & "'  order by City_Name", ddlCity) '' Fill City DropDownList
            Fill_Combo("Select Distinct Location_Kid,Location_Name from Location_Master where location_isdeleted=0 and location_companyId='" & g_Company & "'  order by Location_Name", ddlLocation) '' Fill Location DropDownList
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Private Sub GenerateCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            txtBranchKid.Text = Trim(NewPrimaryKey(Me))
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "BR" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "Select count(*) from branch_Master where branch_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = (Val(i) + 1).ToString
                    i = Format(Val(i), "000")
                Else
                    txtBranchCode.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