Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports System.Math
Imports System.IO
Imports System.EnterpriseServices
Imports System.Drawing
'Imports System.Web.HttpWriter
Imports System.Web.Security
Imports System.Resources.Tools
Imports System.Configuration
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.Sql
Imports System.Text
Partial Class Change_Password
    Inherits System.Web.UI.Page
    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
                'g_User = CType(Session("g_User"), System.String)          'It's Already Defined Glogal Shared Variable
                'g_Company = CType(Session("g_Company"), System.String)       'It's Already Defined Glogal Shared Variable
                ' ClearFields()                   'If IsPostBack is false then 
                ' UnLockTextBox(True)            'Unlock (Enable) TextFields
                'Fills the DatagGrid with All Records
                Call Fill_Combo("select UserType_Kid,UserType_Name from UserType where UserType_CompanyId='" & Session("g_Company").ToString & "' and UserType_IsDeleted='0' order by UserType_Name", ddlUserType1)
            End If
            'TxtUserName.MaxLength = 50 : TxtUserCode.MaxLength = 50
            lblerror.Text = ""
            lblerror.Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            lblerror.Visible = False
            If Len(txtPassword.Text) <= 6 Then
                showmessage("Password must be greater than or equalt to six character !")                              'Display the Message 
                txtPassword.Text = ""
                txtPassword.Focus()
                Exit Sub
            End If

            If Trim(txtrPassword.Text) <> Trim(txtPassword.Text) Then               'If txtRPassword TextField is not equal to txtRPassword then
                showmessage("Please Retype Password !")                              'Display the Message 
                txtrPassword.Text = ""
                txtPassword.Focus()
                Exit Sub
            End If
            Dim cipher As String
            Dim ciphernew As String
            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()
            Dim Str As String = "select  AdminUser_Password,AdminUser_Code from AdminUser where AdminUser_Kid ='" & ddlusername1.SelectedItem.Value & "' "
            Dim cmd As New SqlCommand(Str, con)                       'Define command variable to execute the query   
            Dim dr As SqlDataReader = cmd.ExecuteReader()

            cipher = Encrypt(txtoldpassword.Text)
            If dr.Read Then
                If Trim(cipher.ToString) = Trim(dr(0)) Then
                    '  If Trim(txtoldpassword.Text) = Trim(dr(0)) Then
                    TxtUserCode.Text = Trim(dr(1))
                    dr.Close()
                    con.Close()

                    con.Open()
                    ciphernew = Encrypt(txtPassword.Text)
                    ' Str = "update AdminUser  set  AdminUser_Password='" & Trim(txtPassword.Text) & "' where AdminUser_Kid='" & Trim(ddlusername1.SelectedItem.Value) & "'"
                    Str = "update AdminUser  set  AdminUser_Password='" & ciphernew.ToString & "' where AdminUser_Kid='" & Trim(ddlusername1.SelectedItem.Value) & "'"
                    cmd = New SqlCommand(Str, con)
                    cmd.ExecuteNonQuery()
                    If con.State = ConnectionState.Open Then
                        con.Close()                                     'Close the Connection
                    End If
                    cmd.Dispose()
                    con.Dispose()
                    showmessage("Record Modified Successfully")                                     'Display the Message 
                Else
                    showmessage("Old password does not match.")                                     'Display the Message 
                End If

            End If


            'ClearFields()                                                                   'Clear the TextFields
            'UnLockTextBox(False)                                                            'Disable the TextFields

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try


    End Sub

    Protected Sub ddlUserType1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Call Fill_Combo("select AdminUser_Kid,AdminUser_Name from AdminUser where AdminUser_CompanyId='" & Session("g_Company").ToString & "' and AdminUser_IsDeleted='0' and AdminUser_UserTypeId ='" & ddlUserType1.SelectedValue & "' order by adminuser_name", ddlusername1)  'Fill dropdownlist
    End Sub
    Protected Sub showmessage(ByVal msg As String)
        'This function is uesd to show the message
        lblerror.Visible = True
        lblerror.Text = msg
    End Sub

End Class
