Option Explicit On 
Option Strict On
Imports System.Data.SqlClient

Public MustInherit Class ChangeTeacherPass
	Inherits System.Web.UI.UserControl
	Protected WithEvents lblErrorMsg As System.Web.UI.WebControls.Label
	Protected WithEvents txtOldPass As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtNewPass As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtConfPass As System.Web.UI.WebControls.TextBox
	Protected WithEvents btnSave As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	End Sub

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region

	Dim _TeacherID As String
	Dim _AlignTable As System.Web.UI.WebControls.HorizontalAlign

	Public Event PasswordChanged()
	Public Property TeacherID() As String
		Get
			Return _TeacherID
		End Get
		Set(ByVal Value As String)
			_TeacherID = Value
		End Set
	End Property

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If Trim(txtOldPass.Text) = "" Then
			lblErrorMsg.Text = "Old Password was not entered!"
			lblErrorMsg.Visible = True
			Exit Sub
		ElseIf Trim(txtNewPass.Text) = "" Then
			lblErrorMsg.Text = "New Password was not entered!"
			lblErrorMsg.Visible = True
			Exit Sub
		ElseIf Trim(txtConfPass.Text) = "" Then
			lblErrorMsg.Text = "Confirm Password was not entered!"
			lblErrorMsg.Visible = True
			Exit Sub
		ElseIf txtNewPass.Text <> txtConfPass.Text Then
			lblErrorMsg.Text = "New Password and Confirm Password did not match!"
			lblErrorMsg.Visible = True
			Exit Sub
		Else
			Dim SqlConn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
			SqlConn.Open()
			Dim cmd As New SqlCommand()
			With cmd
				.CommandText = "Update as_Teachers set password=@TPass where LoginName='" & _TeacherID & "'"
				.Parameters.Add("@TPass", txtNewPass.Text)
				.Parameters("@TPass").Value = txtNewPass.Text
				.CommandType = CommandType.Text
				.Connection = SqlConn
				Try
					.ExecuteNonQuery()
				Catch ex As Exception
					Response.Write(ex.ToString)
					If SqlConn.State = ConnectionState.Open Then SqlConn.Close()
					Exit Sub
				End Try
			End With
			lblErrorMsg.Text = "Password changed successfully!"
			lblErrorMsg.Visible = True
			RaiseEvent PasswordChanged()
			SqlConn.Close()
			SqlConn.Dispose()
		End If
	End Sub

End Class