Partial Class State_Master
    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()
                Fill_Combo("Select Country_Kid,Country_Name from Country_Master where Country_IsDeleted='0' and Country_CompanyID='" & g_Company.Trim & "' order by Country_Name", ddlStateCntcode)
                FillGrid("")                  'Fills the DatagGrid with All Records
                gvState.SelectedIndex = -1
                Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, g_User, "State Master")
                BtnDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            End If
            txtStateCode.MaxLength = 20 : txtStateName.MaxLength = 50
            txtStateName.Attributes.Add("onblur", "return CheckNull('txtStateName');")
            txtStateName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")
            '  txtCityName.Attributes.Add("onblur", "return toUpper('txtCityName');")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub


    Protected Sub gvState_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            txtStateId.Text = gvState.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            txtStateCode.Text = gvState.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtStateName.Text = gvState.SelectedRow.Cells(3).Text     'Assign the value of cell to the textField
            ddlStateCntcode.ClearSelection()
            ddlStateCntcode.Items.FindByText(gvState.SelectedRow.Cells(4).Text.Trim).Selected = True
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
            txtStateName.Focus()
        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)
        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 AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtStateId.Text = NewPrimaryKey(Me)
            txtStateCode.Text = Generate_Code("select count(*) from State_Master where State_Code='", "ST")
            txtStateName.Focus()                                 'Put the Focus to CountryCode TextField
            FillGrid("")
        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)
        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(txtStateCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "State Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtStateName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If ddlStateCntcode.Items.FindByText("Select").Selected = True Then
                CreateMessageAlert(Me, "Select Country Name Properly  !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from State_Master where State_Code='" & txtStateCode.Text.Trim & "'and state_isdeleted='0' and State_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " State Code Already Present !", "StrKeyVal")      'Display the Message
                txtStateCode.Text = "" : txtStateCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from State_Master where State_Name='" & txtStateName.Text.Trim & "' and State_CountryId='" & ddlStateCntcode.SelectedItem.Value & "'and state_isdeleted='0' and State_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " State Name Already Present !", "StrKeyVal")      'Display the Message
                txtStateName.Text = "" : txtStateName.Focus()
                Exit Sub
            End If
            UpdateData("Insert")   'Inserts the Record
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call SaveClickVisible(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)
        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(txtStateCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "State Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtStateName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If ddlStateCntcode.Items.FindByText("Select").Selected = True Then
                CreateMessageAlert(Me, "Select Country Name Properly  !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from State_Master where State_code <>'" & Trim(txtStateCode.Text) & "'and State_Name='" & txtStateName.Text & "'and State_CountryId='" & ddlStateCntcode.SelectedItem.Value & "' and state_isdeleted='0' and State_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " State Name Already Present !", "StrKeyVal")      'Display the Message
                txtStateName.Text = "" : txtStateName.Focus()
                Exit Sub
            End If
            UpdateData("Update")  'Updates the record
            CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call EditClickVisible(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)
        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(txtStateCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "State Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtStateCode.Focus()
                Exit Sub
            End If
            If Trim(txtStateName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "State Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtStateName.Focus()
                Exit Sub
            End If
            'If IsAlreadyPresent("select count(*) from State_Master where State_Code='" & txtStateCode.Text.Trim & "'and State_isdeleted='0' and State_CompanyID='" & 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
            UpdateData("Delete")  'Deletes the record                                               'Execute the Query
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClickVisible(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)
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Search = True
            Call FillGrid("")
            txtStateName.AutoPostBack = False

            Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            gvState.SelectedIndex = -1
        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)
        Search = True                                       'Set Search Mode True
        Call SearchClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        txtStateName.Enabled = True
        txtStateName.AutoPostBack = True
        'Enable txtPartTypeName TextField
        Call txtStateName_TextChanged(sender, e)
        txtStateName.Focus()                             'Set Focus to txtPartTypeName TextField
    End Sub

    Protected Sub txtStateName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Search = True Then                               'If Search Mode is true then
            Call FillGrid(Trim(txtStateName.Text))  'Fill the Grid filtering on txtStockLocationName 
        End If
        If BtnSave.Visible Then BtnSave.Focus()
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillGrid">
    '   <Desc> For Filling the GridView </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 20/06/2007 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Sub FillGrid(ByVal strID As String)
        Try
            Dim strQuery As String
            If strID.Trim.Length = 0 Then
                strQuery = "select distinct st.State_Kid as State_Kid,st.State_Code as State_Code,st.State_Name as State_Name,cn.Country_Name as CName from State_master st,Country_Master cn where cn.Country_Kid=st.State_CountryID and st.state_CompanyID = '" & g_Company & "' and st.State_IsDeleted = '0' and cn.Country_CompanyID = '" & g_Company & "' and cn.Country_IsDeleted = '0' order by st.State_Name"
            Else
                strQuery = "select distinct st.State_Kid as State_Kid,st.State_Code as State_Code,st.State_Name as State_Name,cn.Country_Name as CName from State_master st,Country_Master cn where cn.Country_Kid=st.State_CountryID and st.state_CompanyID = '" & g_Company & "' and st.State_IsDeleted = '0' and cn.Country_CompanyID = '" & g_Company & "' and cn.Country_IsDeleted = '0' and st.State_Name like'" & strID.Trim & "%' order by st.State_Name "
            End If
            gvState.Columns(1).Visible = True
            gvState.DataSource = getTable(strQuery.Trim, "Country")
            gvState.DataBind()
            gvState.Columns(1).Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 20/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            txtStateCode.Text = ""                           'Assign Null to the TextField
            txtStateName.Text = ""
            txtStateId.Text = ""                               'Assign Null to the TextField
            ddlStateCntcode.ClearSelection()
            ddlStateCntcode.SelectedIndex = 0
        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> 20/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            'txtCountryCode.Enabled = Action                    'Enable / Disable the TextField
            txtStateName.Enabled = Action                    'Enable / Disable the TextField
            ddlStateCntcode.Enabled = Action
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UpdateData">
    '   <Desc> For Inserting,Updating and Delete Records from DataBase </Desc>
    '   <Input> 
    '   <Param Name="strMode">
    '   Insert,Update or Delete
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 20/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Private Sub UpdateData(ByVal strMode As String)
        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
        sqlcoll.Add("@State_Kid", Data.SqlDbType.NVarChar, 50).Value = txtStateId.Text.Trim
        sqlcoll.Add("@State_Code", Data.SqlDbType.NVarChar, 50).Value = txtStateCode.Text.Trim
        sqlcoll.Add("@State_Name", Data.SqlDbType.NVarChar, 50).Value = txtStateName.Text.Trim
        sqlcoll.Add("@State_CountryID", Data.SqlDbType.NVarChar, 50).Value = ddlStateCntcode.SelectedValue.Trim
        sqlcoll.Add("@State_FinancialYearID", Data.SqlDbType.NVarChar, 50).Value = g_FinYear
        sqlcoll.Add("@State_CompanyID", Data.SqlDbType.NVarChar, 50).Value = g_Company
        sqlcoll.Add("@State_UserID", Data.SqlDbType.NVarChar, 50).Value = g_User
        sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
        sqlcoll.Add("@Mode", Data.SqlDbType.Char, 15).Value = strMode.Trim
        If ExecQuery("State_Master_Proc", sqlcoll).StartsWith("True") Then
            FillGrid("")
            cmd = Nothing
            sqlcoll = Nothing
        End If
    End Sub

    Protected Sub gvState_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvState.PageIndex = e.NewPageIndex
        FillGrid("")
        gvState.SelectedIndex = -1
        ClearFields()
        UnLockTextBox(False)
        Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
    End Sub
End Class
