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
Partial Class Password
    Inherits System.Web.UI.Page
    Shared Search As Boolean                'For Checking the Search Mode
    Shared g_User As String
    Shared g_Company As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If (Not IsPostBack) Then            'If IsPostBack is false then
                'g_User = CType(Session("g_User"), System.String)
                'g_Company = CType(Session("g_Company"), System.String)
                g_User = "U1"
                g_Company = "COM1"
                ClearFields()                   'If IsPostBack is false then 
                UnLockTextBox(False)            'Unlock (Enable) TextFields
                FillGrid("")                    'Fills the DatagGrid with All Records
                Search = False                  'Default Value for Search Mode
                BtnSave.Enabled = False
                Call CheckRights(Me, g_User, "Password")
            End If
        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> 12/09/06 </DateOfCreation>
    '   <Developer> Rajesh </Developer>
    '</UserDefinedFunction>
    '-------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            TxtUserCode.Text = ""           'Assign Null to the TextField
            TxtUserName.Text = ""           'Assign Null to the TextField
            txtOldPwd.Text = ""          'Assign Null to the TextField
            txtNewPwd.Text = ""           'Assign Null to the TextField
            txtRPwd.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> 12/09/06 </DateOfCreation>
    '   <Developer> Rajesh </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------

    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            TxtUserCode.Enabled = Action             'Enable / Disable the TextField
            TxtUserName.Enabled = Action             'Enable / Disable the TextField
            txtOldPwd.Enabled = Action             'Enable / Disable the TextField
            txtNewPwd.Enabled = Action             'Enable / Disable the TextField
            txtRPwd.Enabled = Action             'Enable / Disable the TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        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="FillGrid">
    '   <Desc> To Fill the Grid with All Records </Desc>
    '   <Input> 
    '  <Param Name="PTN">
    '   PartType_Name
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 12/09/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
            gvUsers.Columns(1).Visible = True
            con.Open()                                          'Open the Connection
            If Trim(PTN) = "" Then
                qry = "select AdminUser_Kid,AdminUser_Code,AdminUser_Name from AdminUser where adminuser_companyid='" & g_Company & "' order by AdminUser_name"
            Else
                qry = "select AdminUser_Kid,AdminUser_Code,AdminUser_Name from AdminUser where adminuser_companyid='" & g_Company & "' and adminuser_name like '" & Trim(PTN) & "%' order by AdminUser_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
            gvUsers.DataSource = dr                              'Set the DataReader Variable as DataSource for the Grid
            gvUsers.DataBind()                                   'Bind the Data to the Grid
            gvUsers.Columns(1).Visible = False
        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 TxtUserCode.Text = "" Then                                           'If TxtUserCode TextField is Null then
                CreateMessageAlert(Me, "User Code Cannot be Null !", "StrKeyVal")   'Display the Message 
                TxtUserCode.Focus()
                Exit Sub
            End If
            If TxtUserName.Text = "" Then                                           'If TxtUserName TextField is Null then
                CreateMessageAlert(Me, "User Name Cannot be Null !", "StrKeyVal")   'Display the Message 
                TxtUserName.Focus()
                Exit Sub
            End If
            If Trim(txtOldPwd.Text) = "" Then
                CreateMessageAlert(Me, "Old Password Cannot be Null !", "StrKeyVal") 'Display the Message 
                txtOldPwd.Focus()
                Exit Sub
            End If
            Dim oldpwd As String
            oldpwd = ReturnValue("select AdminUser_Password from AdminUser where AdminUser_Code= '" & TxtUserCode.Text.Trim & "'")

            If txtOldPwd.Text.Trim <> oldpwd.Trim Then
                CreateMessageAlert(Me, "Old Password Does Not Match !", "StrKeyVal")   'Display the Message 
                txtOldPwd.Text = ""
                txtOldPwd.Focus()
                Exit Sub
            End If
            oldpwd = txtOldPwd.Text.Trim

            If Trim(txtNewPwd.Text) = "" Then
                CreateMessageAlert(Me, "New Password Cannot be Null !", "StrKeyVal")   'Display the Message 
                txtOldPwd.Focus()
                Exit Sub
            End If

            If txtRPwd.Text = "" Then
                CreateMessageAlert(Me, "Retype Password Cannot be Null !", "StrKeyVal")   'Display the Message 
                txtOldPwd.Focus()
                Exit Sub
            End If
            If Trim(txtRPwd.Text) <> Trim(txtNewPwd.Text) Then               'If txtRPwd TextField is not equal to txtNewPwd
                CreateMessageAlert(Me, "Please Retype Password !", "StrKeyVal")   'Display the Message
                txtOldPwd.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.Close()                                     'Close 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 = "AdminUser_Password_Proc"            'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                            'Assign the connection
            con.Open()                                      'Open Connection
            cmd.Parameters.Add(New SqlParameter("@UserId", SqlDbType.NVarChar, 20)).Value = Trim(txtUserId.Text) 'Add Parameter UserCode and assign the value of txtUserCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@NewPwd", SqlDbType.NVarChar, 50)).Value = Trim(txtNewPwd.Text) 'Add Parameter Password and assign the value of txtPassword TextField to it  
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Change Password"

            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 20)).Value = "Update"                     'Add Parameter Mode and assign the DML Operation to Execute (Update)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            con.Close()
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Password Changed Successfully", "StrKeyVal")
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            BtnSave.Enabled = False
            BtnSearch.Enabled = True
        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
            TxtUserName.AutoPostBack = False
            Search = False
            Call FillGrid("")
            BtnSave.Enabled = False
            BtnSearch.Enabled = True
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub gvUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvUsers.SelectedIndexChanged
        Try
            txtUserId.Text = gvUsers.SelectedRow.Cells(1).Text      'Assign the value of cell to the textField
            TxtUserCode.Text = gvUsers.SelectedRow.Cells(2).Text      'Assign the value of cell to the textField
            TxtUserName.Text = gvUsers.SelectedRow.Cells(3).Text      'Assign the value of cell to the textField
            UnLockTextBox(True)                                       'Enable the TextFields
            TxtUserCode.Enabled = False
            TxtUserName.Enabled = False
            Search = False
            BtnSave.Enabled = True
            BtnSearch.Enabled = False
            txtOldPwd.Focus()
        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
            Search = True                                       'Set Search Mode True
            'Call SearchClick(Me, BtnChangeRequestCategoryAdd, BtnChangeRequestCategorySave, BtnChangeRequestCategoryEdit, BtnChangeRequestCategoryDelete, BtnChangeRequestCategoryCancel, BtnChangeRequestCategorySearch)
            BtnCancel.Enabled = True
            BtnSave.Enabled = False
            BtnSearch.Enabled = False
            TxtUserName.Enabled = True
            TxtUserName.AutoPostBack = True
            'Enable txtPartTypeName TextField
            Call TxtUserName_TextChanged(sender, e)
            TxtUserName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try            'Call textchanged Event of 
    End Sub

    Protected Sub TxtUserName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUserName.TextChanged
        Try
            If Search = True Then
                Call FillGrid(TxtUserName.Text)
            End If
            If BtnSave.Enabled Then BtnSave.Focus()
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")
        End Try
    End Sub
    Sub Node_Populate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs)
        If e.Node.ChildNodes.Count = 0 Then
            Select Case (e.Node.Depth)
                Case 0

                    FillModule(e.Node, "1")

                    Exit Sub
                Case 1

                    FillMenu(e.Node)

                    Exit Sub

                Case 2

                    FillSubMenu(e.Node)

                    Exit Sub

            End Select

        End If

    End Sub
End Class
