Partial Class City_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
            lblerror.Visible = False
            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'", ddlCountryCode)
                FillGrid("", "")                  'Fills the DatagGrid with All Records
                gvCity.SelectedIndex = -1
                BtnDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
                Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, Session("g_User"), "City Master")
                ScriptManager1.SetFocus(BtnAdd)
            End If
            txtCityCode.MaxLength = 20 : txtCityName.MaxLength = 50
            txtCityName.Attributes.Add("onblur", "return CheckNull('txtCityName');")
            txtCityName.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 gvCity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            txtCityId.Text = RemoveLiterals(gvCity.SelectedRow.Cells(1).Text.ToString)     'Assign the value of cell to the textField
            txtCityCode.Text = gvCity.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtCityName.Text = gvCity.SelectedRow.Cells(3).Text     'Assign the value of cell to the textField

            Dim stateid As String
            stateid = ReturnValue("Select City_StateId from City_Master where City_IsDeleted='0' AND City_Kid='" & RemoveLiterals(gvCity.SelectedRow.Cells(1).Text.ToString) & "'")

            Dim countryid As String
            countryid = ReturnValue("Select State_CountryId from State_Master where State_Kid='" & stateid.ToString & "' and State_IsDeleted='0'")

            Fill_Combo("Select Country_Kid,Country_Name from Country_Master where Country_IsDeleted='0'", ddlCountryCode)

            ddlCountryCode.ClearSelection()
            ddlCountryCode.Items.FindByValue(countryid).Selected = True

            Fill_Combo("Select State_Kid,State_Name from State_Master where State_IsDeleted='0' AND State_CountryId='" & ddlCountryCode.SelectedItem.Value & "'", ddlStateCtcode)

            ddlStateCtcode.ClearSelection()
            ddlStateCtcode.Items.FindByText(gvCity.SelectedRow.Cells(4).Text.Trim).Selected = True
            If gvCity.SelectedRow.Cells(6).Text.Trim = "Y" Then
                chkWareHouse.Checked = True
            Else
                chkWareHouse.Checked = False
            End If
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
            txtCityName.Focus()
            ScriptManager1.SetFocus(txtCityName)
        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
            gvCity.SelectedIndex = -1
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)                                 'Enable the TextFields
            Call AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtCityId.Text = NewPrimaryKey(Me)
            txtCityCode.Text = Generate_Code("select count(*) from City_Master where City_Code='", "CT")
            txtCityName.Focus()                                 'Put the Focus to CountryCode TextField
            ddlStateCtcode.Enabled = False
            ScriptManager1.SetFocus(txtCityName)
        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 ddlCountryCode.SelectedItem.Text = "Select" Then
                CreateMessageAlert(Me, "Select Proper Country Name !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If ddlStateCtcode.SelectedItem.Text = "Select" Then
                CreateMessageAlert(Me, "Select Proper State Name !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If Trim(txtCityCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "City Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtCityName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from City_Master where City_Code='" & txtCityCode.Text.Trim & "'and City_isdeleted='0' and City_CompanyID='" & Session("g_Company") & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, "City Code Already Present !", "StrKeyVal")      'Display the Message
                txtCityCode.Text = "" : txtCityCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from City_Master where City_Name='" & txtCityName.Text.Trim & "'and City_isdeleted='0' and City_CompanyID='" & Session("g_Company") & "' and City_StateId='" & ddlStateCtcode.SelectedItem.Value & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, "City Name Already Present !", "StrKeyVal")      'Display the Message
                txtCityName.Text = "" : txtCityName.Focus()
                ScriptManager1.SetFocus(txtCityName)
                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) 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 ddlCountryCode.SelectedItem.Text = "Select" Then
                CreateMessageAlert(Me, "Select Proper Country Name !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If ddlStateCtcode.SelectedItem.Text = "Select" Then
                CreateMessageAlert(Me, "Select Proper State Name !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If Trim(txtCityCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "City Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtCityName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If

            Dim dk As String = "select count(*) from City_Master where City_Name='" & txtCityName.Text.Trim & "'AND City_StateId='" & ddlStateCtcode.SelectedValue.Trim & "' AND City_isdeleted='0' and City_CompanyID='" & Session("g_Company") & "' AND City_code <> '" & Trim(txtCityCode.Text) & "'"
            If IsAlreadyPresent(dk) Then ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " City Name Already Present !", "StrKeyVal")      'Display the Message
                txtCityName.Text = "" : txtCityName.Focus()
                ScriptManager1.SetFocus(txtCityName)
                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(txtCityCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            '    CreateMessageAlert(Me, "City Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    txtCityCode.Focus()
            '    Exit Sub
            'End If
            'If Trim(txtCityName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
            '    CreateMessageAlert(Me, "City Name Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    txtCityName.Focus()
            '    Exit Sub
            'End If
            'If IsAlreadyPresent("select count(*) from City_Master where City_Code='" & txtCityCode.Text.Trim & "'and City_Isdeleted='0' and City_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
            txtCityName.AutoPostBack = False
            Call FillGrid("", "")
            gvCity.SelectedIndex = -1
            Call CancelClickVisible(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)
        Try
            Search = True                                       'Set Search Mode True
            Call SearchClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtCityName.Enabled = True
            txtCityName.AutoPostBack = True
            'Enable txtPartTypeName TextField
            ' Call txtCountryName_TextChanged(sender, e)
            txtCityName.Focus()
            ScriptManager1.SetFocus(txtCityName)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try 'Set Focus to txtPartTypeName TextField
    End Sub

    Protected Sub txtCityName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCityName.TextChanged
        Try
            If Search = True Then                               'If Search Mode is true then
                Call FillGrid(Trim(txtCityName.Text), "")  'Fill the Grid filtering on txtStockLocationName 
            End If
            If BtnSave.Visible Then ScriptManager1.SetFocus(BtnSave)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="LoadData">
    '   <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, ByVal type As String)
        Try
            Dim strQuery As String
           
            If strID.Trim.Length = 0 Then
                strQuery = "select distinct ct.City_Kid as City_Kid,ct.City_Code as City_Code,ct.City_Name as City_Name,st.State_Name as SName,  ct.City_IsWareHouse from City_master Ct,State_Master st where st.State_Kid=ct.City_StateID and ct.City_CompanyID = '" & Session("g_Company") & "' and ct.City_IsDeleted = '0' and st.State_CompanyID = '" & Session("g_Company") & "' and st.State_IsDeleted = '0' order by st.State_Name"
            Else
                strQuery = "select distinct ct.City_Kid as City_Kid,ct.City_Code as City_Code,ct.City_Name as City_Name,st.State_Name as SName, ct.City_IsWareHouse from City_master Ct,State_Master st where st.State_Kid=ct.City_StateID and ct.City_CompanyID = '" & Session("g_Company") & "' and ct.City_IsDeleted = '0' and st.State_CompanyID = '" & Session("g_Company") & "' and st.State_IsDeleted = '0' and ct.City_Name like '" & strID.Trim & "%' order by st.State_Name"
            End If
            If type = "State" Then
                strQuery = "select distinct ct.City_Kid as City_Kid,ct.City_Code as City_Code,ct.City_Name as City_Name,st.State_Name as SName, ct.City_IsWareHouse from City_master Ct,State_Master st where st.State_Kid=ct.City_StateID and ct.City_CompanyID = '" & Session("g_Company") & "' and ct.City_IsDeleted = '0' and st.State_CompanyID = '" & Session("g_Company") & "' and st.State_IsDeleted = '0' and ct.City_StateId='" & strID.Trim & "' order by st.State_Name"
            End If
            gvCity.Columns(1).Visible = True
            gvCity.Columns(6).Visible = True
            gvCity.DataSource = getTable(strQuery.Trim, "Country")
            gvCity.DataBind()
            gvCity.Columns(1).Visible = False
            gvCity.Columns(6).Visible = False
            gvCity.SelectedIndex = -1
        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
            txtCityCode.Text = ""                           'Assign Null to the TextField
            txtCityName.Text = ""
            txtCityId.Text = ""                               'Assign Null to the TextField
            ddlStateCtcode.ClearSelection()
            ddlStateCtcode.SelectedIndex = 0
            chkWareHouse.Checked = False
            ddlCountryCode.ClearSelection()
            ddlCountryCode.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
            txtCityName.Enabled = Action                    'Enable / Disable the TextField
            ddlStateCtcode.Enabled = Action
            chkWareHouse.Enabled = Action
            ddlCountryCode.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)
        Try
            Dim iswarehouse As String = "N"
            If chkWareHouse.Checked = True Then
                iswarehouse = "Y"
            End If
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Add("@City_Kid", Data.SqlDbType.NVarChar, 50).Value = txtCityId.Text.Trim
            sqlcoll.Add("@City_Code", Data.SqlDbType.NVarChar, 50).Value = txtCityCode.Text.Trim
            sqlcoll.Add("@City_Name", Data.SqlDbType.NVarChar, 50).Value = txtCityName.Text.Trim
            sqlcoll.Add("@City_StateID", Data.SqlDbType.NVarChar, 50).Value = ddlStateCtcode.SelectedValue.Trim
            sqlcoll.Add("@City_IsWareHouse", Data.SqlDbType.NVarChar, 1).Value = iswarehouse
            sqlcoll.Add("@City_FinancialYearID", Data.SqlDbType.NVarChar, 50).Value = Session("g_FinYear")
            sqlcoll.Add("@City_CompanyID", Data.SqlDbType.NVarChar, 50).Value = Session("g_Company")
            sqlcoll.Add("@City_UserID", Data.SqlDbType.NVarChar, 50).Value = Session("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("City_Master_Proc", sqlcoll).StartsWith("True") Then
                FillGrid("", "")
                cmd = Nothing
                sqlcoll = Nothing
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub gvCity_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCity.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                If e.Row.Cells(6).Text = "Y" Then
                    e.Row.Cells(5).Text = "Yes"
                Else
                    e.Row.Cells(5).Text = "No"
                End If
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

   
    Protected Sub gvCity_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvCity.PageIndex = e.NewPageIndex
        FillGrid("", "")
        ClearFields()
        UnLockTextBox(False)
        gvCity.SelectedIndex = -1
    End Sub

    Protected Sub ddlCountryCode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountryCode.SelectedIndexChanged
        ddlStateCtcode.Enabled = True
        Fill_Combo("Select State_Kid,State_Name from State_Master where State_IsDeleted='0' AND State_CountryId='" & ddlCountryCode.SelectedItem.Value & "'", ddlStateCtcode)
    End Sub

    Protected Sub ddlStateCtcode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStateCtcode.SelectedIndexChanged

        FillGrid(ddlStateCtcode.SelectedItem.Value, "State")
    End Sub
End Class
